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>
This commit is contained in:
parent
f18a801e03
commit
89c6b93ba2
|
@ -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
|
/* capture log output and return it. A garbage collect can
|
||||||
* happen, so we need a reference count to this object */
|
* happen, so we need a reference count to this object */
|
||||||
Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
|
Jim_Obj *jim_output = Jim_NewStringObj(interp, "", 0);
|
||||||
if (NULL == tclOutput)
|
if (!jim_output)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
Jim_IncrRefCount(jim_output);
|
||||||
|
|
||||||
struct log_capture_state *state = malloc(sizeof(*state));
|
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;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
state->interp = interp;
|
state->interp = interp;
|
||||||
Jim_IncrRefCount(tclOutput);
|
state->output = jim_output;
|
||||||
state->output = tclOutput;
|
|
||||||
|
|
||||||
log_add_callback(tcl_output, state);
|
log_add_callback(tcl_output, state);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue