mirror of
https://github.com/sudo-project/sudo.git
synced 2025-09-02 15:25:58 +00:00
Add debugging output on memory alloc failure.
Add missing checks in event_select.c for reallocarray() failure.
This commit is contained in:
@@ -88,13 +88,18 @@ sudo_ev_base_alloc_v1(void)
|
|||||||
debug_decl(sudo_ev_base_alloc, SUDO_DEBUG_EVENT)
|
debug_decl(sudo_ev_base_alloc, SUDO_DEBUG_EVENT)
|
||||||
|
|
||||||
base = calloc(1, sizeof(*base));
|
base = calloc(1, sizeof(*base));
|
||||||
if (base != NULL) {
|
if (base == NULL) {
|
||||||
TAILQ_INIT(&base->events);
|
sudo_debug_printf(SUDO_DEBUG_ERROR,
|
||||||
TAILQ_INIT(&base->timeouts);
|
"%s: unable to allocate base", __func__);
|
||||||
if (sudo_ev_base_alloc_impl(base) != 0) {
|
debug_return_ptr(NULL);
|
||||||
free(base);
|
}
|
||||||
base = 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);
|
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 */
|
/* XXX - sanity check events value */
|
||||||
|
|
||||||
ev = calloc(1, sizeof(*ev));
|
ev = calloc(1, sizeof(*ev));
|
||||||
if (ev != NULL) {
|
if (ev == NULL) {
|
||||||
ev->fd = fd;
|
sudo_debug_printf(SUDO_DEBUG_ERROR,
|
||||||
ev->events = events;
|
"%s: unable to allocate event", __func__);
|
||||||
ev->pfd_idx = -1;
|
debug_return_ptr(NULL);
|
||||||
ev->callback = callback;
|
|
||||||
ev->closure = closure;
|
|
||||||
}
|
}
|
||||||
|
ev->fd = fd;
|
||||||
|
ev->events = events;
|
||||||
|
ev->pfd_idx = -1;
|
||||||
|
ev->callback = callback;
|
||||||
|
ev->closure = closure;
|
||||||
|
|
||||||
debug_return_ptr(ev);
|
debug_return_ptr(ev);
|
||||||
}
|
}
|
||||||
|
@@ -68,10 +68,9 @@ sudo_ev_base_alloc_impl(struct sudo_event_base *base)
|
|||||||
|
|
||||||
if (base->readfds_in == NULL || base->writefds_in == NULL ||
|
if (base->readfds_in == NULL || base->writefds_in == NULL ||
|
||||||
base->readfds_out == NULL || base->writefds_out == NULL) {
|
base->readfds_out == NULL || base->writefds_out == NULL) {
|
||||||
free(base->readfds_in);
|
sudo_debug_printf(SUDO_DEBUG_WARN, "%s: unable to calloc(1, %zu)",
|
||||||
free(base->writefds_in);
|
__func__, sizeof(fd_mask));
|
||||||
free(base->readfds_out);
|
sudo_ev_base_free_impl(base);
|
||||||
free(base->writefds_out);
|
|
||||||
debug_return_int(-1);
|
debug_return_int(-1);
|
||||||
}
|
}
|
||||||
debug_return_int(0);
|
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 int n = howmany(ev->fd + 1, NFDBITS);
|
||||||
const size_t used_bytes = o * sizeof(fd_mask);
|
const size_t used_bytes = o * sizeof(fd_mask);
|
||||||
const size_t new_bytes = (n - o) * sizeof(fd_mask);
|
const size_t new_bytes = (n - o) * sizeof(fd_mask);
|
||||||
|
fd_set *rfds_in, *wfds_in, *rfds_out, *wfds_out;
|
||||||
|
|
||||||
base->readfds_in = reallocarray(base->readfds_in, n, sizeof(fd_mask));
|
rfds_in = reallocarray(base->readfds_in, n, sizeof(fd_mask));
|
||||||
memset((char *)base->readfds_in + used_bytes, 0, new_bytes);
|
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->writefds_in = reallocarray(base->writefds_in, n, sizeof(fd_mask));
|
/* Clear newly allocated space. */
|
||||||
memset((char *)base->writefds_in + used_bytes, 0, new_bytes);
|
memset((char *)rfds_in + used_bytes, 0, new_bytes);
|
||||||
|
memset((char *)wfds_in + used_bytes, 0, new_bytes);
|
||||||
base->readfds_out = reallocarray(base->readfds_out, n, sizeof(fd_mask));
|
memset((char *)rfds_out + used_bytes, 0, new_bytes);
|
||||||
memset((char *)base->readfds_out + used_bytes, 0, new_bytes);
|
memset((char *)wfds_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);
|
|
||||||
|
|
||||||
|
/* 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;
|
base->maxfd = (n * NFDBITS) - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user