From: Roy Marples Date: Fri, 26 Feb 2016 19:45:29 +0000 (+0000) Subject: Roll our own reallocarray(3) function because that and the reallocarr(3) functions... X-Git-Tag: v6.10.2~37 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=30a35f135370e8caf59c8a5ec511c3ce93a4ea5e;p=thirdparty%2Fdhcpcd.git Roll our own reallocarray(3) function because that and the reallocarr(3) functions aren't portable in eloop. --- diff --git a/eloop.c b/eloop.c index 64267fbb..a7763aa3 100644 --- a/eloop.c +++ b/eloop.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -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;