From f7d7905976d2db2497a0b2b87a73fc85e92ff808 Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Fri, 10 Jan 2014 21:06:07 -0500 Subject: [PATCH] libuuid: add error checking to syscalls in get_clock() Also remove redundant close() of state_fd, since the fclose() of state_f will result in the fd being closed. Addresses-Coverity-Id: #1049146 Addresses-Coverity-Id: #26092 Signed-off-by: "Theodore Ts'o" --- lib/uuid/gen_uuid.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/uuid/gen_uuid.c b/lib/uuid/gen_uuid.c index 1b23d112..22b45134 100644 --- a/lib/uuid/gen_uuid.c +++ b/lib/uuid/gen_uuid.c @@ -326,10 +326,12 @@ static int get_clock(uint32_t *clock_high, uint32_t *clock_low, state_fd = open("/var/lib/libuuid/clock.txt", O_RDWR|O_CREAT, 0660); (void) umask(save_umask); - state_f = fdopen(state_fd, "r+"); - if (!state_f) { - close(state_fd); - state_fd = -1; + if (state_fd >= 0) { + state_f = fdopen(state_fd, "r+"); + if (!state_f) { + close(state_fd); + state_fd = -1; + } } } fl.l_type = F_WRLCK; @@ -343,7 +345,6 @@ static int get_clock(uint32_t *clock_high, uint32_t *clock_low, if ((errno == EAGAIN) || (errno == EINTR)) continue; fclose(state_f); - close(state_fd); state_fd = -1; break; } @@ -412,7 +413,10 @@ try_again: } rewind(state_f); fl.l_type = F_UNLCK; - fcntl(state_fd, F_SETLK, &fl); + if (fcntl(state_fd, F_SETLK, &fl) < 0) { + fclose(state_f); + state_fd = -1; + } } *clock_high = clock_reg >> 32;