summaryrefslogtreecommitdiff
path: root/common/compat.c
diff options
context:
space:
mode:
authorDaiki Ueno <dueno@redhat.com>2017-08-08 14:52:24 +0200
committerDaiki Ueno <ueno@gnu.org>2017-08-08 16:04:40 +0200
commita860db364521ca6e9046bbf60fbbb1ca2bc08711 (patch)
tree2dc9206f1abd6a72697a3a851594cbd6bd7816b1 /common/compat.c
parent53402f9e5296718d22ddf1a77658067c2751f068 (diff)
common: Use reallocarray instead of realloc as appropriate
reallocarray is a new POSIX function added in glibc 2.26, with built-in overflow checks. Take advantage of that function for internal array allocation.
Diffstat (limited to 'common/compat.c')
-rw-r--r--common/compat.c17
1 files changed, 17 insertions, 0 deletions
diff --git a/common/compat.c b/common/compat.c
index 692e2ca..3114724 100644
--- a/common/compat.c
+++ b/common/compat.c
@@ -487,6 +487,23 @@ strndup (const char *data,
#endif /* HAVE_STRNDUP */
+#ifndef HAVE_REALLOCARRAY
+
+void *
+reallocarray (void *ptr,
+ size_t nmemb,
+ size_t size)
+{
+ assert (nmemb > 0 && size > 0);
+ if (SIZE_MAX / nmemb < size) {
+ errno = ENOMEM;
+ return NULL;
+ }
+ return realloc (ptr, nmemb * size);
+}
+
+#endif /* HAVE_MEMDUP */
+
#ifndef HAVE_STRCONCAT
#include <stdarg.h>