[PATCH 3/3] alfred: Close the periodic timer fd on setup errors

Sven Eckelmann <[email protected]> Fri, 31 Jul 2026 08:44:21 +0200
Newsgroups org.open-mesh.lists.batman
Message-ID <[email protected]>
create_sync_period_timer() created the timerfd but returned -1 without
closing it when timerfd_settime() or the epoll_ctl() registration failed,
leaking the descriptor. Route those failures through an error label that
closes the timerfd and resets globals->check_timerfd to -1.

Fixes: d04723b78ae1 ("alfred: Stabilize synchronization period using timerfd")
Signed-off-by: Sven Eckelmann <[email protected]>
---
 server.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/server.c b/server.c
index cb3fcfe..72b5ba2 100644
--- a/server.c
+++ b/server.c
@@ -440,7 +440,7 @@ static int create_sync_period_timer(struct globals *globals)
 	ret = timerfd_settime(globals->check_timerfd, 0, &sync_timer, NULL);
 	if (ret < 0) {
 		perror("Failed to arm synchronization timer");
-		return -1;
+		goto err;
 	}
 
 	ev.events = EPOLLIN;
@@ -450,10 +450,15 @@ static int create_sync_period_timer(struct globals *globals)
 	if (epoll_ctl(globals->epollfd, EPOLL_CTL_ADD, globals->check_timerfd,
 		      &ev) == -1) {
 		perror("Failed to add epoll for check_timer");
-		return -1;
+		goto err;
 	}
 
 	return 0;
+
+err:
+	close(globals->check_timerfd);
+	globals->check_timerfd = -1;
+	return -1;
 }
 
 int alfred_server(struct globals *globals)

-- 
2.47.3