From bbcc808b1a4064fcd3e4a41eb08fb3e7e621bbca Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Fri, 17 Apr 2026 15:41:47 +1000 Subject: [PATCH] util: use x{re}alloc, not the libc ones Might as well blow up when allocation fails, not that this ever happens. And in the case of cmdline_as_str() this now also means we definitely have to free the result (we did before but well, there was the theoretical case of it failling). Assisted-by: Claude:claude-opus-4-6 Part-of: --- src/util-io.c | 10 +++------- src/util-strings.c | 8 ++------ src/util-strings.h | 4 ++-- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/util-io.c b/src/util-io.c index 7b39b6c..f3f7af8 100644 --- a/src/util-io.c +++ b/src/util-io.c @@ -126,11 +126,8 @@ struct iobuf { struct iobuf * iobuf_new(size_t size) { - struct iobuf *buf = malloc(sizeof(*buf)); - uint8_t *data = malloc(size); - - assert(buf); - assert(data); + struct iobuf *buf = xalloc(sizeof(*buf)); + uint8_t *data = xalloc(size); *buf = (struct iobuf){ .sz = size, @@ -212,8 +209,7 @@ iobuf_take_fd(struct iobuf *buf) static inline void iobuf_resize(struct iobuf *buf, size_t to_size) { - uint8_t *newdata = realloc(buf->data, to_size); - assert(newdata); + uint8_t *newdata = xrealloc(buf->data, to_size); buf->data = newdata; buf->sz = to_size; diff --git a/src/util-strings.c b/src/util-strings.c index 656426d..71a3387 100644 --- a/src/util-strings.c +++ b/src/util-strings.c @@ -214,9 +214,7 @@ strreplace(const char *string, const char *separator, const char *replacement) destptr += len; } - void *tmp = realloc(r, (destptr - r) + 1); - assert(tmp); - return tmp; + return xrealloc(r, (destptr - r) + 1); } size_t @@ -240,9 +238,7 @@ strv_append_take(char **strv, char **str) size_t len = strv_len(strv) + 1; len = max(len, 2); - char **s = realloc(strv, len * sizeof(*strv)); - if (!s) - abort(); + char **s = xrealloc(strv, len * sizeof(*strv)); s[len - 1] = NULL; s[len - 2] = *str; *str = NULL; diff --git a/src/util-strings.h b/src/util-strings.h index 74b7f38..f82874d 100644 --- a/src/util-strings.h +++ b/src/util-strings.h @@ -441,11 +441,11 @@ cmdline_as_str(void) if (sysctl(mib, ARRAY_LENGTH(mib), NULL, &len, NULL, 0)) return NULL; - char *const procargs = malloc(len); + _cleanup_free_ char *procargs = xalloc(len); if (sysctl(mib, ARRAY_LENGTH(mib), procargs, &len, NULL, 0)) return NULL; - return procargs; + return steal(&procargs); #else int fd = open("/proc/self/cmdline", O_RDONLY); if (fd != -1) {