USE_DEV_RANDOM close the file descriptor even in the rare case it can't read it

This commit is contained in:
David Carlier authored and Ozkan Sezer committed 2023-02-26 14:00:00 +03:00
1 parent f18b5656f6
commit a5a53c12bf
1 file changed
+6 -3
+6 -3
View File
@@ -2531,11 +2531,14 @@ static int init_mparams(void) {
int fd; int fd;
unsigned char buf[sizeof(size_t)]; unsigned char buf[sizeof(size_t)];
/* Try to use /dev/urandom, else fall back on using time */ /* Try to use /dev/urandom, else fall back on using time */
if ((fd = open("/dev/urandom", O_RDONLY)) >= 0 && if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
read(fd, buf, sizeof(buf)) == sizeof(buf)) { s = 0;
s = *((size_t *) buf); } else {
s = read(fd, buf, sizeof(buf));
close(fd); close(fd);
} }
if (s == sizeof(buf))
s = *((size_t *)buf);
else else
#endif /* USE_DEV_RANDOM */ #endif /* USE_DEV_RANDOM */
s = (size_t)(time(0) ^ (size_t)0x55555555U); s = (size_t)(time(0) ^ (size_t)0x55555555U);