From 4ecdadb7a601b0613e65bf8547d17b39ed87153f Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Sat, 28 Mar 2026 08:32:27 +0000 Subject: libreboot-utils: unified errno handling on returns Signed-off-by: Leah Rowe --- util/libreboot-utils/include/common.h | 1 + util/libreboot-utils/lib/file.c | 36 +++++++---------------------------- util/libreboot-utils/lib/io.c | 17 +++++------------ util/libreboot-utils/lib/mkhtemp.c | 29 +++++----------------------- util/libreboot-utils/lib/string.c | 23 +++++++++++++++------- 5 files changed, 34 insertions(+), 72 deletions(-) (limited to 'util/libreboot-utils') diff --git a/util/libreboot-utils/include/common.h b/util/libreboot-utils/include/common.h index d9f327ee..e8252d4b 100644 --- a/util/libreboot-utils/include/common.h +++ b/util/libreboot-utils/include/common.h @@ -490,6 +490,7 @@ int try_err(int loop_err, int errval); */ void usage(void); +int set_errno(int saved_errno, int fallback); void err_exit(int nvm_errval, const char *msg, ...); func_t errhook(func_t ptr); /* hook function for cleanup on err */ const char *getnvmprogname(void); diff --git a/util/libreboot-utils/lib/file.c b/util/libreboot-utils/lib/file.c index 5d656e0e..96654640 100644 --- a/util/libreboot-utils/lib/file.c +++ b/util/libreboot-utils/lib/file.c @@ -62,11 +62,7 @@ same_file(int fd, struct stat *st_old, return 0; err_same_file: - - if (errno == saved_errno) - errno = ESTALE; - - return -1; + return set_errno(saved_errno, ESTALE); } void @@ -155,13 +151,11 @@ fsync_dir(const char *path) err_fsync_dir: - if (errno == saved_errno) - errno = EIO; free_and_set_null(&dirbuf); close_on_eintr(&dirfd); - return -1; + return set_errno(saved_errno, EIO); } /* rw_file_exact() - Read perfectly or die @@ -264,10 +258,7 @@ rw_file_exact(int fd, unsigned char *mem, size_t nrw, err_rw_file_exact: - if (errno == saved_errno) - errno = EIO; - - return -1; + return set_errno(saved_errno, EIO); } /* prw() - portable read-write with more @@ -446,11 +437,7 @@ real_pread_pwrite: #endif err_prw: - - if (errno == saved_errno) - errno = EIO; - - return -1; + return set_errno(saved_errno, EIO); } int @@ -472,10 +459,7 @@ io_args(int fd, void *mem, size_t nrw, return 0; err_io_args: - if (errno == saved_errno) - errno = EINVAL; - - return -1; + return set_errno(saved_errno, EINVAL); } int @@ -493,10 +477,7 @@ check_file(int fd, struct stat *st) return 0; err_is_file: - if (errno == saved_errno) - errno = EINVAL; - - return -1; + return set_errno(saved_errno, EINVAL); } /* POSIX can say whatever it wants. @@ -518,10 +499,7 @@ rw_over_nrw(ssize_t r, size_t nrw) return r; err_rw_over_nrw: - if (errno == saved_errno) - errno = EIO; - - return -1; + return set_errno(saved_errno, EIO); } off_t diff --git a/util/libreboot-utils/lib/io.c b/util/libreboot-utils/lib/io.c index f69954c8..d3f5733d 100644 --- a/util/libreboot-utils/lib/io.c +++ b/util/libreboot-utils/lib/io.c @@ -439,10 +439,8 @@ gbe_mv(void) tmp_gbe_bin_exists = 0; ret_gbe_mv: - if (f->gbe_fd > -1) { close_on_eintr(&f->gbe_fd); - f->gbe_fd = -1; if (fsync_dir(f->fname) < 0) { f->io_err_gbe_bin = 1; @@ -462,17 +460,12 @@ ret_gbe_mv: tmp_gbe_bin_exists = 0; } - if (rval < 0) { - /* if nothing set errno, - * we assume EIO, or we - * use what was set - */ - if (errno == saved_errno) - errno = EIO; - } else { - errno = saved_errno; - } + if (rval >= 0) + goto out; + return set_errno(saved_errno, EIO); +out: + errno = saved_errno; return rval; } diff --git a/util/libreboot-utils/lib/mkhtemp.c b/util/libreboot-utils/lib/mkhtemp.c index 2726cb02..b5b9aeeb 100644 --- a/util/libreboot-utils/lib/mkhtemp.c +++ b/util/libreboot-utils/lib/mkhtemp.c @@ -295,11 +295,7 @@ tmpdir_policy(const char *path, return 0; err_tmpdir_policy: - - if (errno == saved_errno) - errno = EIO; - - return -1; + return set_errno(saved_errno, EIO); } int @@ -377,10 +373,7 @@ err_same_dir: close_on_eintr(&fd_a); close_on_eintr(&fd_b); - if (errno == saved_errno) - errno = EIO; - - return -1; + return set_errno(saved_errno, EIO); } /* bypass_all_sticky_checks: if set, @@ -467,19 +460,15 @@ world_writeable_and_sticky( goto sticky_hell; /* heaven visa denied */ sticky_heaven: - close_on_eintr(&dirfd); errno = saved_errno; return 1; sticky_hell: - - if (errno == saved_errno) - errno = EPERM; - close_on_eintr(&dirfd); + (void) set_errno(saved_errno, EPERM); return 0; } @@ -923,11 +912,7 @@ int secure_file(int *fd, return 0; err_demons: - - if (errno == saved_errno) - errno = EIO; - - return -1; + return set_errno(saved_errno, EIO); } int @@ -1037,9 +1022,5 @@ lock_file(int fd, int flags) return 0; err_lock_file: - - if (errno == saved_errno) - errno = EIO; - - return -1; + return set_errno(saved_errno, EIO); } diff --git a/util/libreboot-utils/lib/string.c b/util/libreboot-utils/lib/string.c index 9e38a9e9..76141c58 100644 --- a/util/libreboot-utils/lib/string.c +++ b/util/libreboot-utils/lib/string.c @@ -201,14 +201,10 @@ scatn(ssize_t sc, const char **sv, errno = saved_errno; return 0; err: - if (ct != NULL) - free(ct); - if (size != NULL) - free(size); - if (errno == saved_errno) - errno = EFAULT; + free_and_set_null(&ct); + free_and_set_null((char **)&size); - return -1; + return set_errno(saved_errno, EFAULT); } /* strict strcat */ @@ -285,6 +281,19 @@ err: return -1; } +/* on functions that return with errno, + * i sometimes have a default fallback, + * which is set if errno wasn't changed, + * under error condition. + */ +int +set_errno(int saved_errno, int fallback) +{ + if (errno == saved_errno) + errno = fallback; + return -1; +} + /* the one for nvmutil state is in state.c */ /* this one just exits */ void -- cgit v1.2.1