From 09738283d9cfc62f7a1de44f1d10c4e20ee50d43 Mon Sep 17 00:00:00 2001 From: icy17 <1061499390@qq.com> Date: Wed, 10 Apr 2024 18:21:47 +0800 Subject: [PATCH 1/2] Fix potential Null pointer dereference in event-read-fifo.c --- sample/event-read-fifo.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sample/event-read-fifo.c b/sample/event-read-fifo.c index a17b9bd9..fe725f33 100644 --- a/sample/event-read-fifo.c +++ b/sample/event-read-fifo.c @@ -139,6 +139,10 @@ main(int argc, char **argv) #else /* catch SIGINT so that event.fifo can be cleaned up */ signal_int = evsignal_new(base, SIGINT, signal_cb, base); + if (signal_int == NULL) { + perror("evsignal_new"); + exit(1); + } event_add(signal_int, NULL); evfifo = event_new(base, socket, EV_READ|EV_PERSIST, fifo_read, From 25f4439dbdc1ca9695452873e0194fc4c0549e8b Mon Sep 17 00:00:00 2001 From: icy17 <1061499390@qq.com> Date: Mon, 22 Apr 2024 15:11:15 +0800 Subject: [PATCH 2/2] sample/event-read-fifo: add proper exit path Co-authored-by: Azat Khuzhin --- sample/event-read-fifo.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/sample/event-read-fifo.c b/sample/event-read-fifo.c index fe725f33..d3a81f07 100644 --- a/sample/event-read-fifo.c +++ b/sample/event-read-fifo.c @@ -84,7 +84,7 @@ signal_cb(evutil_socket_t fd, short event, void *arg) int main(int argc, char **argv) { - struct event *evfifo; + struct event *evfifo = NULL; struct event_base* base; #ifdef _WIN32 HANDLE socket; @@ -101,7 +101,7 @@ main(int argc, char **argv) return 1; #else - struct event *signal_int; + struct event *signal_int = NULL; struct stat st; const char *fifo = "event.fifo"; int socket; @@ -141,18 +141,27 @@ main(int argc, char **argv) signal_int = evsignal_new(base, SIGINT, signal_cb, base); if (signal_int == NULL) { perror("evsignal_new"); - exit(1); + goto err; } event_add(signal_int, NULL); evfifo = event_new(base, socket, EV_READ|EV_PERSIST, fifo_read, event_self_cbarg()); #endif + if (evfifo == NULL) { + perror("event_new"); + goto err; + } /* Add it to the active events, without a timeout */ event_add(evfifo, NULL); event_base_dispatch(base); + +#ifndef _WIN32 + event_free(signal_int); +#endif + event_free(evfifo); event_base_free(base); #ifdef _WIN32 CloseHandle(socket); @@ -161,6 +170,22 @@ main(int argc, char **argv) unlink(fifo); #endif libevent_global_shutdown(); - return (0); -} + return 0; +err: +#ifndef _WIN32 + if (signal_int) + event_free(signal_int); +#endif + if (evfifo) + event_free(evfifo); + event_base_free(base); +#ifdef _WIN32 + CloseHandle(socket); +#else + close(socket); + unlink(fifo); +#endif + libevent_global_shutdown(); + return 1; +}