From: Lennart Poettering Date: Thu, 17 Jan 2013 23:47:19 +0000 (+0100) Subject: strv: make strv_extend() smarter X-Git-Tag: v198~490 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=82dde599ed2b8aa4877900d84e7a2ddc31ef8da2 strv: make strv_extend() smarter --- diff --git a/src/shared/strv.c b/src/shared/strv.c index aed45d261..2d556f4a0 100644 --- a/src/shared/strv.c +++ b/src/shared/strv.c @@ -372,15 +372,26 @@ fail: int strv_extend(char ***l, const char *value) { char **c; + char *v; + unsigned n; if (!value) return 0; - c = strv_append(*l, value); - if (!c) + v = strdup(value); + if (!v) return -ENOMEM; - strv_free(*l); + n = strv_length(*l); + c = realloc(*l, sizeof(char*) * (n + 2)); + if (!c) { + free(v); + return -ENOMEM; + } + + c[n] = v; + c[n+1] = NULL; + *l = c; return 0; }