comparison src/evalfunc.c @ 31363:0608de106485 v9.0.1015

patch 9.0.1015: without /dev/urandom srand() seed is too predictable Commit: https://github.com/vim/vim/commit/f0a9c004825ab686270ee57260652cce25e61049 Author: Yasuhiro Matsumoto <mattn.jp@gmail.com> Date: Mon Dec 5 21:55:55 2022 +0000 patch 9.0.1015: without /dev/urandom srand() seed is too predictable Problem: Without /dev/urandom srand() seed is too predictable. Solution: Use micro seconds and XOR with process ID. (Yasuhiro Matsumoto, closes #11656)
author Bram Moolenaar <Bram@vim.org>
date Mon, 05 Dec 2022 23:00:05 +0100
parents 56a2af8c0980
children 540e85ac14c9
comparison
equal deleted inserted replaced
31362:7fc63cd1c462 31363:0608de106485
8157 } 8157 }
8158 close(fd); 8158 close(fd);
8159 } 8159 }
8160 } 8160 }
8161 if (dev_urandom_state != OK) 8161 if (dev_urandom_state != OK)
8162 // Reading /dev/urandom doesn't work, fall back to time(). 8162 #endif
8163 #endif 8163 {
8164 *x = vim_time(); 8164 // Reading /dev/urandom doesn't work, fall back to:
8165 // - randombytes_random()
8166 // - reltime() or time()
8167 // - XOR with process ID
8168 #if defined(FEAT_SODIUM)
8169 if (sodium_init() >= 0)
8170 *x = randombytes_random();
8171 else
8172 #endif
8173 {
8174 #if defined(FEAT_RELTIME)
8175 proftime_T res;
8176 profile_start(&res);
8177 # if defined(MSWIN)
8178 *x = (UINT32_T)res.LowPart;
8179 # else
8180 *x = (UINT32_T)res.tv_usec;
8181 # endif
8182 #else
8183 *x = vim_time();
8184 #endif
8185 *x ^= mch_get_pid();
8186 }
8187 }
8165 } 8188 }
8166 8189
8167 #define ROTL(x, k) (((x) << (k)) | ((x) >> (32 - (k)))) 8190 #define ROTL(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
8168 #define SPLITMIX32(x, z) ( \ 8191 #define SPLITMIX32(x, z) ( \
8169 (z) = ((x) += 0x9e3779b9), \ 8192 (z) = ((x) += 0x9e3779b9), \