about summary refs log tree commit diff
diff options
context:
space:
mode:
authorsternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org>2020-08-24 21:24:22 +0200
committersternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org>2020-08-24 21:24:22 +0200
commit2f9406fc55907e15abbe80d79e84c8e11ef07b60 (patch)
tree93911946606879e6b050eb687c87fcc81c13fc95
parentdf05c1648557fbff87653e632556b10eff171e90 (diff)
fix(stringutil): skip strings that are NULL
Prevents segfaults with careless uses
-rw-r--r--stringutil.c22
1 files changed, 12 insertions, 10 deletions
diff --git a/stringutil.c b/stringutil.c
index baef713..44cec9e 100644
--- a/stringutil.c
+++ b/stringutil.c
@@ -37,19 +37,21 @@ char *catn_alloc(size_t n, ...) {
 
     for(size_t i = 0; i < n; i++) {
         char *str = va_arg(args, char *);
-        size_t copy_len = strlen(str) + (i + 1 == n ? 1 : 0);
+        if(str != NULL) {
+            size_t copy_len = strlen(str) + (i + 1 == n ? 1 : 0);
 
-        char *tmp = realloc(buffer, buffer_size + copy_len);
+            char *tmp = realloc(buffer, buffer_size + copy_len);
 
-        if(tmp == NULL) {
-            break;
-        } else {
-            buffer = tmp;
-        }
+            if(tmp == NULL) {
+                break;
+            } else {
+                buffer = tmp;
+            }
 
-        buffer_size += copy_len;
-        memcpy(buffer + pos, str, copy_len);
-        pos += copy_len;
+            buffer_size += copy_len;
+            memcpy(buffer + pos, str, copy_len);
+            pos += copy_len;
+        }
     }
 
     if(buffer != NULL) {