From 77a8914b7f960a304406ba4fd39b410c50f25be8 Mon Sep 17 00:00:00 2001 From: Christopher Head Date: Fri, 7 Jun 2019 11:40:25 -0700 Subject: [PATCH] helper/command: make command_run_line reentrant MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `command_run_line` function contains a comment saying it should be reentrant. However, it isn’t: it NULLs out `current_target_override` and doesn’t restore it before returning, and it changes the `context` associated data of the `interp` object and then deletes that associated data before returning rather than restoring it to its previous value. Change-Id: I84fd46ef7173f08cf7c57b9a5b76e4986a60816f Signed-off-by: Christopher Head Reviewed-on: http://openocd.zylin.com/5223 Tested-by: jenkins Reviewed-by: Antonio Borneo Reviewed-by: Tomas Vanek --- src/helper/command.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/helper/command.c b/src/helper/command.c index ab0654b6e..7b93df6e1 100644 --- a/src/helper/command.c +++ b/src/helper/command.c @@ -652,9 +652,11 @@ int command_run_line(struct command_context *context, char *line) * happen when the Jim Tcl interpreter is provided by eCos for * instance. */ + struct target *saved_target_override = context->current_target_override; context->current_target_override = NULL; Jim_Interp *interp = context->interp; + struct command_context *old_context = Jim_GetAssocData(interp, "context"); Jim_DeleteAssocData(interp, "context"); retcode = Jim_SetAssocData(interp, "context", NULL, context); if (retcode == JIM_OK) { @@ -667,7 +669,11 @@ int command_run_line(struct command_context *context, char *line) Jim_DeleteAssocData(interp, "retval"); } Jim_DeleteAssocData(interp, "context"); + int inner_retcode = Jim_SetAssocData(interp, "context", NULL, old_context); + if (retcode == JIM_OK) + retcode = inner_retcode; } + context->current_target_override = saved_target_override; if (retcode == JIM_OK) { const char *result; int reslen;