summaryrefslogtreecommitdiffstats
path: root/lib/system.h
diff options
context:
space:
mode:
authorMark Wielaard <[email protected]>2021-07-28 16:46:36 +0200
committerMark Wielaard <[email protected]>2021-07-29 14:08:45 +0200
commit1a13c35dc41e82c563ac971de93f2d5caed80815 (patch)
tree0bb640bd34afc5dabfaf6593a752c8a380cf4b8b /lib/system.h
parent9ab0c139eebf4ba40ac721224a673e4b66d29cd9 (diff)
lib: Add static inline reallocarray fallback function
Signed-off-by: Mark Wielaard <[email protected]>
Diffstat (limited to 'lib/system.h')
-rw-r--r--lib/system.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/system.h b/lib/system.h
index cdf18ed7..58d9deee 100644
--- a/lib/system.h
+++ b/lib/system.h
@@ -38,6 +38,7 @@
#include <byteswap.h>
#include <unistd.h>
#include <string.h>
+#include <stdlib.h>
#if __BYTE_ORDER == __LITTLE_ENDIAN
# define LE32(n) (n)
@@ -70,6 +71,19 @@
((void *) ((char *) memcpy (dest, src, n) + (size_t) n))
#endif
+#if !HAVE_DECL_REALLOCARRAY
+static inline void *
+reallocarray (void *ptr, size_t nmemb, size_t size)
+{
+ if (size > 0 && nmemb > SIZE_MAX / size)
+ {
+ errno = ENOMEM;
+ return NULL;
+ }
+ return realloc (ptr, nmemb * size);
+}
+#endif
+
/* Return TRUE if the start of STR matches PREFIX, FALSE otherwise. */
static inline int