Skip to content

Commit 36ae487

Browse files
committed
jimtcl: add temporary workaround for memory leak in jimtcl 0.80
The API Jim_CreateCommand() in latest version of jimtcl leaks the memory allocated internally by jimtcl when it converts the string command-name to a Jim_Obj. The fix is already merged upstream and would be available in next jimtcl 0.81, expected in ~6 months, hopefully before the next tag for OpenOCD v0.12.0. OpenOCD v0.11.0 is distributed with jimtcl 0.79. Debian distributes jimtcl as a separate library package and today it's still on 0.79. It make sense to keep using jimtcl 0.80 in current development cycle to test it further. But having this background memory leak noise hides the eventual new memory leaks that could come from the development activity. This patch uses the internal jimtcl API Jim_CreateCommandObj() and correctly free the internal object, avoiding the memory leak. Being an internal API, it is not accessible if OpenOCD is linked with an external jimtcl library. Nevertheless, building jimtcl as a submodule of OpenOCD makes the trick effective. The scope of this patch is thus limited at developers that build OpenOCD with jimtcl submodule and need to control and debug memory leaks. This patch is supposed to be removed as soon as jimtcl 0.81 gets available. The added code is located, on purpose, in an area of the file that hopefully will not conflict other patches pending in gerrit. Change-Id: I4d300ad21bdb6c616c3f0f14b429b4fdf360900d Signed-off-by: Antonio Borneo <[email protected]> Reported-by: Tarek BOCHKATI <[email protected]> Reviewed-on: http://openocd.zylin.com/6130 Tested-by: jenkins Reviewed-by: Tomas Vanek <[email protected]> Reviewed-by: Oleksij Rempel <[email protected]> Reviewed-by: Jonathan McDowell <[email protected]>
1 parent a5b5907 commit 36ae487

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/helper/command.c

+34
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,40 @@ static void command_log_capture_finish(struct log_capture_state *state)
117117
free(state);
118118
}
119119

120+
/*
121+
* FIXME: workaround for memory leak in jimtcl 0.80
122+
* Jim API Jim_CreateCommand() converts the command name in a Jim object and
123+
* does not free the object. Fixed for jimtcl 0.81 by e4416cf86f0b
124+
* Use the internal jimtcl API Jim_CreateCommandObj, not exported by jim.h,
125+
* and override the bugged API through preprocessor's macro.
126+
* This workaround works only when jimtcl is compiled as OpenOCD submodule.
127+
* If jimtcl is linked-in from a precompiled library, either static or dynamic,
128+
* the symbol Jim_CreateCommandObj is not exported and the build will use the
129+
* bugged API.
130+
* To be removed when OpenOCD will switch to jimtcl 0.81
131+
*/
132+
#if JIM_VERSION == 80
133+
static int workaround_createcommand(Jim_Interp *interp, const char *cmdName,
134+
Jim_CmdProc *cmdProc, void *privData, Jim_DelCmdProc *delProc);
135+
int Jim_CreateCommandObj(Jim_Interp *interp, Jim_Obj *cmdNameObj,
136+
Jim_CmdProc *cmdProc, void *privData, Jim_DelCmdProc *delProc)
137+
__attribute__((weak, alias("workaround_createcommand")));
138+
static int workaround_createcommand(Jim_Interp *interp, const char *cmdName,
139+
Jim_CmdProc *cmdProc, void *privData, Jim_DelCmdProc *delProc)
140+
{
141+
if ((void *)Jim_CreateCommandObj == (void *)workaround_createcommand)
142+
return Jim_CreateCommand(interp, cmdName, cmdProc, privData, delProc);
143+
144+
Jim_Obj *cmd_name = Jim_NewStringObj(interp, cmdName, -1);
145+
Jim_IncrRefCount(cmd_name);
146+
int retval = Jim_CreateCommandObj(interp, cmd_name, cmdProc, privData, delProc);
147+
Jim_DecrRefCount(interp, cmd_name);
148+
return retval;
149+
}
150+
#define Jim_CreateCommand workaround_createcommand
151+
#endif /* JIM_VERSION == 80 */
152+
/* FIXME: end of workaround for memory leak in jimtcl 0.80 */
153+
120154
static int command_retval_set(Jim_Interp *interp, int retval)
121155
{
122156
int *return_retval = Jim_GetAssocData(interp, "retval");

0 commit comments

Comments
 (0)