Patch 9.0.1015
Problem: Without /dev/urandom srand() seed is too predictable.
Solution: Use micro seconds and XOR with process ID. (Yasuhiro Matsumoto,
closes #11656)
Files: src/evalfunc.c, src/testdir/test_random.vim
*** ../vim-9.0.1014/src/evalfunc.c 2022-12-05 13:50:49.718052362 +0000
--- src/evalfunc.c 2022-12-05 21:52:38.207045205 +0000
***************
*** 8159,8167 ****
}
}
if (dev_urandom_state != OK)
- // Reading /dev/urandom doesn't work, fall back to time().
#endif
! *x = vim_time();
}
#define ROTL(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
--- 8159,8190 ----
}
}
if (dev_urandom_state != OK)
#endif
! {
! // Reading /dev/urandom doesn't work, fall back to:
! // - randombytes_random()
! // - reltime() or time()
! // - XOR with process ID
! #if defined(FEAT_SODIUM)
! if (sodium_init() >= 0)
! *x = randombytes_random();
! else
! #endif
! {
! #if defined(FEAT_RELTIME)
! proftime_T res;
! profile_start(&res);
! # if defined(MSWIN)
! *x = (UINT32_T)res.LowPart;
! # else
! *x = (UINT32_T)res.tv_usec;
! # endif
! #else
! *x = vim_time();
! #endif
! *x ^= mch_get_pid();
! }
! }
}
#define ROTL(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
*** ../vim-9.0.1014/src/testdir/test_random.vim 2022-09-17 21:07:52.107993141 +0100
--- src/testdir/test_random.vim 2022-12-05 21:55:10.487030631 +0000
***************
*** 1,5 ****
--- 1,8 ----
" Tests for srand() and rand()
+ source check.vim
+ source shared.vim
+
func Test_Rand()
let r = srand(123456789)
call assert_equal([1573771921, 319883699,
2742014374, 1324369493], r)
***************
*** 44,47 ****
--- 47,66 ----
call rand()
endfunc
+ func Test_srand()
+ CheckNotGui
+
+ let cmd = GetVimCommand() .. ' -V -es -c "echo rand()" -c qa!'
+ let bad = 0
+ for _ in range(10)
+ echo cmd
+ let result1 = system(cmd)
+ let result2 = system(cmd)
+ if result1 ==# result2
+ let bad += 1
+ endif
+ endfor
+ call assert_inrange(0, 4, bad)
+ endfunc
+
" vim: shiftwidth=2 sts=2 expandtab
*** ../vim-9.0.1014/src/version.c 2022-12-05 21:40:33.159131913 +0000
--- src/version.c 2022-12-05 21:49:06.075066892 +0000
***************
*** 697,698 ****
--- 697,700 ----
{ /* Add new patch number below this line */
+ /**/
+ 1015,
/**/
--
A man is incomplete until he's married ... and then he's finished!
/// Bram Moolenaar -- Br...@Moolenaar.net --
http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features --
http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims --
http://ICCF-Holland.org ///