summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Wielaard <mark@klomp.org>2021-07-28 16:46:36 +0200
committerMark Wielaard <mark@klomp.org>2021-07-29 14:08:45 +0200
commit1a13c35dc41e82c563ac971de93f2d5caed80815 (patch)
tree0bb640bd34afc5dabfaf6593a752c8a380cf4b8b
parent9ab0c139eebf4ba40ac721224a673e4b66d29cd9 (diff)
downloadelfutils-1a13c35dc41e82c563ac971de93f2d5caed80815.tar.gz
lib: Add static inline reallocarray fallback function
Signed-off-by: Mark Wielaard <mark@klomp.org>
-rw-r--r--ChangeLog4
-rw-r--r--configure.ac3
-rw-r--r--lib/ChangeLog4
-rw-r--r--lib/system.h14
4 files changed, 25 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index a01f6f9f..12b8f403 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2021-07-28 Mark Wielaard <mark@klomp.org>
+
+ * configure.ac (AC_CHECK_DECLS): Add reallocarray check.
+
2021-05-22 Mark Wielaard <mark@klomp.org>
* configure.ac (AC_INIT): Set version to 0.185.
diff --git a/configure.ac b/configure.ac
index b348a717..7caff2c5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -425,6 +425,9 @@ AC_CHECK_DECLS([powerof2],[],[],[#include <sys/param.h>])
AC_CHECK_DECLS([mempcpy],[],[],
[#define _GNU_SOURCE
#include <string.h>])
+AC_CHECK_DECLS([reallocarray],[],[],
+ [#define _GNU_SOURCE
+ #include <stdlib.h>])
AC_CHECK_FUNCS([process_vm_readv])
diff --git a/lib/ChangeLog b/lib/ChangeLog
index dd3ebcab..44366fec 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,7 @@
+2021-07-28 Mark Wielaard <mark@klomp.org>
+
+ * system.h (reallocarray): New static inline fallback function.
+
2021-04-19 Martin Liska <mliska@suse.cz>
* system.h (startswith): New function.
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