Here's an updated diff: - use "overflow" error message for snprintf and friends - use err instead of errx for out of memory conditions - if fatal() doesn't print error string, use ": %s", strerror(errno)
Is this okay for ssh and tmux, which are out to be very portable? Nicholas mentioned that malloc is not required to set errno. I've also checked the standard and it's just an extension. Although at worst, the user sees a wrong error message... Index: usr.bin/cvs/xmalloc.c =================================================================== RCS file: /cvs/src/usr.bin/cvs/xmalloc.c,v retrieving revision 1.12 diff -u -p -u -p -r1.12 xmalloc.c --- usr.bin/cvs/xmalloc.c 5 Nov 2015 09:48:21 -0000 1.12 +++ usr.bin/cvs/xmalloc.c 8 Nov 2015 00:27:13 -0000 @@ -13,6 +13,8 @@ * called by a name other than "ssh" or "Secure Shell". */ +#include <errno.h> +#include <limits.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> @@ -30,7 +32,8 @@ xmalloc(size_t size) fatal("xmalloc: zero size"); ptr = malloc(size); if (ptr == NULL) - fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size); + fatal("xmalloc: allocating %zu bytes: %s", + size, strerror(errno)); return ptr; } @@ -41,12 +44,10 @@ xcalloc(size_t nmemb, size_t size) if (size == 0 || nmemb == 0) fatal("xcalloc: zero size"); - if (SIZE_MAX / nmemb < size) - fatal("xcalloc: nmemb * size > SIZE_MAX"); ptr = calloc(nmemb, size); if (ptr == NULL) - fatal("xcalloc: out of memory (allocating %lu bytes)", - (u_long)(size * nmemb)); + fatal("xcalloc: allocating %zu * %zu bytes: %s", + nmemb, size, strerror(errno)); return ptr; } @@ -54,28 +55,23 @@ void * xreallocarray(void *ptr, size_t nmemb, size_t size) { void *new_ptr; - size_t new_size = nmemb * size; - if (new_size == 0) - fatal("xrealloc: zero size"); - if (SIZE_MAX / nmemb < size) - fatal("xrealloc: nmemb * size > SIZE_MAX"); - new_ptr = realloc(ptr, new_size); + if (nmemb == 0 || size == 0) + fatal("xreallocarray: zero size"); + new_ptr = reallocarray(ptr, nmemb, size); if (new_ptr == NULL) - fatal("xrealloc: out of memory (new_size %lu bytes)", - (u_long) new_size); + fatal("xreallocarray: allocating %zu * %zu bytes: %s", + nmemb, size, strerror(errno)); return new_ptr; } char * xstrdup(const char *str) { - size_t len; char *cp; - len = strlen(str) + 1; - cp = xmalloc(len); - strlcpy(cp, str, len); + if ((cp = strdup(str)) == NULL) + fatal("xstrdup: %s", strerror(errno)); return cp; } @@ -90,23 +86,26 @@ xasprintf(char **ret, const char *fmt, . va_end(ap); if (i < 0 || *ret == NULL) - fatal("xasprintf: could not allocate memory"); + fatal("xasprintf: %s", strerror(errno)); - return (i); + return i; } int -xsnprintf(char *str, size_t size, const char *fmt, ...) +xsnprintf(char *str, size_t len, const char *fmt, ...) { va_list ap; int i; + if (len > INT_MAX) + fatal("xsnprintf: len > INT_MAX"); + va_start(ap, fmt); - i = vsnprintf(str, size, fmt, ap); + i = vsnprintf(str, len, fmt, ap); va_end(ap); - if (i == -1 || i >= (int)size) + if (i < 0 || i >= (int)len) fatal("xsnprintf: overflow"); - return (i); + return i; } Index: usr.bin/diff/xmalloc.c =================================================================== RCS file: /cvs/src/usr.bin/diff/xmalloc.c,v retrieving revision 1.8 diff -u -p -u -p -r1.8 xmalloc.c --- usr.bin/diff/xmalloc.c 25 Sep 2015 16:16:26 -0000 1.8 +++ usr.bin/diff/xmalloc.c 8 Nov 2015 00:27:13 -0000 @@ -27,9 +27,11 @@ xmalloc(size_t size) { void *ptr; + if (size == 0) + errx(2, "xmalloc: zero size"); ptr = malloc(size); if (ptr == NULL) - err(2, "xmalloc %zu", size); + err(2, "xmalloc: allocating %zu bytes", size); return ptr; } @@ -40,8 +42,7 @@ xcalloc(size_t nmemb, size_t size) ptr = calloc(nmemb, size); if (ptr == NULL) - err(2, "xcalloc: out of memory (allocating %zu*%zu bytes)", - nmemb, size); + err(2, "xcalloc: allocating %zu * %zu bytes", nmemb, size); return ptr; } @@ -52,7 +53,8 @@ xreallocarray(void *ptr, size_t nmemb, s new_ptr = reallocarray(ptr, nmemb, size); if (new_ptr == NULL) - err(2, "xrealloc %zu*%zu", nmemb, size); + err(2, "xreallocarray: allocating %zu * %zu bytes", + nmemb, size); return new_ptr; } @@ -62,7 +64,7 @@ xstrdup(const char *str) char *cp; if ((cp = strdup(str)) == NULL) - err(1, "xstrdup"); + err(2, "xstrdup"); return cp; } @@ -79,5 +81,5 @@ xasprintf(char **ret, const char *fmt, . if (i < 0 || *ret == NULL) err(2, "xasprintf"); - return (i); + return i; } Index: usr.bin/diff/xmalloc.h =================================================================== RCS file: /cvs/src/usr.bin/diff/xmalloc.h,v retrieving revision 1.3 diff -u -p -u -p -r1.3 xmalloc.h --- usr.bin/diff/xmalloc.h 29 Apr 2015 04:00:25 -0000 1.3 +++ usr.bin/diff/xmalloc.h 8 Nov 2015 00:27:13 -0000 @@ -22,7 +22,6 @@ void *xmalloc(size_t); void *xcalloc(size_t, size_t); void *xreallocarray(void *, size_t, size_t); -void xfree(void *); char *xstrdup(const char *); int xasprintf(char **, const char *, ...) __attribute__((__format__ (printf, 2, 3))) Index: usr.bin/file/xmalloc.c =================================================================== RCS file: /cvs/src/usr.bin/file/xmalloc.c,v retrieving revision 1.2 diff -u -p -u -p -r1.2 xmalloc.c --- usr.bin/file/xmalloc.c 17 Jun 2015 18:51:11 -0000 1.2 +++ usr.bin/file/xmalloc.c 8 Nov 2015 00:27:13 -0000 @@ -31,9 +31,7 @@ xmalloc(size_t size) errx(1, "xmalloc: zero size"); ptr = malloc(size); if (ptr == NULL) - errx(1, - "xmalloc: out of memory (allocating %zu bytes)", - size); + err(1, "xmalloc: allocating %zu bytes", size); return ptr; } @@ -44,12 +42,9 @@ xcalloc(size_t nmemb, size_t size) if (size == 0 || nmemb == 0) errx(1, "xcalloc: zero size"); - if (SIZE_MAX / nmemb < size) - errx(1, "xcalloc: nmemb * size > SIZE_MAX"); ptr = calloc(nmemb, size); if (ptr == NULL) - errx(1, "xcalloc: out of memory (allocating %zu bytes)", - (size * nmemb)); + err(1, "xcalloc: allocating %zu * %zu bytes", nmemb, size); return ptr; } @@ -60,8 +55,8 @@ xreallocarray(void *ptr, size_t nmemb, s new_ptr = reallocarray(ptr, nmemb, size); if (new_ptr == NULL) - errx(1, "xreallocarray: out of memory (new_size %zu bytes)", - nmemb * size); + err(1, "xreallocarray: allocating %zu * %zu bytes", + nmemb, size); return new_ptr; } @@ -86,7 +81,7 @@ xasprintf(char **ret, const char *fmt, . va_end(ap); if (i < 0 || *ret == NULL) - errx(1, "xasprintf: could not allocate memory"); + err(1, "xasprintf"); - return (i); + return i; } Index: usr.bin/file/xmalloc.h =================================================================== RCS file: /cvs/src/usr.bin/file/xmalloc.h,v retrieving revision 1.2 diff -u -p -u -p -r1.2 xmalloc.h --- usr.bin/file/xmalloc.h 20 Jul 2015 08:51:41 -0000 1.2 +++ usr.bin/file/xmalloc.h 8 Nov 2015 00:27:13 -0000 @@ -27,4 +27,4 @@ int xasprintf(char **, const char *, .. __attribute__((__format__ (printf, 2, 3))) __attribute__((__nonnull__ (2))); -#endif /* XMALLOC_H */ +#endif /* XMALLOC_H */ Index: usr.bin/rcs/xmalloc.c =================================================================== RCS file: /cvs/src/usr.bin/rcs/xmalloc.c,v retrieving revision 1.10 diff -u -p -u -p -r1.10 xmalloc.c --- usr.bin/rcs/xmalloc.c 17 Jun 2015 20:50:10 -0000 1.10 +++ usr.bin/rcs/xmalloc.c 8 Nov 2015 00:27:13 -0000 @@ -31,9 +31,7 @@ xmalloc(size_t size) errx(1, "xmalloc: zero size"); ptr = malloc(size); if (ptr == NULL) - errx(1, - "xmalloc: out of memory (allocating %zu bytes)", - size); + err(1, "xmalloc: allocating %zu bytes", size); return ptr; } @@ -44,12 +42,9 @@ xcalloc(size_t nmemb, size_t size) if (size == 0 || nmemb == 0) errx(1, "xcalloc: zero size"); - if (SIZE_MAX / nmemb < size) - errx(1, "xcalloc: nmemb * size > SIZE_MAX"); ptr = calloc(nmemb, size); if (ptr == NULL) - errx(1, "xcalloc: out of memory (allocating %zu bytes)", - (size * nmemb)); + err(1, "xcalloc: allocating %zu * %zu bytes", nmemb, size); return ptr; } @@ -60,8 +55,8 @@ xreallocarray(void *ptr, size_t nmemb, s new_ptr = reallocarray(ptr, nmemb, size); if (new_ptr == NULL) - errx(1, "xreallocarray: out of memory (new_size %zu bytes)", - nmemb * size); + err(1, "xreallocarray: allocating %zu * %zu bytes", + nmemb, size); return new_ptr; } @@ -86,7 +81,7 @@ xasprintf(char **ret, const char *fmt, . va_end(ap); if (i < 0 || *ret == NULL) - errx(1, "xasprintf: could not allocate memory"); + err(1, "xasprintf"); - return (i); + return i; } Index: usr.bin/rcs/xmalloc.h =================================================================== RCS file: /cvs/src/usr.bin/rcs/xmalloc.h,v retrieving revision 1.3 diff -u -p -u -p -r1.3 xmalloc.h --- usr.bin/rcs/xmalloc.h 13 Jun 2015 20:15:21 -0000 1.3 +++ usr.bin/rcs/xmalloc.h 8 Nov 2015 00:27:13 -0000 @@ -27,4 +27,4 @@ int xasprintf(char **, const char *, .. __attribute__((__format__ (printf, 2, 3))) __attribute__((__nonnull__ (2))); -#endif /* XMALLOC_H */ +#endif /* XMALLOC_H */ Index: usr.bin/ssh/xmalloc.c =================================================================== RCS file: /cvs/src/usr.bin/ssh/xmalloc.c,v retrieving revision 1.32 diff -u -p -u -p -r1.32 xmalloc.c --- usr.bin/ssh/xmalloc.c 24 Apr 2015 01:36:01 -0000 1.32 +++ usr.bin/ssh/xmalloc.c 8 Nov 2015 00:27:14 -0000 @@ -13,6 +13,7 @@ * called by a name other than "ssh" or "Secure Shell". */ +#include <errno.h> #include <stdarg.h> #include <stdint.h> #include <stdio.h> @@ -31,7 +32,8 @@ xmalloc(size_t size) fatal("xmalloc: zero size"); ptr = malloc(size); if (ptr == NULL) - fatal("xmalloc: out of memory (allocating %zu bytes)", size); + fatal("xmalloc: allocating %zu bytes: %s", size, + strerror(errno)); return ptr; } @@ -42,12 +44,10 @@ xcalloc(size_t nmemb, size_t size) if (size == 0 || nmemb == 0) fatal("xcalloc: zero size"); - if (SIZE_MAX / nmemb < size) - fatal("xcalloc: nmemb * size > SIZE_MAX"); ptr = calloc(nmemb, size); if (ptr == NULL) - fatal("xcalloc: out of memory (allocating %zu bytes)", - size * nmemb); + fatal("xcalloc: allocating %zu * %zu bytes: %s", + nmemb, size, strerror(errno)); return ptr; } @@ -58,20 +58,18 @@ xreallocarray(void *ptr, size_t nmemb, s new_ptr = reallocarray(ptr, nmemb, size); if (new_ptr == NULL) - fatal("xreallocarray: out of memory (%zu elements of %zu bytes)", - nmemb, size); + fatal("xreallocarray: allocating %zu * %zu bytes: %s", + nmemb, size, strerror(errno)); return new_ptr; } char * xstrdup(const char *str) { - size_t len; char *cp; - len = strlen(str) + 1; - cp = xmalloc(len); - strlcpy(cp, str, len); + if ((cp = strdup(str)) == NULL) + fatal("xstrdup: %s", strerror(errno)); return cp; } @@ -86,7 +84,7 @@ xasprintf(char **ret, const char *fmt, . va_end(ap); if (i < 0 || *ret == NULL) - fatal("xasprintf: could not allocate memory"); + fatal("xasprintf: %s", strerror(errno)); - return (i); + return i; } Index: usr.bin/ssh/xmalloc.h =================================================================== RCS file: /cvs/src/usr.bin/ssh/xmalloc.h,v retrieving revision 1.15 diff -u -p -u -p -r1.15 xmalloc.h --- usr.bin/ssh/xmalloc.h 24 Apr 2015 01:36:01 -0000 1.15 +++ usr.bin/ssh/xmalloc.h 8 Nov 2015 00:27:14 -0000 @@ -16,6 +16,9 @@ * called by a name other than "ssh" or "Secure Shell". */ +#ifndef XMALLOC_H +#define XMALLOC_H + void *xmalloc(size_t); void *xcalloc(size_t, size_t); void *xreallocarray(void *, size_t, size_t); @@ -23,3 +26,5 @@ char *xstrdup(const char *); int xasprintf(char **, const char *, ...) __attribute__((__format__ (printf, 2, 3))) __attribute__((__nonnull__ (2))); + +#endif /* XMALLOC_H */ Index: usr.bin/tmux/xmalloc.c =================================================================== RCS file: /cvs/src/usr.bin/tmux/xmalloc.c,v retrieving revision 1.7 diff -u -p -u -p -r1.7 xmalloc.c --- usr.bin/tmux/xmalloc.c 20 Oct 2014 23:57:14 -0000 1.7 +++ usr.bin/tmux/xmalloc.c 8 Nov 2015 00:27:14 -0000 @@ -25,16 +25,13 @@ #include "tmux.h" char * -xstrdup(const char *s) +xstrdup(const char *str) { - char *ptr; - size_t len; + char *cp; - len = strlen(s) + 1; - ptr = xmalloc(len); - - strlcpy(ptr, s, len); - return (ptr); + if ((cp = strdup(str)) == NULL) + fatal("xstrdup"); + return (cp); } void * @@ -43,11 +40,9 @@ xcalloc(size_t nmemb, size_t size) void *ptr; if (size == 0 || nmemb == 0) - fatalx("zero size"); - if (SIZE_MAX / nmemb < size) - fatalx("nmemb * size > SIZE_MAX"); + fatalx("xcalloc: zero size"); if ((ptr = calloc(nmemb, size)) == NULL) - fatal("xcalloc failed"); + log_fatal("xcalloc: allocating %zu bytes", size); return (ptr); } @@ -58,9 +53,9 @@ xmalloc(size_t size) void *ptr; if (size == 0) - fatalx("zero size"); + fatalx("xmalloc: zero size"); if ((ptr = malloc(size)) == NULL) - fatal("xmalloc failed"); + log_fatal("xmalloc: allocating %zu bytes", size); return (ptr); } @@ -71,9 +66,9 @@ xrealloc(void *oldptr, size_t newsize) void *newptr; if (newsize == 0) - fatalx("zero size"); + fatalx("xrealloc: zero size"); if ((newptr = realloc(oldptr, newsize)) == NULL) - fatal("xrealloc failed"); + log_fatal("xrealloc: allocating %zu bytes", newsize); return (newptr); } @@ -81,15 +76,13 @@ xrealloc(void *oldptr, size_t newsize) void * xreallocarray(void *oldptr, size_t nmemb, size_t size) { - size_t newsize = nmemb * size; void *newptr; - if (newsize == 0) - fatalx("zero size"); - if (SIZE_MAX / nmemb < size) - fatalx("nmemb * size > SIZE_MAX"); - if ((newptr = realloc(oldptr, newsize)) == NULL) - fatal("xreallocarray failed"); + if (nmemb == 0 || size == 0) + fatalx("xreallocarray: zero size"); + if ((newptr = reallocarray(oldptr, nmemb, size)) == NULL) + log_fatal("xreallocarray: allocating %zu * %zu bytes", + nmemb, size); return (newptr); } @@ -114,7 +107,7 @@ xvasprintf(char **ret, const char *fmt, i = vasprintf(ret, fmt, ap); if (i < 0 || *ret == NULL) - fatal("xvasprintf failed"); + fatal("xvasprintf"); return (i); } @@ -138,11 +131,11 @@ xvsnprintf(char *buf, size_t len, const int i; if (len > INT_MAX) - fatalx("len > INT_MAX"); + fatalx("xvsnprintf: len > INT_MAX"); i = vsnprintf(buf, len, fmt, ap); - if (i < 0) - fatal("vsnprintf failed"); + if (i < 0 || i >= (int)len) + fatalx("xvsnprintf: overflow"); return (i); }