From: Yu Watanabe Date: Wed, 16 Mar 2022 13:29:32 +0000 (+0900) Subject: strv: rewrite strv_copy() with cleanup attribute and STRV_FOREACH() X-Git-Tag: v251-rc1~98 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9eb814818d0c35f0c9e05cb596508c1536b0e654;p=thirdparty%2Fsystemd.git strv: rewrite strv_copy() with cleanup attribute and STRV_FOREACH() --- diff --git a/src/basic/strv.c b/src/basic/strv.c index cf573a37838..07a6c49b50d 100644 --- a/src/basic/strv.c +++ b/src/basic/strv.c @@ -89,23 +89,23 @@ char** strv_free_erase(char **l) { } char** strv_copy(char * const *l) { - char **r, **k; + _cleanup_strv_free_ char **result = NULL; + char **k, * const *i; - k = r = new(char*, strv_length(l) + 1); - if (!r) + result = new(char*, strv_length(l) + 1); + if (!result) return NULL; - if (l) - for (; *l; k++, l++) { - *k = strdup(*l); - if (!*k) { - strv_free(r); - return NULL; - } - } + k = result; + STRV_FOREACH(i, l) { + *k = strdup(*i); + if (!*k) + return NULL; + k++; + } *k = NULL; - return r; + return TAKE_PTR(result); } size_t strv_length(char * const *l) {