From a860db364521ca6e9046bbf60fbbb1ca2bc08711 Mon Sep 17 00:00:00 2001 From: Daiki Ueno Date: Tue, 8 Aug 2017 14:52:24 +0200 Subject: 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. --- common/compat.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'common/compat.c') 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 -- cgit v1.1