aboutsummaryrefslogtreecommitdiff
path: root/src/helper/command.c
diff options
context:
space:
mode:
authorAntonio Borneo <borneo.antonio@gmail.com>2021-04-25 20:38:58 +0200
committerAntonio Borneo <borneo.antonio@gmail.com>2021-05-01 13:37:13 +0100
commit89c6b93ba278e26113aadcc3b7d357e05264beba (patch)
treea7e00077d833573753fa3ffcec9c3db0100f9056 /src/helper/command.c
parentf18a801e03e50274676544d10029c05e1f219246 (diff)
downloadriscv-openocd-89c6b93ba278e26113aadcc3b7d357e05264beba.zip
riscv-openocd-89c6b93ba278e26113aadcc3b7d357e05264beba.tar.gz
riscv-openocd-89c6b93ba278e26113aadcc3b7d357e05264beba.tar.bz2
helper/command: fix memory leak on malloc() fail
If malloc() fails, the just allocated Jim_Obj will leaks. Move Jim_IncrRefCount() before the malloc() and deallocate the Jim object with Jim_DecrRefCount() on malloc() fail. While there, add the 'out of memory' log and fix the CamelCase name of the symbol tclOutput. Change-Id: Ic733db229d5aa5d477d758ea9cb88cd81d7542cd Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com> Reviewed-on: http://openocd.zylin.com/6188 Tested-by: jenkins Reviewed-by: Tomas Vanek <vanekt@fbl.cz>
Diffstat (limited to 'src/helper/command.c')
-rw-r--r--src/helper/command.c14
1 files changed, 9 insertions, 5 deletions
diff --git a/src/helper/command.c b/src/helper/command.c
index e703be4..08d14b4 100644
--- a/src/helper/command.c
+++ b/src/helper/command.c
@@ -83,17 +83,21 @@ static struct log_capture_state *command_log_capture_start(Jim_Interp *interp)
{
/* capture log output and return it. A garbage collect can
* happen, so we need a reference count to this object */
- Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
- if (NULL == tclOutput)
+ Jim_Obj *jim_output = Jim_NewStringObj(interp, "", 0);
+ if (!jim_output)
return NULL;
+ Jim_IncrRefCount(jim_output);
+
struct log_capture_state *state = malloc(sizeof(*state));
- if (NULL == state)
+ if (!state) {
+ LOG_ERROR("Out of memory");
+ Jim_DecrRefCount(interp, jim_output);
return NULL;
+ }
state->interp = interp;
- Jim_IncrRefCount(tclOutput);
- state->output = tclOutput;
+ state->output = jim_output;
log_add_callback(tcl_output, state);