]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
strv: rewrite strv_copy() with cleanup attribute and STRV_FOREACH()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 16 Mar 2022 13:29:32 +0000 (22:29 +0900)
committerLennart Poettering <lennart@poettering.net>
Fri, 18 Mar 2022 17:36:48 +0000 (18:36 +0100)
src/basic/strv.c

index cf573a378383590ba6d864e5b9ade265e3534757..07a6c49b50dfcf76d5098d355c5b4f271108eb8e 100644 (file)
@@ -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) {