base: NULL-terminate getarg_strings

This commit is contained in:
Nicolas Williams
2025-12-02 17:17:50 -06:00
parent a7bba71ab8
commit 3451950db7
+11 -1
View File
@@ -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;
}