summaryrefslogtreecommitdiff
path: root/util/xmalloc.c
diff options
context:
space:
mode:
authorRuss Allbery <eagle@eyrie.org>2014-07-16 13:46:50 -0700
committerRuss Allbery <eagle@eyrie.org>2014-07-16 13:46:50 -0700
commit1796d631f0846ec98cd286bc4284898a7300ee78 (patch)
tree6fd42de6dc858ef06c6d270410c32ec61f39e593 /util/xmalloc.c
parentf5194217566a6f4cdeffbae551153feb1412210d (diff)
parent6409733ee3b7b1910dc1c166a392cc628834146c (diff)
Merge tag 'upstream/1.1' into debian
Upstream version 1.1 Conflicts: NEWS README client/keytab.c perl/lib/Wallet/ACL.pm perl/sql/Wallet-Schema-0.08-PostgreSQL.sql perl/t/general/admin.t perl/t/verifier/ldap-attr.t Change-Id: I1a1dc09b97c9258e61f1c8877d0837193c8ae2c6
Diffstat (limited to 'util/xmalloc.c')
-rw-r--r--util/xmalloc.c39
1 files changed, 31 insertions, 8 deletions
diff --git a/util/xmalloc.c b/util/xmalloc.c
index a78e31a..721447a 100644
--- a/util/xmalloc.c
+++ b/util/xmalloc.c
@@ -33,6 +33,10 @@
* allocation function will try its allocation again (calling the handler
* again if it still fails).
*
+ * xreallocarray behaves the same as the OpenBSD reallocarray function but for
+ * the same error checking, which in turn is the same as realloc but with
+ * calloc-style arguments and size overflow checking.
+ *
* xstrndup behaves like xstrdup but only copies the given number of
* characters. It allocates an additional byte over its second argument and
* always nul-terminates the string.
@@ -58,7 +62,7 @@
* The canonical version of this file is maintained in the rra-c-util package,
* which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
*
- * Copyright 2012
+ * Copyright 2012, 2013, 2014
* The Board of Trustees of the Leland Stanford Junior University
* Copyright (c) 2004, 2005, 2006
* by Internet Systems Consortium, Inc. ("ISC")
@@ -84,8 +88,6 @@
#include <config.h>
#include <portable/system.h>
-#include <errno.h>
-
#include <util/messages.h>
#include <util/xmalloc.h>
@@ -96,8 +98,12 @@
void
xmalloc_fail(const char *function, size_t size, const char *file, int line)
{
- sysdie("failed to %s %lu bytes at %s line %d", function,
- (unsigned long) size, file, line);
+ if (size == 0)
+ sysdie("failed to format output with %s at %s line %d", function,
+ file, line);
+ else
+ sysdie("failed to %s %lu bytes at %s line %d", function,
+ (unsigned long) size, file, line);
}
/* Assign to this variable to choose a handler other than the default. */
@@ -150,6 +156,20 @@ x_realloc(void *p, size_t size, const char *file, int line)
}
+void *
+x_reallocarray(void *p, size_t n, size_t size, const char *file, int line)
+{
+ void *newp;
+
+ newp = reallocarray(p, n, size);
+ while (newp == NULL && size > 0 && n > 0) {
+ (*xmalloc_error_handler)("reallocarray", n * size, file, line);
+ newp = reallocarray(p, n, size);
+ }
+ return newp;
+}
+
+
char *
x_strdup(const char *s, const char *file, int line)
{
@@ -208,7 +228,8 @@ x_vasprintf(char **strp, const char *fmt, va_list args, const char *file,
va_copy(args_copy, args);
status = vsnprintf(NULL, 0, fmt, args_copy);
va_end(args_copy);
- (*xmalloc_error_handler)("vasprintf", status + 1, file, line);
+ status = (status < 0) ? 0 : status + 1;
+ (*xmalloc_error_handler)("vasprintf", status, file, line);
va_copy(args_copy, args);
status = vasprintf(strp, fmt, args_copy);
va_end(args_copy);
@@ -231,7 +252,8 @@ x_asprintf(char **strp, const char *file, int line, const char *fmt, ...)
va_copy(args_copy, args);
status = vsnprintf(NULL, 0, fmt, args_copy);
va_end(args_copy);
- (*xmalloc_error_handler)("asprintf", status + 1, file, line);
+ status = (status < 0) ? 0 : status + 1;
+ (*xmalloc_error_handler)("asprintf", status, file, line);
va_copy(args_copy, args);
status = vasprintf(strp, fmt, args_copy);
va_end(args_copy);
@@ -252,7 +274,8 @@ x_asprintf(char **strp, const char *fmt, ...)
va_copy(args_copy, args);
status = vsnprintf(NULL, 0, fmt, args_copy);
va_end(args_copy);
- (*xmalloc_error_handler)("asprintf", status + 1, __FILE__, __LINE__);
+ status = (status < 0) ? 0 : status + 1;
+ (*xmalloc_error_handler)("asprintf", status, __FILE__, __LINE__);
va_copy(args_copy, args);
status = vasprintf(strp, fmt, args_copy);
va_end(args_copy);