From 6741fb5378c738d1e21420f70a4116efef70e42c Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Wed, 20 May 2015 10:39:08 -0600 Subject: [PATCH] Use non-exiting allocators. --- lib/util/event.c | 33 ++++++++++++++-------------- lib/util/event_select.c | 48 +++++++++++++++++++++++++++-------------- 2 files changed, 49 insertions(+), 32 deletions(-) diff --git a/lib/util/event.c b/lib/util/event.c index de1761de6..a8f7fc9a3 100644 --- a/lib/util/event.c +++ b/lib/util/event.c @@ -44,14 +44,11 @@ #include #include "sudo_compat.h" -#include "sudo_alloc.h" #include "sudo_fatal.h" #include "sudo_debug.h" #include "sudo_event.h" #include "sudo_util.h" -/* XXX - use non-exiting allocators? */ - /* * Add an event to the base's active queue and mark it active. */ @@ -90,12 +87,14 @@ sudo_ev_base_alloc_v1(void) struct sudo_event_base *base; debug_decl(sudo_ev_base_alloc, SUDO_DEBUG_EVENT) - base = sudo_ecalloc(1, sizeof(*base)); - TAILQ_INIT(&base->events); - TAILQ_INIT(&base->timeouts); - if (sudo_ev_base_alloc_impl(base) != 0) { - sudo_efree(base); - base = NULL; + 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; + } } debug_return_ptr(base); @@ -112,7 +111,7 @@ sudo_ev_base_free_v1(struct sudo_event_base *base) sudo_ev_del(base, ev); } sudo_ev_base_free_impl(base); - sudo_efree(base); + free(base); debug_return; } @@ -125,12 +124,14 @@ sudo_ev_alloc_v1(int fd, short events, sudo_ev_callback_t callback, void *closur /* XXX - sanity check events value */ - ev = sudo_ecalloc(1, sizeof(*ev)); - ev->fd = fd; - ev->events = events; - ev->pfd_idx = -1; - ev->callback = callback; - ev->closure = closure; + ev = calloc(1, sizeof(*ev)); + if (ev != NULL) { + ev->fd = fd; + ev->events = events; + ev->pfd_idx = -1; + ev->callback = callback; + ev->closure = closure; + } debug_return_ptr(ev); } diff --git a/lib/util/event_select.c b/lib/util/event_select.c index a515e95ba..1a2e30583 100644 --- a/lib/util/event_select.c +++ b/lib/util/event_select.c @@ -50,25 +50,30 @@ #include #include "sudo_compat.h" -#include "sudo_alloc.h" #include "sudo_fatal.h" #include "sudo_debug.h" #include "sudo_event.h" #include "sudo_util.h" -/* XXX - use non-exiting allocators? */ - int sudo_ev_base_alloc_impl(struct sudo_event_base *base) { debug_decl(sudo_ev_base_alloc_impl, SUDO_DEBUG_EVENT) base->maxfd = NFDBITS - 1; - base->readfds_in = sudo_ecalloc(1, sizeof(fd_mask)); - base->writefds_in = sudo_ecalloc(1, sizeof(fd_mask)); - base->readfds_out = sudo_ecalloc(1, sizeof(fd_mask)); - base->writefds_out = sudo_ecalloc(1, sizeof(fd_mask)); - + base->readfds_in = calloc(1, sizeof(fd_mask)); + base->writefds_in = calloc(1, sizeof(fd_mask)); + base->readfds_out = calloc(1, sizeof(fd_mask)); + base->writefds_out = calloc(1, sizeof(fd_mask)); + + 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); + debug_return_int(-1); + } debug_return_int(0); } @@ -76,10 +81,10 @@ void sudo_ev_base_free_impl(struct sudo_event_base *base) { debug_decl(sudo_ev_base_free_impl, SUDO_DEBUG_EVENT) - sudo_efree(base->readfds_in); - sudo_efree(base->writefds_in); - sudo_efree(base->readfds_out); - sudo_efree(base->writefds_out); + free(base->readfds_in); + free(base->writefds_in); + free(base->readfds_out); + free(base->writefds_out); debug_return; } @@ -92,10 +97,21 @@ sudo_ev_add_impl(struct sudo_event_base *base, struct sudo_event *ev) if (ev->fd > base->maxfd) { const int o = (base->maxfd + 1) / NFDBITS; const int n = howmany(ev->fd + 1, NFDBITS); - base->readfds_in = sudo_erecalloc(base->readfds_in, o, n, sizeof(fd_mask)); - base->writefds_in = sudo_erecalloc(base->writefds_in, o, n, sizeof(fd_mask)); - base->readfds_out = sudo_erecalloc(base->readfds_out, o, n, sizeof(fd_mask)); - base->writefds_out = sudo_erecalloc(base->writefds_out, o, n, sizeof(fd_mask)); + const size_t used_bytes = o * sizeof(fd_mask); + const size_t new_bytes = (n - o) * sizeof(fd_mask); + + 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); + base->maxfd = (n * NFDBITS) - 1; } -- 2.40.0