util/nvmutil: close files in err()

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2026-03-08 20:57:30 +00:00
parent 840f79fd82
commit 6abc150e89

View File

@@ -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
}