Merge xmalloc.[ch] files across base, skipping OpenSSH for now.

ok nicm
This commit is contained in:
tobias 2015-11-17 18:25:03 +00:00
parent 775fb562bd
commit d0505fd042

View File

@ -25,16 +25,13 @@
#include "tmux.h" #include "tmux.h"
char * char *
xstrdup(const char *s) xstrdup(const char *str)
{ {
char *ptr; char *cp;
size_t len;
len = strlen(s) + 1; if ((cp = strdup(str)) == NULL)
ptr = xmalloc(len); fatal("xstrdup");
return (cp);
strlcpy(ptr, s, len);
return (ptr);
} }
void * void *
@ -43,11 +40,9 @@ xcalloc(size_t nmemb, size_t size)
void *ptr; void *ptr;
if (size == 0 || nmemb == 0) if (size == 0 || nmemb == 0)
fatalx("zero size"); fatalx("xcalloc: zero size");
if (SIZE_MAX / nmemb < size)
fatalx("nmemb * size > SIZE_MAX");
if ((ptr = calloc(nmemb, size)) == NULL) if ((ptr = calloc(nmemb, size)) == NULL)
fatal("xcalloc failed"); log_fatal("xcalloc: allocating %zu bytes", size);
return (ptr); return (ptr);
} }
@ -58,9 +53,9 @@ xmalloc(size_t size)
void *ptr; void *ptr;
if (size == 0) if (size == 0)
fatalx("zero size"); fatalx("xmalloc: zero size");
if ((ptr = malloc(size)) == NULL) if ((ptr = malloc(size)) == NULL)
fatal("xmalloc failed"); log_fatal("xmalloc: allocating %zu bytes", size);
return (ptr); return (ptr);
} }
@ -71,9 +66,9 @@ xrealloc(void *oldptr, size_t newsize)
void *newptr; void *newptr;
if (newsize == 0) if (newsize == 0)
fatalx("zero size"); fatalx("xrealloc: zero size");
if ((newptr = realloc(oldptr, newsize)) == NULL) if ((newptr = realloc(oldptr, newsize)) == NULL)
fatal("xrealloc failed"); log_fatal("xrealloc: allocating %zu bytes", newsize);
return (newptr); return (newptr);
} }
@ -81,15 +76,13 @@ xrealloc(void *oldptr, size_t newsize)
void * void *
xreallocarray(void *oldptr, size_t nmemb, size_t size) xreallocarray(void *oldptr, size_t nmemb, size_t size)
{ {
size_t newsize = nmemb * size;
void *newptr; void *newptr;
if (newsize == 0) if (nmemb == 0 || size == 0)
fatalx("zero size"); fatalx("xreallocarray: zero size");
if (SIZE_MAX / nmemb < size) if ((newptr = reallocarray(oldptr, nmemb, size)) == NULL)
fatalx("nmemb * size > SIZE_MAX"); log_fatal("xreallocarray: allocating %zu * %zu bytes",
if ((newptr = realloc(oldptr, newsize)) == NULL) nmemb, size);
fatal("xreallocarray failed");
return (newptr); return (newptr);
} }
@ -114,7 +107,7 @@ xvasprintf(char **ret, const char *fmt, va_list ap)
i = vasprintf(ret, fmt, ap); i = vasprintf(ret, fmt, ap);
if (i < 0 || *ret == NULL) if (i < 0 || *ret == NULL)
fatal("xvasprintf failed"); fatal("xvasprintf");
return (i); return (i);
} }
@ -138,11 +131,11 @@ xvsnprintf(char *buf, size_t len, const char *fmt, va_list ap)
int i; int i;
if (len > INT_MAX) if (len > INT_MAX)
fatalx("len > INT_MAX"); fatalx("xvsnprintf: len > INT_MAX");
i = vsnprintf(buf, len, fmt, ap); i = vsnprintf(buf, len, fmt, ap);
if (i < 0) if (i < 0 || i >= (int)len)
fatal("vsnprintf failed"); fatalx("xvsnprintf: overflow");
return (i); return (i);
} }