summaryrefslogtreecommitdiff
path: root/parse-options-cb.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2016-08-03 15:10:25 -0700
committerJunio C Hamano <gitster@pobox.com>2016-08-03 15:10:25 -0700
commitd083d420b7d24a57cfd32af71100ae4c887f3a39 (patch)
tree99cd4d8f9c8408dfdab7739fb467fab3d5559ff0 /parse-options-cb.c
parentcf27c7996e986395a05c0056684923195295fd14 (diff)
parent023ff39b2994804e4a7b2274b22336bdb37d4a54 (diff)
downloadgit-d083d420b7d24a57cfd32af71100ae4c887f3a39.tar.gz
Merge branch 'jk/parse-options-concat'
Users of the parse_options_concat() API function need to allocate extra slots in advance and fill them with OPT_END() when they want to decide the set of supported options dynamically, which makes the code error-prone and hard to read. This has been corrected by tweaking the API to allocate and return a new copy of "struct option" array. * jk/parse-options-concat: parse_options: allocate a new array when concatenating
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 ba5acf3111..9667bc75a0 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)