diff --git a/src/event.c b/src/event.c index 7fb3193..020201c 100644 --- a/src/event.c +++ b/src/event.c @@ -57,8 +57,14 @@ int uev_event_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg) if (fd < 0) return -1; - return _uev_watcher_init(ctx, w, UEV_EVENT_TYPE, cb, arg, fd, UEV_READ) - || _uev_watcher_start(w); + if (_uev_watcher_init(ctx, w, UEV_EVENT_TYPE, cb, arg, fd, UEV_READ) || + _uev_watcher_start(w)) { + close(fd); + w->fd = -1; + return -1; + } + + return 0; } /** diff --git a/test/.gitignore b/test/.gitignore index 01a9842..db5b9b0 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -5,6 +5,7 @@ api complete cronrun cronstart +error io signal stdin diff --git a/test/Makefile.am b/test/Makefile.am index e3fb6a1..137850b 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -10,6 +10,7 @@ TESTS += api TESTS += complete TESTS += cronrun TESTS += cronstart +TESTS += error TESTS += io TESTS += signal TESTS += stdin diff --git a/test/error.c b/test/error.c new file mode 100644 index 0000000..3c9dc34 --- /dev/null +++ b/test/error.c @@ -0,0 +1,36 @@ +/* Verifies cleanup on failure to set up a context or watcher */ +#include "check.h" + +/* A failed uev_event_init() must not leak its eventfd */ +static void event_init(void) +{ + uev_ctx_t ctx; + uev_t w; + int fd; + + fail_unless(uev_init(&ctx) == 0); + fail_unless(close(ctx.fd) == 0); + + fd = dup(STDIN_FILENO); + fail_unless(fd >= 0); + close(fd); + + fail_unless(uev_event_init(&ctx, &w, NULL, NULL) != 0); + fail_unless(w.fd == -1); + fail_unless(dup(STDIN_FILENO) == fd); + close(fd); +} + +int main(void) +{ + event_init(); + + return 0; +} + +/** + * Local Variables: + * indent-tabs-mode: t + * c-file-style: "linux" + * End: + */