]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Roll our own reallocarray(3) function because that and the reallocarr(3) functions...
authorRoy Marples <roy@marples.name>
Fri, 26 Feb 2016 19:45:29 +0000 (19:45 +0000)
committerRoy Marples <roy@marples.name>
Fri, 26 Feb 2016 19:45:29 +0000 (19:45 +0000)
eloop.c

diff --git a/eloop.c b/eloop.c
index 64267fbb33efd3a6f3b5243e35517b0f687ea7a7..a7763aa36b32eb1454872e912776d4c173189176 100644 (file)
--- a/eloop.c
+++ b/eloop.c
@@ -34,6 +34,7 @@
 #include <errno.h>
 #include <limits.h>
 #include <signal.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
@@ -181,6 +182,24 @@ struct eloop {
        int exitcode;
 };
 
+#ifdef HAVE_POLL
+/* Handy routing to check for potential overflow.
+ * reallocarray(3) and reallocarr(3) are not portable and this
+ * implementation is smaller than using either in libc in
+ * the final binary size. */
+#define SQRT_SIZE_MAX (((size_t)1) << (sizeof(size_t) * CHAR_BIT / 2))
+static void *
+eloop_realloca(void *ptr, size_t n, size_t size)
+{
+
+       if ((n | size) >= SQRT_SIZE_MAX && n > SIZE_MAX / size) {
+               errno = EOVERFLOW;
+               return NULL;
+       }
+       return realloc(ptr, n * size);
+}
+#endif
+
 #ifdef HAVE_POLL
 static void
 eloop_event_setup_fds(struct eloop *eloop)
@@ -323,8 +342,8 @@ eloop_event_add(struct eloop *eloop, int fd,
        eloop->events_len++;
 #ifdef HAVE_POLL
        if (eloop->events_len > eloop->fds_len) {
-               nfds = realloc(eloop->fds,
-                   sizeof(*eloop->fds) * (eloop->fds_len + 5));
+               nfds = eloop_realloca(eloop->fds,
+                   (eloop->fds_len + 5), sizeof(*eloop->fds));
                if (nfds == NULL)
                        goto err;
                eloop->fds_len += 5;