aboutsummaryrefslogtreecommitdiff
path: root/jim.c
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2021-03-19 08:28:39 +1000
committerSteve Bennett <steveb@workware.net.au>2021-03-19 08:32:22 +1000
commite4416cf86f0b05c0396895fb38f7c77854b5fe46 (patch)
tree41ed0899c9e4e390b7713f5b80d4347308de8761 /jim.c
parent196a48eda0ae94c5c3e102299fa19190946d96a9 (diff)
downloadjimtcl-e4416cf86f0b05c0396895fb38f7c77854b5fe46.zip
jimtcl-e4416cf86f0b05c0396895fb38f7c77854b5fe46.tar.gz
jimtcl-e4416cf86f0b05c0396895fb38f7c77854b5fe46.tar.bz2
core: Fix memory leak replacing existing commands
Fixes #198 Reported-by: Antonio Borneo <borneo.antonio@gmail.com> Signed-off-by: Steve Bennett <steveb@workware.net.au>
Diffstat (limited to 'jim.c')
-rw-r--r--jim.c14
1 files changed, 11 insertions, 3 deletions
diff --git a/jim.c b/jim.c
index f749500..64f47bd 100644
--- a/jim.c
+++ b/jim.c
@@ -4006,8 +4006,14 @@ static Jim_Obj *JimQualifyName(Jim_Interp *interp, Jim_Obj *objPtr)
/**
* Add the command to the commands hash table
*/
-static int JimCreateCommand(Jim_Interp *interp, Jim_Obj *nameObjPtr, Jim_Cmd *cmd)
+static void JimCreateCommand(Jim_Interp *interp, Jim_Obj *nameObjPtr, Jim_Cmd *cmd)
{
+ /* If the entry already exists, nameObjPtr will not be used,
+ * so the refCount of nameObjPtr can't be zero, relying on this function to
+ * release it in that case.
+ */
+ JimPanic((nameObjPtr->refCount == 0, "JimCreateCommand called with zero ref count name"));
+
/* It may already exist, so we try to delete the old one.
* Note that reference count means that it won't be deleted yet if
* it exists in the call stack.
@@ -4023,7 +4029,7 @@ static int JimCreateCommand(Jim_Interp *interp, Jim_Obj *nameObjPtr, Jim_Cmd *cm
Jim_SetHashVal(&interp->commands, he, cmd);
/* Need to increment the proc epoch here so that the new command will be used */
Jim_InterpIncrProcEpoch(interp);
- return JIM_OK;
+ return;
}
}
@@ -4033,7 +4039,7 @@ static int JimCreateCommand(Jim_Interp *interp, Jim_Obj *nameObjPtr, Jim_Cmd *cm
* existing command that is replace will be held as a negative cache entry
* until the next time the proc epoch is incremented.
*/
- return Jim_ReplaceHashEntry(&interp->commands, nameObjPtr, cmd);
+ Jim_ReplaceHashEntry(&interp->commands, nameObjPtr, cmd);
}
int Jim_CreateCommandObj(Jim_Interp *interp, Jim_Obj *cmdNameObj,
@@ -4048,7 +4054,9 @@ int Jim_CreateCommandObj(Jim_Interp *interp, Jim_Obj *cmdNameObj,
cmdPtr->u.native.cmdProc = cmdProc;
cmdPtr->u.native.privData = privData;
+ Jim_IncrRefCount(cmdNameObj);
JimCreateCommand(interp, cmdNameObj, cmdPtr);
+ Jim_DecrRefCount(interp, cmdNameObj);
return JIM_OK;
}