mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06: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)
|
static void init_srand(uint32_t *const x)
|
||||||
FUNC_ATTR_NONNULL_ALL
|
FUNC_ATTR_NONNULL_ALL
|
||||||
{
|
{
|
||||||
#ifndef MSWIN
|
union {
|
||||||
static int dev_urandom_state = NOTDONE; // FAIL or OK once tried
|
uint32_t number;
|
||||||
|
uint8_t bytes[sizeof(uint32_t)];
|
||||||
|
} buf;
|
||||||
|
|
||||||
if (dev_urandom_state != FAIL) {
|
if (uv_random(NULL, NULL, buf.bytes, sizeof(buf.bytes), 0, NULL) == 0) {
|
||||||
const int fd = os_open("/dev/urandom", O_RDONLY, 0);
|
*x = buf.number;
|
||||||
struct {
|
return;
|
||||||
union {
|
}
|
||||||
uint32_t number;
|
|
||||||
char bytes[sizeof(uint32_t)];
|
|
||||||
} contents;
|
|
||||||
} buf;
|
|
||||||
|
|
||||||
// Attempt reading /dev/urandom.
|
// The system's random number generator doesn't work,
|
||||||
if (fd == -1) {
|
// fall back to os_hrtime() XOR with process ID
|
||||||
dev_urandom_state = FAIL;
|
*x = (uint32_t)os_hrtime();
|
||||||
} else {
|
*x ^= (uint32_t)os_get_pid();
|
||||||
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;
|
|
||||||
}
|
|
||||||
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
|
|
||||||
*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)
|
static inline uint32_t splitmix32(uint32_t *const x)
|
||||||
|
Loading…
Reference in New Issue
Block a user