]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
strv: modernize strv_insert()
authorLennart Poettering <lennart@poettering.net>
Tue, 16 Jan 2024 18:15:34 +0000 (19:15 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 17 Jan 2024 10:32:11 +0000 (11:32 +0100)
Let's use memmove() to move the string contents, rather than manual
loops.

Fix the overflow extension.

Prefer reallocarray() over malloc()

src/basic/strv.c

index fa17631aa0e575ec2562b464ec53d5e12766973a..27a70a84158a5003ecbe632ef4a593b7aba549dd 100644 (file)
@@ -505,29 +505,31 @@ int strv_insert(char ***l, size_t position, char *value) {
         char **c;
         size_t n, m;
 
+        assert(l);
+
         if (!value)
                 return 0;
 
         n = strv_length(*l);
         position = MIN(position, n);
 
-        /* increase and check for overflow */
-        m = n + 2;
-        if (m < n)
+        /* check for overflow and increase*/
+        if (n > SIZE_MAX - 2)
                 return -ENOMEM;
+        m = n + 2;
 
-        c = new(char*, m);
+        c = reallocarray(*l, GREEDY_ALLOC_ROUND_UP(m), sizeof(char*));
         if (!c)
                 return -ENOMEM;
 
-        for (size_t i = 0; i < position; i++)
-                c[i] = (*l)[i];
+        if (n > position)
+                memmove(c + position + 1, c + position, (n - position) * sizeof(char*));
+
         c[position] = value;
-        for (size_t i = position; i < n; i++)
-                c[i+1] = (*l)[i];
-        c[n+1] = NULL;
+        c[n + 1] = NULL;
 
-        return free_and_replace(*l, c);
+        *l = c;
+        return 0;
 }
 
 int strv_consume_with_size(char ***l, size_t *n, char *value) {