From 0ba80dd8b3c2d688406cae448ffc109c4e773858 Mon Sep 17 00:00:00 2001 From: Marco d'Itri Date: Sat, 13 Sep 2014 02:40:40 +0200 Subject: [PATCH] mkpasswd: correctly report read(2) failures They would always be reported as a short read. --- mkpasswd.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/mkpasswd.c b/mkpasswd.c index 8e56323..bbd155d 100644 --- a/mkpasswd.c +++ b/mkpasswd.c @@ -354,7 +354,7 @@ int main(int argc, char *argv[]) void* get_random_bytes(const unsigned int count) { char *buf; - int fd; + int fd, bytes_read; buf = NOFAIL(malloc(count)); fd = open(RANDOM_DEVICE, O_RDONLY); @@ -362,11 +362,13 @@ void* get_random_bytes(const unsigned int count) perror("open(" RANDOM_DEVICE ")"); exit(2); } - if (read(fd, buf, count) != count) { - if (count < 0) - perror("read(" RANDOM_DEVICE ")"); - else - fprintf(stderr, "Short read of %s.\n", RANDOM_DEVICE); + bytes_read = read(fd, buf, count); + if (bytes_read < 0) { + perror("read(" RANDOM_DEVICE ")"); + exit(2); + } + if (bytes_read != count) { + fprintf(stderr, "Short read of %s.\n", RANDOM_DEVICE); exit(2); } close(fd);