From 6abc150e89f890f20ec4c3fd7835bfeecd4fed3d Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Sun, 8 Mar 2026 20:57:30 +0000 Subject: util/nvmutil: close files in err() Signed-off-by: Leah Rowe --- util/nvmutil/nvmutil.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index 7cc782c8..7216382e 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -81,6 +81,7 @@ static int xstrxcmp(const char *a, const char *b, size_t maxlen); static void err(int nvm_errval, const char *msg, ...); static const char *getnvmprogname(void); static void set_err(int errval); +static void close_files(void); /* * Sizes in bytes: @@ -372,17 +373,7 @@ main(int argc, char *argv[]) else if (gbe_flags != O_RDONLY) write_gbe_file(); - if (gbe_fd > -1) { - if (close(gbe_fd) == -1) - err(ECANCELED, "close '%s'", fname); - } - -#ifndef HAVE_ARC4RANDOM_BUF - if (urandom_fd > -1) { - if (close(urandom_fd) == -1) - err(ECANCELED, "close '%s'", rname); - } -#endif + close_files(); if (errno) err(ECANCELED, "Unhandled error on exit"); @@ -1345,6 +1336,9 @@ xstrxcmp(const char *a, const char *b, size_t maxlen) static void err(int nvm_errval, const char *msg, ...) { + if (nvm_errval != -1) + close_files(); + va_list args; fprintf(stderr, "%s: ", getnvmprogname()); @@ -1386,3 +1380,19 @@ set_err(int x) else errno = ECANCELED; } + +static void +close_files(void) +{ + if (gbe_fd > -1) { + if (close(gbe_fd) == -1) + err(-1, "close '%s'", fname); + } + +#ifndef HAVE_ARC4RANDOM_BUF + if (urandom_fd > -1) { + if (close(urandom_fd) == -1) + err(-1, "close '%s'", rname); + } +#endif +} -- cgit v1.2.1