From 7bca47385d95e666a37f412520aca6de2abb6d7f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Thu, 23 Jul 2020 11:04:09 +0200
Subject: [PATCH] Fixes to include_eqs/exclude_eqs
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

— fix segfault due to bad handling of iterators while erasing elements of a set
— fix logic for excluding endogenous variable when it is present on the LHS
---
 src/ModelTree.cc | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/src/ModelTree.cc b/src/ModelTree.cc
index 856c2260..1bbcb48c 100644
--- a/src/ModelTree.cc
+++ b/src/ModelTree.cc
@@ -1632,12 +1632,15 @@ ModelTree::includeExcludeEquations(set<pair<string, string>> &eqs, bool exclude_
 
   // Get equation numbers of tags
   set<int> tag_eqns;
-  for (auto &it : eqs)
-    if (auto tmp = equation_tags.getEqnsByTag(it.first, it.second); !tmp.empty())
+  for (auto it = eqs.begin(); it != eqs.end();)
+    if (auto tmp = equation_tags.getEqnsByTag(it->first, it->second);
+        !tmp.empty())
       {
         tag_eqns.insert(tmp.begin(), tmp.end());
-        eqs.erase(it);
+        it = eqs.erase(it);
       }
+    else
+      ++it;
 
   if (tag_eqns.empty())
     return excluded_vars;
@@ -1658,8 +1661,9 @@ ModelTree::includeExcludeEquations(set<pair<string, string>> &eqs, bool exclude_
     if (eqns.find(i) != eqns.end())
       {
         if (auto tmp = equation_tags.getTagValueByEqnAndKey(i, "endogenous"); !tmp.empty())
+          excluded_vars.push_back(symbol_table.getID(tmp));
+        else
           {
-            excluded_vars.push_back(symbol_table.getID(tmp));
             set<pair<int, int>> result;
             equations[i]->arg1->collectDynamicVariables(SymbolType::endogenous, result);
             if (result.size() == 1)
-- 
GitLab