Satisfy clang's aggressive strlcpy warnings
[openafs.git] / src / cmd / cmd.c
index 903dc94..1bbdc8e 100644 (file)
@@ -1018,7 +1018,7 @@ cmd_Parse(int argc, char **argv, struct cmd_syndesc **outsyntax)
        /* Display full help syntax if we don't have subcommands */
        if (noOpcodes)
            PrintFlagHelp(ts);
-       code = CMD_USAGE;
+       code = CMD_HELP;
        goto out;
     }
 
@@ -1061,8 +1061,12 @@ cmd_Dispatch(int argc, char **argv)
     int code;
 
     code = cmd_Parse(argc, argv, &ts);
-    if (code)
+    if (code) {
+       if (code == CMD_HELP) {
+           code = 0; /* displaying help is not an error */
+       }
        return code;
+    }
 
     /*
      * Before calling the beforeProc and afterProc and all the implications
@@ -1358,9 +1362,6 @@ cmd_OptionAsString(struct cmd_syndesc *syn, int pos, char **value)
     const char *str;
     int code;
 
-    if (*value)
-       free(*value);
-
     code = _get_config_string(syn, pos, &str);
     if (code)
        return code;
@@ -1368,6 +1369,8 @@ cmd_OptionAsString(struct cmd_syndesc *syn, int pos, char **value)
     if (str == NULL)
        return CMD_MISSING;
 
+    if (*value)
+       free(*value);
     *value = strdup(str);
 
     return 0;
@@ -1379,6 +1382,7 @@ cmd_OptionAsList(struct cmd_syndesc *syn, int pos, struct cmd_item **value)
     const char *str;
     struct cmd_item *item, **last;
     const char *start, *end;
+    size_t len;
     int code;
 
     if (pos > syn->nParms)
@@ -1403,8 +1407,9 @@ cmd_OptionAsList(struct cmd_syndesc *syn, int pos, struct cmd_item **value)
     start = str;
     while ((end = strchr(start, ' '))) {
        item = calloc(1, sizeof(struct cmd_item));
-       item->data = malloc(end - start + 1);
-       strlcpy(item->data, start, end - start + 1);
+       len = end - start + 1;
+       item->data = malloc(len);
+       strlcpy(item->data, start, len);
        *last = item;
        last = &item->next;
        for (start = end; *start == ' '; start++); /* skip any whitespace */
@@ -1413,8 +1418,9 @@ cmd_OptionAsList(struct cmd_syndesc *syn, int pos, struct cmd_item **value)
     /* Catch the final element */
     if (*start != '\0') {
        item = calloc(1, sizeof(struct cmd_item));
-       item->data = malloc(strlen(start) + 1);
-       strlcpy(item->data, start, strlen(start) + 1);
+       len = strlen(start) + 1;
+       item->data = malloc(len);
+       strlcpy(item->data, start, len);
        *last = item;
     }