From ad47d4f190175a5690f275175f3b3cd4ed35a095 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Tue, 29 Mar 2022 01:06:13 -0400 Subject: seedrng: fix up exit path and style A few days of cooking have yielded a few trivial cleanups. --- seedrng.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/seedrng.c b/seedrng.c index 5b8533e..2bc7521 100644 --- a/seedrng.c +++ b/seedrng.c @@ -251,8 +251,7 @@ static int read_new_seed(uint8_t *seed, size_t len, bool *is_creditable) if (ret == (ssize_t)len) { *is_creditable = true; return 0; - } - if (ret == -1 && errno == ENOSYS) { + } else if (ret < 0 && errno == ENOSYS) { struct pollfd random_fd = { .fd = open("/dev/random", O_RDONLY), .events = POLLIN @@ -363,7 +362,7 @@ int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused))) { static const char seedrng_prefix[] = "SeedRNG v1 Old+New Prefix"; static const char seedrng_failure[] = "SeedRNG v1 No New Seed Failure"; - int ret, fd, lock, program_ret = 0; + int ret, fd = -1, lock, program_ret = 0; uint8_t new_seed[MAX_SEED_LEN]; size_t new_seed_len; bool new_seed_creditable; @@ -391,7 +390,8 @@ int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused))) lock = open(LOCK_FILE, O_WRONLY | O_CREAT, 0000); if (lock < 0 || flock(lock, LOCK_EX) < 0) { fprintf(stderr, "ERROR: Unable to open lock file: %s\n", strerror(errno)); - return 1; + program_ret = 1; + goto out; } ret = seed_from_file_if_exists(NON_CREDITABLE_SEED, false, &hash); @@ -430,7 +430,9 @@ int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused))) program_ret |= 1 << 6; } out: - close(fd); - close(lock); + if (fd >= 0) + close(fd); + if (lock >= 0) + close(lock); return program_ret; } -- cgit 1.4.1