From cb46a2697eb48c92cb34e2a8ede4845f9eabc22d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Mon, 2 Jul 2018 16:37:46 +0200
Subject: [PATCH] Fix invalid memory write in k-order DLL

The bug would show at order 3 when the last output argument (derivs) is not
requested (in practice every 3rd order solution without pruning). The DLL would
still attempt to write into it, causing an invalid memory access.
---
 mex/sources/k_order_perturbation/k_order_perturbation.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mex/sources/k_order_perturbation/k_order_perturbation.cc b/mex/sources/k_order_perturbation/k_order_perturbation.cc
index 1159b810b..1e6a88cc0 100644
--- a/mex/sources/k_order_perturbation/k_order_perturbation.cc
+++ b/mex/sources/k_order_perturbation/k_order_perturbation.cc
@@ -293,7 +293,7 @@ extern "C" {
                 ++ii;
 
               }
-            if (kOrder == 3 && nlhs > 4)
+            if (kOrder == 3 && nlhs > 5)
               {
                 const FGSContainer *derivs = app.get_rule_ders();
                 const std::string fieldnames[] = {"gy", "gu", "gyy", "gyu", "guu", "gss",
-- 
GitLab