From f6adb1d9ad9d05c05e9b7e812f96905842d6c0c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Thu, 2 Nov 2023 16:35:31 +0100 Subject: [PATCH] Bytecode: safer handling of block= option --- matlab/evaluate_steady_state.m | 4 ++-- mex/sources/bytecode/bytecode.cc | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/matlab/evaluate_steady_state.m b/matlab/evaluate_steady_state.m index e5a6c7a47..764c7dbab 100644 --- a/matlab/evaluate_steady_state.m +++ b/matlab/evaluate_steady_state.m @@ -426,7 +426,7 @@ elseif options_.bytecode % Also update the temporary terms vector (needed for the dynare_solve case) try [~, ~, ys, T] = bytecode(M_, options_, ys, exo_ss, params, ys, 1, ys, T, 'evaluate', 'static', ... - 'block_decomposed', ['block = ' int2str(b)]); + 'block_decomposed', ['block=' int2str(b)]); catch ME if options_.verbosity >= 1 disp(ME.message); @@ -521,5 +521,5 @@ function [r, g1] = block_bytecode_mfs_steadystate(y, b, y_all, exo, params, T, M % Wrapper around the static files, for bytecode with block mfs_idx = M_.block_structure_stat.block(b).variable(end-M_.block_structure_stat.block(b).mfs+1:end); y_all(mfs_idx) = y; -[r, g1] = bytecode(M_, options_, y_all, exo, params, y_all, 1, y_all, T, 'evaluate', 'static', 'block_decomposed', ['block = ' int2str(b) ]); +[r, g1] = bytecode(M_, options_, y_all, exo, params, y_all, 1, y_all, T, 'evaluate', 'static', 'block_decomposed', ['block=' int2str(b) ]); g1 = g1(:,end-M_.block_structure_stat.block(b).mfs+1:end); % Make Jacobian square if mfs>0 diff --git a/mex/sources/bytecode/bytecode.cc b/mex/sources/bytecode/bytecode.cc index accd4f546..1bed2a1b7 100644 --- a/mex/sources/bytecode/bytecode.cc +++ b/mex/sources/bytecode/bytecode.cc @@ -65,7 +65,6 @@ Get_Arguments_and_global_variables(int nrhs, bool *extended_path, mxArray **ep_struct) { int count_array_argument {0}; - size_t pos; *extended_path = false; for (int i = 0; i < nrhs; i++) { @@ -132,15 +131,16 @@ Get_Arguments_and_global_variables(int nrhs, print = true; else { - pos = 0; - if (Get_Argument(prhs[i]).substr(0, 5) == "block") + if (Get_Argument(prhs[i]).substr(0, 6) == "block=") { - size_t pos1 = Get_Argument(prhs[i]).find("=", pos + 5); - if (pos1 != string::npos) - pos = pos1 + 1; - else - pos += 5; - block = atoi(Get_Argument(prhs[i]).substr(pos, string::npos).c_str())-1; + try + { + block = stoi(Get_Argument(prhs[i]).substr(6))-1; + } + catch (...) + { + throw FatalException{"ERROR: incorrect syntax for the 'block=' option"}; + } } else if (Get_Argument(prhs[i]).substr(0, 13) == "extended_path") {