mkpasswd: correctly report read(2) failures

They would always be reported as a short read.
This commit is contained in:
Marco d'Itri 2014-09-13 02:40:40 +02:00
parent 9c0fbf514b
commit 0ba80dd8b3

View File

@ -354,7 +354,7 @@ int main(int argc, char *argv[])
void* get_random_bytes(const unsigned int count) void* get_random_bytes(const unsigned int count)
{ {
char *buf; char *buf;
int fd; int fd, bytes_read;
buf = NOFAIL(malloc(count)); buf = NOFAIL(malloc(count));
fd = open(RANDOM_DEVICE, O_RDONLY); fd = open(RANDOM_DEVICE, O_RDONLY);
@ -362,11 +362,13 @@ void* get_random_bytes(const unsigned int count)
perror("open(" RANDOM_DEVICE ")"); perror("open(" RANDOM_DEVICE ")");
exit(2); exit(2);
} }
if (read(fd, buf, count) != count) { bytes_read = read(fd, buf, count);
if (count < 0) if (bytes_read < 0) {
perror("read(" RANDOM_DEVICE ")"); perror("read(" RANDOM_DEVICE ")");
else exit(2);
fprintf(stderr, "Short read of %s.\n", RANDOM_DEVICE); }
if (bytes_read != count) {
fprintf(stderr, "Short read of %s.\n", RANDOM_DEVICE);
exit(2); exit(2);
} }
close(fd); close(fd);