]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
strbuf: Re-use buf_realloc()
authorLucas De Marchi <lucas.de.marchi@gmail.com>
Tue, 12 Nov 2024 15:07:05 +0000 (09:07 -0600)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Sun, 17 Nov 2024 21:35:13 +0000 (15:35 -0600)
Let the realloc happen in only one place, so we can change its behavior
in future.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/239
shared/strbuf.c

index 290d1db37d59bc8bcc46586e53c46d19482f889a..e193d21908568215b5a090c15c8966583820e110 100644 (file)
@@ -55,12 +55,14 @@ char *strbuf_steal(struct strbuf *buf)
 {
        char *bytes;
 
-       bytes = realloc(buf->bytes, buf->used + 1);
-       if (!bytes) {
+       if (!buf_realloc(buf, buf->used + 1)) {
                free(buf->bytes);
                return NULL;
        }
+
+       bytes = buf->bytes;
        bytes[buf->used] = '\0';
+
        return bytes;
 }