From 715ec9e4bc681bf2e1d5236d0dc4098212ddf9e6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Fri, 3 Mar 2023 17:21:53 +0100
Subject: [PATCH] Fix bytecode for block+mfs>0 in static model

Variable indices would be incorrect in the evaluated Jacobian if recursive
variables were present. This would lead to incorrect results and/or crashes in
bytecode MEX. This bug has been exposed by commit
f45a99fc68dc2b5ceade836715666cb7a00599f1, which actually enabled mfs>0 for
static models.
---
 src/StaticModel.hh | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/StaticModel.hh b/src/StaticModel.hh
index a6f2b666..8f02c004 100644
--- a/src/StaticModel.hh
+++ b/src/StaticModel.hh
@@ -87,7 +87,8 @@ private:
   int
   getBlockJacobianEndoCol([[maybe_unused]] int blk, int var, [[maybe_unused]] int lag) const override
   {
-    return var;
+    assert(var >= blocks[blk].getRecursiveSize());
+    return var - blocks[blk].getRecursiveSize();
   }
 
   // Write the block structure of the model in the driver file
-- 
GitLab