diff options
author | Zachary T Welch <zw@superlucidity.net> | 2009-11-28 10:42:51 -0800 |
---|---|---|
committer | Zachary T Welch <zw@superlucidity.net> | 2009-11-28 13:00:39 -0800 |
commit | 8795b8f9df5ba1fd8466a04b515aa5f56c0c4015 (patch) | |
tree | 3266949da5d71d417b81ac2ae8a6219f342153eb /src/helper | |
parent | 42e00bb379fe7591b6d74768a45855ed5cd0c24f (diff) | |
download | openocd_libswd-8795b8f9df5ba1fd8466a04b515aa5f56c0c4015.tar.gz openocd_libswd-8795b8f9df5ba1fd8466a04b515aa5f56c0c4015.tar.bz2 openocd_libswd-8795b8f9df5ba1fd8466a04b515aa5f56c0c4015.tar.xz openocd_libswd-8795b8f9df5ba1fd8466a04b515aa5f56c0c4015.zip |
add error checking in command_new
Adds checks for memory allocation failures. Started to use calloc()
instead of malloc()/memset(), but I got carried away. This kind of work
should be done throughout the tree, but it's almost hopeless at present.
Diffstat (limited to 'src/helper')
-rw-r--r-- | src/helper/command.c | 50 |
1 files changed, 30 insertions, 20 deletions
diff --git a/src/helper/command.c b/src/helper/command.c index 4b7d8cb9..ce857dd6 100644 --- a/src/helper/command.c +++ b/src/helper/command.c @@ -253,19 +253,44 @@ static struct command **command_list_for_parent( return parent ? &parent->children : &cmd_ctx->commands; } +static void command_free(struct command *c) +{ + /// @todo if command has a handler, unregister its jim command! + + while (NULL != c->children) + { + struct command *tmp = c->children; + c->children = tmp->next; + command_free(tmp); + } + + if (c->name) + free(c->name); + if (c->help) + free((void*)c->help); + if (c->usage) + free((void*)c->usage); + free(c); +} + static struct command *command_new(struct command_context *cmd_ctx, struct command *parent, const struct command_registration *cr) { assert(cr->name); - struct command *c = malloc(sizeof(struct command)); - memset(c, 0, sizeof(struct command)); + struct command *c = calloc(1, sizeof(struct command)); + if (NULL == c) + return NULL; c->name = strdup(cr->name); if (cr->help) c->help = strdup(cr->help); if (cr->usage) c->usage = strdup(cr->usage); + + if (!c->name || (cr->help && !c->help) || (cr->usage && !c->usage)) + goto command_new_error; + c->parent = parent; c->handler = cr->handler; c->jim_handler = cr->jim_handler; @@ -275,25 +300,10 @@ static struct command *command_new(struct command_context *cmd_ctx, command_add_child(command_list_for_parent(cmd_ctx, parent), c); return c; -} -static void command_free(struct command *c) -{ - /// @todo if command has a handler, unregister its jim command! - while (NULL != c->children) - { - struct command *tmp = c->children; - c->children = tmp->next; - command_free(tmp); - } - - if (c->name) - free(c->name); - if (c->help) - free((void*)c->help); - if (c->usage) - free((void*)c->usage); - free(c); +command_new_error: + command_free(c); + return NULL; } static int command_unknown(Jim_Interp *interp, int argc, Jim_Obj *const *argv); |