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: <https://gitlab.freedesktop.org/libinput/libei/-/merge_requests/388>
This commit is contained in:
Peter Hutterer 2026-04-17 15:41:47 +10:00 committed by Marge Bot
parent e2197b8389
commit bbcc808b1a
3 changed files with 7 additions and 15 deletions

View file

@ -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;

View file

@ -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;

View file

@ -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) {