summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2026-03-08 20:57:30 +0000
committerLeah Rowe <leah@libreboot.org>2026-03-08 20:57:30 +0000
commit6abc150e89f890f20ec4c3fd7835bfeecd4fed3d (patch)
treea55bede0db77f5bd52dd185611a9ca29e5a30e9a /util
parent840f79fd82839b6031c67f22e955aa87395ffb45 (diff)
util/nvmutil: close files in err()
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util')
-rw-r--r--util/nvmutil/nvmutil.c32
1 files 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
+}