add error checking in command_new
authorZachary T Welch <zw@superlucidity.net>
Sat, 28 Nov 2009 18:42:51 +0000 (10:42 -0800)
committerZachary T Welch <zw@superlucidity.net>
Sat, 28 Nov 2009 21:00:39 +0000 (13:00 -0800)
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.

src/helper/command.c

index 4b7d8cb9b4d86f282806a0ab9341eab0f19c7c3a..ce857dd6c21c32152f79687dc544bc0d54674228 100644 (file)
@@ -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);

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)