From 870f14108fd701922561a33458f245213dd61575 Mon Sep 17 00:00:00 2001 From: Simon Kelley Date: Sat, 31 Jan 2026 21:20:17 +0000 Subject: [PATCH] Tidy up memory allocation in read_event() --- src/dnsmasq.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/dnsmasq.c b/src/dnsmasq.c index 7a6da56..7dbefc4 100644 --- a/src/dnsmasq.c +++ b/src/dnsmasq.c @@ -1450,8 +1450,6 @@ void send_event(int fd, int event, int data, char *msg) while (writev(fd, iov, msg ? 2 : 1) == -1 && errno == EINTR); } -/* NOTE: the memory used to return msg is leaked: use msgs in events only - to describe fatal errors. */ static int read_event(int fd, struct event_desc *evp, char **msg) { char *buf; @@ -1461,12 +1459,22 @@ static int read_event(int fd, struct event_desc *evp, char **msg) *msg = NULL; - if (evp->msg_sz != 0 && - (buf = malloc(evp->msg_sz + 1)) && - read_write(fd, (unsigned char *)buf, evp->msg_sz, RW_READ)) + if (evp->msg_sz != 0) { - buf[evp->msg_sz] = 0; - *msg = buf; + if (!(buf = whine_malloc(evp->msg_sz + 1))) + { + int i; + unsigned char a; + + /* Keep the stream synchronised if malloc fails. */ + for (i = 0; i < evp->msg_sz; i++) + read_write(fd, &a, 1, RW_READ); + } + else if (read_write(fd, (unsigned char *)buf, evp->msg_sz, RW_READ)) + { + buf[evp->msg_sz] = 0; + *msg = buf; + } } return 1; @@ -1529,9 +1537,6 @@ static void async_event(int pipe, time_t now) int wstatus, i, check = 0; char *msg; - /* NOTE: the memory used to return msg is leaked: use msgs in events only - to describe fatal errors. */ - if (read_event(pipe, &ev, &msg)) switch (ev.event) { @@ -1637,7 +1642,6 @@ static void async_event(int pipe, time_t now) case EVENT_SCRIPT_LOG: my_syslog(MS_SCRIPT | LOG_DEBUG, "%s", msg ? msg : ""); free(msg); - msg = NULL; break; /* necessary for fatal errors in helper */