Make the build reproducible.
[lopsub.git] / lopsub.c
index 2ba2621..d495663 100644 (file)
--- a/lopsub.c
+++ b/lopsub.c
@@ -651,7 +651,7 @@ static int lls_parse_arg(struct lls_arg *la, const struct lls_option *opts,
        }
        switch (opt->arg_type) {
        case LLS_STRING:
-               if (lor->given > 0 && !multiple)
+               if (!opt->values && lor->given > 0 && !multiple)
                        free(lor->value[idx].string_val);
                if (opt->values) {
                        ret = check_enum_arg(la->arg, opt, errctx);
@@ -724,9 +724,11 @@ int lls_check_arg_count(const struct lls_parse_result *lpr,
 {
        if (errctx)
                *errctx = NULL;
+       assert(min_argc <= max_argc);
        if (lpr->num_inputs < min_argc) {
-               xasprintf(errctx, "at least %u non-option args required, "
-                       "%u given", min_argc, lpr->num_inputs);
+               xasprintf(errctx, "%s %u non-option args required, %u given",
+                       min_argc < max_argc? "at least" : "exactly",
+                       min_argc, lpr->num_inputs);
                return -E_LLS_BAD_ARG_COUNT;
        }
        if (lpr->num_inputs > max_argc) {
@@ -734,8 +736,10 @@ int lls_check_arg_count(const struct lls_parse_result *lpr,
                        xasprintf(errctx, "no non-option args allowed, "
                                "%u given", lpr->num_inputs);
                else
-                       xasprintf(errctx, "at most %u non-option args allowed, "
-                               "%u given", max_argc, lpr->num_inputs);
+                       xasprintf(errctx, "%s %u non-option args allowed, "
+                               "%u given", min_argc < max_argc?
+                               "at most" : "exactly",
+                               max_argc, lpr->num_inputs);
                return -E_LLS_BAD_ARG_COUNT;
        }
        return 1;
@@ -1296,6 +1300,7 @@ int lls_deserialize_parse_result(const char *buf, const struct lls_command *cmd,
                                if (!lor->value[j].string_val) {
                                        for (; j >= 0; j--)
                                                free(lor->value[j].string_val);
+                                       free(lor->value);
                                        goto free_options;
                                }
                                p += strlen(lor->value[j].string_val) + 1;
@@ -1313,7 +1318,7 @@ int lls_deserialize_parse_result(const char *buf, const struct lls_command *cmd,
        *lprp = lpr;
        return 1;
 free_options:
-       for (; i >= 0; i--) {
+       for (i--; i >= 0; i--) {
                const struct lls_option *opt = cmd->options + i;
                struct lls_opt_result *lor = lpr->opt_result + i;
                unsigned num_vals = (opt->flags & LLS_MULTIPLE)? lor->given : 1;