]> granicus.if.org Git - sudo/commitdiff
Add debugging output on memory alloc failure.
authorTodd C. Miller <Todd.Miller@courtesan.com>
Fri, 5 Jun 2015 19:18:48 +0000 (13:18 -0600)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Fri, 5 Jun 2015 19:18:48 +0000 (13:18 -0600)
Add missing checks in event_select.c for reallocarray() failure.

lib/util/event.c
lib/util/event_select.c

index a8f7fc9a3b41e82487804b8d41865f78408ee45e..790e69e42ccc5729a18ed8651ecaf149295c442d 100644 (file)
@@ -88,13 +88,18 @@ sudo_ev_base_alloc_v1(void)
     debug_decl(sudo_ev_base_alloc, SUDO_DEBUG_EVENT)
 
     base = calloc(1, sizeof(*base));
-    if (base != NULL) {
-       TAILQ_INIT(&base->events);
-       TAILQ_INIT(&base->timeouts);
-       if (sudo_ev_base_alloc_impl(base) != 0) {
-           free(base);
-           base = NULL;
-       }
+    if (base == NULL) {
+       sudo_debug_printf(SUDO_DEBUG_ERROR,
+           "%s: unable to allocate base", __func__);
+       debug_return_ptr(NULL);
+    }
+    TAILQ_INIT(&base->events);
+    TAILQ_INIT(&base->timeouts);
+    if (sudo_ev_base_alloc_impl(base) != 0) {
+       sudo_debug_printf(SUDO_DEBUG_ERROR,
+           "%s: unable to allocate impl base", __func__);
+       free(base);
+       base = NULL;
     }
 
     debug_return_ptr(base);
@@ -125,13 +130,16 @@ sudo_ev_alloc_v1(int fd, short events, sudo_ev_callback_t callback, void *closur
     /* XXX - sanity check events value */
 
     ev = calloc(1, sizeof(*ev));
-       if (ev != NULL) {
-       ev->fd = fd;
-       ev->events = events;
-       ev->pfd_idx = -1;
-       ev->callback = callback;
-       ev->closure = closure;
+    if (ev == NULL) {
+       sudo_debug_printf(SUDO_DEBUG_ERROR,
+           "%s: unable to allocate event", __func__);
+       debug_return_ptr(NULL);
     }
+    ev->fd = fd;
+    ev->events = events;
+    ev->pfd_idx = -1;
+    ev->callback = callback;
+    ev->closure = closure;
 
     debug_return_ptr(ev);
 }
index 1a2e3058308a6a533b1fd9e9b354d8b4df8bd8e9..ae883bb09762c110910047146a93257c0faa8bdb 100644 (file)
@@ -68,10 +68,9 @@ sudo_ev_base_alloc_impl(struct sudo_event_base *base)
 
     if (base->readfds_in == NULL || base->writefds_in == NULL ||
        base->readfds_out == NULL || base->writefds_out == NULL) {
-       free(base->readfds_in);
-       free(base->writefds_in);
-       free(base->readfds_out);
-       free(base->writefds_out);
+       sudo_debug_printf(SUDO_DEBUG_WARN, "%s: unable to calloc(1, %zu)",
+           __func__, sizeof(fd_mask));
+       sudo_ev_base_free_impl(base);
        debug_return_int(-1);
     }
     debug_return_int(0);
@@ -99,19 +98,34 @@ sudo_ev_add_impl(struct sudo_event_base *base, struct sudo_event *ev)
        const int n = howmany(ev->fd + 1, NFDBITS);
        const size_t used_bytes = o * sizeof(fd_mask);
        const size_t new_bytes = (n - o) * sizeof(fd_mask);
+       fd_set *rfds_in, *wfds_in, *rfds_out, *wfds_out;
+
+       rfds_in = reallocarray(base->readfds_in, n, sizeof(fd_mask));
+       wfds_in = reallocarray(base->writefds_in, n, sizeof(fd_mask));
+       rfds_out = reallocarray(base->readfds_out, n, sizeof(fd_mask));
+       wfds_out = reallocarray(base->writefds_out, n, sizeof(fd_mask));
+       if (rfds_in == NULL || wfds_in == NULL ||
+           rfds_out == NULL || wfds_out == NULL) {
+           sudo_debug_printf(SUDO_DEBUG_WARN,
+               "%s: unable to reallocarray(%d, %zu)",
+               __func__, n, sizeof(fd_mask));
+           free(rfds_in);
+           free(wfds_in);
+           free(rfds_out);
+           debug_return_int(-1);
+       }
 
-       base->readfds_in = reallocarray(base->readfds_in, n, sizeof(fd_mask));
-       memset((char *)base->readfds_in + used_bytes, 0, new_bytes);
-
-       base->writefds_in = reallocarray(base->writefds_in, n, sizeof(fd_mask));
-       memset((char *)base->writefds_in + used_bytes, 0, new_bytes);
-
-       base->readfds_out = reallocarray(base->readfds_out, n, sizeof(fd_mask));
-       memset((char *)base->readfds_out + used_bytes, 0, new_bytes);
-
-       base->writefds_out = reallocarray(base->writefds_out, n, sizeof(fd_mask));
-       memset((char *)base->writefds_out + used_bytes, 0, new_bytes);
-
+       /* Clear newly allocated space. */
+       memset((char *)rfds_in + used_bytes, 0, new_bytes);
+       memset((char *)wfds_in + used_bytes, 0, new_bytes);
+       memset((char *)rfds_out + used_bytes, 0, new_bytes);
+       memset((char *)wfds_out + used_bytes, 0, new_bytes);
+
+       /* Update base. */
+       base->readfds_in = rfds_in;
+       base->writefds_in = wfds_in;
+       base->readfds_out = rfds_out;
+       base->writefds_out = wfds_out;
        base->maxfd = (n * NFDBITS) - 1;
     }