From 05bc6a08305d30aa9c52ab992fb1c29ee1dd3f8f Mon Sep 17 00:00:00 2001 From: NefelibateLG <3453510037@qq.com> Date: Wed, 4 Nov 2026 22:07:18 +0800 Subject: [PATCH] Do not leave stdin as descriptor of a failed context uev_init() leaves the context descriptor at 0 when epoll_create1() fails. A following uev_exit() then closes stdin. Set it to -1 before creating the epoll descriptor. --- src/uev.c | 1 + test/error.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/uev.c b/src/uev.c index ca757bc..b75cdc4 100644 --- a/src/uev.c +++ b/src/uev.c @@ -226,6 +226,7 @@ int uev_init1(uev_ctx_t *ctx, int maxevents) maxevents = UEV_MAX_EVENTS; memset(ctx, 0, sizeof(*ctx)); + ctx->fd = -1; ctx->maxevents = maxevents; return _init(ctx, 0); diff --git a/test/error.c b/test/error.c index 3c9dc34..f378b91 100644 --- a/test/error.c +++ b/test/error.c @@ -1,5 +1,6 @@ /* Verifies cleanup on failure to set up a context or watcher */ #include "check.h" +#include /* A failed uev_event_init() must not leak its eventfd */ static void event_init(void) @@ -21,9 +22,29 @@ static void event_init(void) close(fd); } +/* A failed uev_init() must not leave stdin as the context descriptor */ +static void ctx_init(void) +{ + struct rlimit old, lim; + uev_ctx_t ctx; + int rc; + + fail_unless(getrlimit(RLIMIT_NOFILE, &old) == 0); + lim = old; + lim.rlim_cur = 0; + fail_unless(setrlimit(RLIMIT_NOFILE, &lim) == 0); + + rc = uev_init(&ctx); + + fail_unless(setrlimit(RLIMIT_NOFILE, &old) == 0); + fail_unless(rc == -1); + fail_unless(ctx.fd == -1); +} + int main(void) { event_init(); + ctx_init(); return 0; }