瀏覽代碼

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

David Carlier 2 年之前
父節點
當前提交
a5a53c12bf
共有 1 個文件被更改,包括 6 次插入3 次删除
  1. 6 3
      src/stdlib/SDL_malloc.c

+ 6 - 3
src/stdlib/SDL_malloc.c

@@ -2531,11 +2531,14 @@ static int init_mparams(void) {
       int fd;
       unsigned char buf[sizeof(size_t)];
       /* Try to use /dev/urandom, else fall back on using time */
-      if ((fd = open("/dev/urandom", O_RDONLY)) >= 0 &&
-          read(fd, buf, sizeof(buf)) == sizeof(buf)) {
-        s = *((size_t *) buf);
+      if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
+        s = 0;
+      } else {
+	s = read(fd, buf, sizeof(buf));
         close(fd);
       }
+      if (s == sizeof(buf))
+        s = *((size_t *)buf);
       else
 #endif /* USE_DEV_RANDOM */
         s = (size_t)(time(0) ^ (size_t)0x55555555U);