aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/eval/funcs.c
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2024-07-05 18:39:06 +0800
committerGitHub <noreply@github.com>2024-07-05 18:39:06 +0800
commit3c53e8f78511d6db9a6c804e5a479ba38c33102d (patch)
tree56836d2381239742557357befe978d370c5da678 /src/nvim/eval/funcs.c
parent9217e0d671b790d39192181cb894cfec012f06a6 (diff)
downloadrneovim-3c53e8f78511d6db9a6c804e5a479ba38c33102d.tar.gz
rneovim-3c53e8f78511d6db9a6c804e5a479ba38c33102d.tar.bz2
rneovim-3c53e8f78511d6db9a6c804e5a479ba38c33102d.zip
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()
Diffstat (limited to 'src/nvim/eval/funcs.c')
-rw-r--r--src/nvim/eval/funcs.c48
1 files changed, 13 insertions, 35 deletions
diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c
index e28e46d057..c319bd8214 100644
--- a/src/nvim/eval/funcs.c
+++ b/src/nvim/eval/funcs.c
@@ -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;
- } 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;
- }
- 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
+ union {
+ uint32_t number;
+ uint8_t bytes[sizeof(uint32_t)];
+ } buf;
+
+ if (uv_random(NULL, NULL, buf.bytes, sizeof(buf.bytes), 0, NULL) == 0) {
+ *x = buf.number;
+ return;
}
-#endif
- // uncrustify:on
+
+ // 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();
}
static inline uint32_t splitmix32(uint32_t *const x)