Add missing calloc check and use fatal. Prompted by a similar change

from Yayo Razo.
This commit is contained in:
nicm
2026-07-12 20:16:20 +00:00
committed by tmux update bot
parent cc8e0f2f67
commit 3612a9a605

View File

@@ -1,4 +1,4 @@
/* $OpenBSD: xmalloc.c,v 1.14 2026/06/18 10:56:22 nicm Exp $ */ /* $OpenBSD: xmalloc.c,v 1.15 2026/07/12 20:16:20 nicm Exp $ */
/* /*
* Author: Tatu Ylonen <ylo@cs.hut.fi> * Author: Tatu Ylonen <ylo@cs.hut.fi>
@@ -32,8 +32,7 @@ xmalloc(size_t size)
fatalx("xmalloc: zero size"); fatalx("xmalloc: zero size");
ptr = malloc(size); ptr = malloc(size);
if (ptr == NULL) if (ptr == NULL)
fatalx("xmalloc: allocating %zu bytes: %s", fatal("xmalloc: allocating %zu bytes", size);
size, strerror(errno));
return ptr; return ptr;
} }
@@ -44,10 +43,11 @@ xcalloc(size_t nmemb, size_t size)
if (size == 0 || nmemb == 0) if (size == 0 || nmemb == 0)
fatalx("xcalloc: zero size"); fatalx("xcalloc: zero size");
if (SIZE_MAX / nmemb < size)
fatalx("xcalloc: nmemb * size > SIZE_MAX");
ptr = calloc(nmemb, size); ptr = calloc(nmemb, size);
if (ptr == NULL) if (ptr == NULL)
fatalx("xcalloc: allocating %zu * %zu bytes: %s", fatal("xcalloc: allocating %zu bytes", size * nmemb);
nmemb, size, strerror(errno));
return ptr; return ptr;
} }
@@ -66,8 +66,7 @@ xreallocarray(void *ptr, size_t nmemb, size_t size)
fatalx("xreallocarray: zero size"); fatalx("xreallocarray: zero size");
new_ptr = reallocarray(ptr, nmemb, size); new_ptr = reallocarray(ptr, nmemb, size);
if (new_ptr == NULL) if (new_ptr == NULL)
fatalx("xreallocarray: allocating %zu * %zu bytes: %s", fatal("xreallocarray: allocating %zu bytes", size * nmemb);
nmemb, size, strerror(errno));
return new_ptr; return new_ptr;
} }
@@ -80,8 +79,7 @@ xrecallocarray(void *ptr, size_t oldnmemb, size_t nmemb, size_t size)
fatalx("xrecallocarray: zero size"); fatalx("xrecallocarray: zero size");
new_ptr = recallocarray(ptr, oldnmemb, nmemb, size); new_ptr = recallocarray(ptr, oldnmemb, nmemb, size);
if (new_ptr == NULL) if (new_ptr == NULL)
fatalx("xrecallocarray: allocating %zu * %zu bytes: %s", fatal("xrecallocarray: allocating %zu bytes", size * nmemb);
nmemb, size, strerror(errno));
return new_ptr; return new_ptr;
} }
@@ -91,7 +89,7 @@ xstrdup(const char *str)
char *cp; char *cp;
if ((cp = strdup(str)) == NULL) if ((cp = strdup(str)) == NULL)
fatalx("xstrdup: %s", strerror(errno)); fatal("xstrdup");
return cp; return cp;
} }
@@ -101,7 +99,7 @@ xstrndup(const char *str, size_t maxlen)
char *cp; char *cp;
if ((cp = strndup(str, maxlen)) == NULL) if ((cp = strndup(str, maxlen)) == NULL)
fatalx("xstrndup: %s", strerror(errno)); fatal("xstrndup");
return cp; return cp;
} }
@@ -126,7 +124,6 @@ xasprintf(char **ret, const char *fmt, ...)
va_start(ap, fmt); va_start(ap, fmt);
i = xvasprintf(ret, fmt, ap); i = xvasprintf(ret, fmt, ap);
va_end(ap); va_end(ap);
return i; return i;
} }
@@ -136,10 +133,8 @@ xvasprintf(char **ret, const char *fmt, va_list ap)
int i; int i;
i = vasprintf(ret, fmt, ap); i = vasprintf(ret, fmt, ap);
if (i == -1 || *ret == NULL)
if (i == -1) fatal("xvasprintf");
fatalx("xasprintf: %s", strerror(errno));
return i; return i;
} }
@@ -152,7 +147,6 @@ xsnprintf(char *str, size_t len, const char *fmt, ...)
va_start(ap, fmt); va_start(ap, fmt);
i = xvsnprintf(str, len, fmt, ap); i = xvsnprintf(str, len, fmt, ap);
va_end(ap); va_end(ap);
return i; return i;
} }
@@ -163,11 +157,8 @@ xvsnprintf(char *str, size_t len, const char *fmt, va_list ap)
if (len > INT_MAX) if (len > INT_MAX)
fatalx("xsnprintf: len > INT_MAX"); fatalx("xsnprintf: len > INT_MAX");
i = vsnprintf(str, len, fmt, ap); i = vsnprintf(str, len, fmt, ap);
if (i < 0 || i >= (int)len) if (i < 0 || i >= (int)len)
fatalx("xsnprintf: overflow"); fatalx("xsnprintf: overflow");
return i; return i;
} }