diff --git a/lib/roken/getarg.c b/lib/roken/getarg.c index c38accaa9..63d880a9d 100644 --- a/lib/roken/getarg.c +++ b/lib/roken/getarg.c @@ -334,7 +334,16 @@ add_string(getarg_strings *s, char *value) { char **strings; - strings = realloc(s->strings, (s->num_strings + 1) * sizeof(*s->strings)); + /* + * O(N^2) here. Fortunately counts will generally be low here. + * + * Either break the ABI by adding an allocated count field to + * `getarg_strings`, or use a specific set of counts like 1, 4, 8, 12, 18, + * etc., i.e., powers of two for a while then switch to increasing by half. + * After some unrealistic number of string arguments switch to incrementing + * by 1. + */ + strings = realloc(s->strings, (s->num_strings + 2) * sizeof(*s->strings)); if (strings == NULL) { free(s->strings); s->strings = NULL; @@ -343,6 +352,7 @@ add_string(getarg_strings *s, char *value) } s->strings = strings; s->strings[s->num_strings] = value; + s->strings[s->num_strings + 1] = NULL; /* NULL-terminator is handy */ s->num_strings++; return 0; }