From 6257e2fb1a2042434358d7347afd5c98467aa64c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 10 Aug 2023 12:10:47 +0200 Subject: [PATCH] coredump: use a cleanup handler for destroying iovw objects --- src/basic/io-util.h | 3 +++ src/coredump/coredump.c | 14 +++++--------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/basic/io-util.h b/src/basic/io-util.h index 3ad8267962..4f7989e66d 100644 --- a/src/basic/io-util.h +++ b/src/basic/io-util.h @@ -93,6 +93,9 @@ struct iovec_wrapper { struct iovec_wrapper *iovw_new(void); struct iovec_wrapper *iovw_free(struct iovec_wrapper *iovw); struct iovec_wrapper *iovw_free_free(struct iovec_wrapper *iovw); + +DEFINE_TRIVIAL_CLEANUP_FUNC(struct iovec_wrapper*, iovw_free_free); + void iovw_free_contents(struct iovec_wrapper *iovw, bool free_vectors); int iovw_put(struct iovec_wrapper *iovw, void *data, size_t len); diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index 8ae4a9f63f..8469ab297c 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -1363,8 +1363,8 @@ static int gather_pid_metadata(struct iovec_wrapper *iovw, Context *context) { } static int process_kernel(int argc, char* argv[]) { + _cleanup_(iovw_free_freep) struct iovec_wrapper *iovw = NULL; Context context = {}; - struct iovec_wrapper *iovw; int r; /* When we're invoked by the kernel, stdout/stderr are closed which is dangerous because the fds @@ -1386,12 +1386,12 @@ static int process_kernel(int argc, char* argv[]) { /* Collect all process metadata passed by the kernel through argv[] */ r = gather_pid_metadata_from_argv(iovw, &context, argc - 1, argv + 1); if (r < 0) - goto finish; + return r; /* Collect the rest of the process metadata retrieved from the runtime */ r = gather_pid_metadata(iovw, &context); if (r < 0) - goto finish; + return r; if (!context.is_journald) /* OK, now we know it's not the journal, hence we can make use of it now. */ @@ -1409,13 +1409,9 @@ static int process_kernel(int argc, char* argv[]) { } if (context.is_journald || context.is_pid1) - r = submit_coredump(&context, iovw, STDIN_FILENO); - else - r = send_iovec(iovw, STDIN_FILENO); + return submit_coredump(&context, iovw, STDIN_FILENO); - finish: - iovw = iovw_free_free(iovw); - return r; + return send_iovec(iovw, STDIN_FILENO); } static int process_backtrace(int argc, char *argv[]) { -- 2.25.1