- fix illegal memory access in unregister_command function

git-svn-id: svn://svn.berlios.de/openocd/trunk@1224 b42882b7-edfa-0310-969c-e2dbd0fdcd60
This commit is contained in:
ntfreak 2008-12-10 17:47:16 +00:00
parent 40042df09d
commit 00b3eb5fed
1 changed files with 19 additions and 11 deletions

View File

@ -289,8 +289,10 @@ int unregister_command(command_context_t *context, char *name)
return ERROR_INVALID_ARGUMENTS; return ERROR_INVALID_ARGUMENTS;
/* find command */ /* find command */
for (c = context->commands; c; c = c->next) c = context->commands;
{
while(NULL != c)
{
if (strcmp(name, c->name) == 0) if (strcmp(name, c->name) == 0)
{ {
/* unlink command */ /* unlink command */
@ -300,26 +302,32 @@ int unregister_command(command_context_t *context, char *name)
} }
else else
{ {
/* first element in command list */
context->commands = c->next; context->commands = c->next;
} }
/* unregister children */ /* unregister children */
if (c->children) while(NULL != c->children)
{ {
for (c2 = c->children; c2; c2 = c2->next) c2 = c->children;
{ c->children = c->children->next;
free(c2->name); free(c2->name);
free(c2); c2->name = NULL;
} free(c2);
c2 = NULL;
} }
/* delete command */ /* delete command */
free(c->name); free(c->name);
c->name = NULL;
free(c); free(c);
c = NULL;
return ERROR_OK;
} }
/* remember the last command for unlinking */ /* remember the last command for unlinking */
p = c; p = c;
c = c->next;
} }
return ERROR_OK; return ERROR_OK;