mirror of
https://github.com/neovim/neovim.git
synced 2024-12-24 13:15:09 -07:00
refactor(eval): use uv_random() for init_srand() (#29575)
N/A patches for version.c: vim-patch:9.1.0518: initialize the random buffer can be improved vim-patch:9.1.0531: resource leak in mch_get_random()
This commit is contained in:
parent
9217e0d671
commit
3c53e8f785
@ -5883,42 +5883,20 @@ static void f_py3eval(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
|
||||
static void init_srand(uint32_t *const x)
|
||||
FUNC_ATTR_NONNULL_ALL
|
||||
{
|
||||
#ifndef MSWIN
|
||||
static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
|
||||
|
||||
if (dev_urandom_state != FAIL) {
|
||||
const int fd = os_open("/dev/urandom", O_RDONLY, 0);
|
||||
struct {
|
||||
union {
|
||||
uint32_t number;
|
||||
char bytes[sizeof(uint32_t)];
|
||||
} contents;
|
||||
uint8_t bytes[sizeof(uint32_t)];
|
||||
} buf;
|
||||
|
||||
// Attempt reading /dev/urandom.
|
||||
if (fd == -1) {
|
||||
dev_urandom_state = FAIL;
|
||||
} else {
|
||||
buf.contents.number = 0;
|
||||
if (read(fd, buf.contents.bytes, sizeof(uint32_t)) != sizeof(uint32_t)) {
|
||||
dev_urandom_state = FAIL;
|
||||
} else {
|
||||
dev_urandom_state = OK;
|
||||
*x = buf.contents.number;
|
||||
if (uv_random(NULL, NULL, buf.bytes, sizeof(buf.bytes), 0, NULL) == 0) {
|
||||
*x = buf.number;
|
||||
return;
|
||||
}
|
||||
os_close(fd);
|
||||
}
|
||||
}
|
||||
if (dev_urandom_state != OK) {
|
||||
// Reading /dev/urandom doesn't work, fall back to os_hrtime() XOR with process ID
|
||||
#endif
|
||||
// uncrustify:off
|
||||
|
||||
// The system's random number generator doesn't work,
|
||||
// fall back to os_hrtime() XOR with process ID
|
||||
*x = (uint32_t)os_hrtime();
|
||||
*x ^= (uint32_t)os_get_pid();
|
||||
#ifndef MSWIN
|
||||
}
|
||||
#endif
|
||||
// uncrustify:on
|
||||
}
|
||||
|
||||
static inline uint32_t splitmix32(uint32_t *const x)
|
||||
|
Loading…
Reference in New Issue
Block a user