From 82aec24db65fd3bf06f60a8952c1b0b3dfd05ec4 Mon Sep 17 00:00:00 2001 From: Dimitri Staessens Date: Sat, 24 Feb 2024 12:52:20 +0100 Subject: irmd: Fix memleak on cancel If the mainloop is cancelled during a write, the response buffer leaks. The IRMd now warns about failed writes only when the error is not EPIPE, as EPIPE is expected to happen with timed out requests. Signed-off-by: Dimitri Staessens Signed-off-by: Sander Vrijders --- src/irmd/main.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/irmd/main.c b/src/irmd/main.c index ecdc2fb9..6e1a1262 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -1536,6 +1536,7 @@ static void * mainloop(void * o) free(cmd); if (msg == NULL) { + log_err("Failed to unpack command message."); close(sfd); continue; } @@ -1582,12 +1583,18 @@ static void * mainloop(void * o) irm_msg__free_unpacked(ret_msg, NULL); pthread_cleanup_push(__cleanup_close_ptr, &sfd); + pthread_cleanup_push(free, buffer.data); + + if (write(sfd, buffer.data, buffer.len) == -1) { + if (errno != EPIPE) + log_warn("Failed to send reply message: %s.", + strerror(errno)); + else + log_dbg("Failed to send reply message: %s.", + strerror(errno)); + } - if (write(sfd, buffer.data, buffer.len) == -1) - log_warn("Failed to send reply message."); - - free(buffer.data); - + pthread_cleanup_pop(true); pthread_cleanup_pop(true); tpm_inc(irmd.tpm); -- cgit v1.2.3