From d783ad76c7d0ec98da4a675f2cbb26fa65ec9b47 Mon Sep 17 00:00:00 2001 From: Kizuna-Meraki Date: Thu, 17 Feb 2022 21:27:01 +0100 Subject: [PATCH] Close file on error path. The file was only be closed when there was no error and was being left open when there was an error. By moving the close(fd) statement out of the if-clause, the file can be close regardless if there is an error or not. After the file is closed, it can be checked for errors. --- random_seed.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/random_seed.c b/random_seed.c index 4622979..7945824 100644 --- a/random_seed.c +++ b/random_seed.c @@ -237,13 +237,15 @@ static int get_dev_random_seed(int *seed) } ssize_t nread = read(fd, seed, sizeof(*seed)); + + close(fd); + if (nread != sizeof(*seed)) { fprintf(stderr, "error short read %s: %s", dev_random_file, strerror(errno)); return -1; } - close(fd); return 0; } -- 2.49.0