summaryrefslogtreecommitdiff
path: root/parse-options-cb.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2016-07-05 16:44:47 -0400
committerJunio C Hamano <gitster@pobox.com>2016-07-06 10:11:08 -0700
commit023ff39b2994804e4a7b2274b22336bdb37d4a54 (patch)
tree961ed842c71e0b8ba81b00b1212f613fd759c781 /parse-options-cb.c
parent0b65a8dbdb38962e700ee16776a3042beb489060 (diff)
downloadgit-023ff39b2994804e4a7b2274b22336bdb37d4a54.tar.gz
parse_options: allocate a new array when concatenatingjk/parse-options-concat
In exactly one callers (builtin/revert.c), we build up the options list dynamically from multiple arrays. We do so by manually inserting "filler" entries into one array, and then copying the other array into the allocated space. This is tedious and error-prone, as you have to adjust the filler any time the second array is modified (although we do at least check and die() when the counts do not match up). Instead, let's just allocate a new array. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'parse-options-cb.c')
-rw-r--r--parse-options-cb.c29
1 files changed, 17 insertions, 12 deletions
diff --git a/parse-options-cb.c b/parse-options-cb.c
index 239898d946..2d875202cd 100644
--- a/parse-options-cb.c
+++ b/parse-options-cb.c
@@ -117,19 +117,24 @@ int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
return 0;
}
-int parse_options_concat(struct option *dst, size_t dst_size, struct option *src)
+struct option *parse_options_concat(struct option *a, struct option *b)
{
- int i, j;
-
- for (i = 0; i < dst_size; i++)
- if (dst[i].type == OPTION_END)
- break;
- for (j = 0; i < dst_size; i++, j++) {
- dst[i] = src[j];
- if (src[j].type == OPTION_END)
- return 0;
- }
- return -1;
+ struct option *ret;
+ size_t i, a_len = 0, b_len = 0;
+
+ for (i = 0; a[i].type != OPTION_END; i++)
+ a_len++;
+ for (i = 0; b[i].type != OPTION_END; i++)
+ b_len++;
+
+ ALLOC_ARRAY(ret, st_add3(a_len, b_len, 1));
+ for (i = 0; i < a_len; i++)
+ ret[i] = a[i];
+ for (i = 0; i < b_len; i++)
+ ret[a_len + i] = b[i];
+ ret[a_len + b_len] = b[b_len]; /* final OPTION_END */
+
+ return ret;
}
int parse_opt_string_list(const struct option *opt, const char *arg, int unset)