To: vim_dev@googlegroups.com Subject: Patch 8.1.2343 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.1.2343 Problem: Using time() for srand() is not very random. Solution: use /dev/urandom if available Files: src/evalfunc.c, src/testdir/test_random.vim *** ../vim-8.1.2342/src/evalfunc.c 2019-11-25 15:40:28.849612230 +0100 --- src/evalfunc.c 2019-11-26 12:14:53.058068739 +0100 *************** *** 7095,7104 **** static void f_srand(typval_T *argvars, typval_T *rettv) { if (rettv_list_alloc(rettv) == FAIL) return; if (argvars[0].v_type == VAR_UNKNOWN) ! list_append_number(rettv->vval.v_list, (varnumber_T)vim_time()); else { int error = FALSE; --- 7095,7139 ---- static void f_srand(typval_T *argvars, typval_T *rettv) { + static int dev_urandom_state = -1; // FAIL or OK once tried + if (rettv_list_alloc(rettv) == FAIL) return; if (argvars[0].v_type == VAR_UNKNOWN) ! { ! if (dev_urandom_state != FAIL) ! { ! int fd = open("/dev/urandom", O_RDONLY); ! struct { ! union { ! UINT32_T number; ! char bytes[sizeof(UINT32_T)]; ! } cont; ! } buf; ! ! // Attempt reading /dev/urandom. ! if (fd == -1) ! dev_urandom_state = FAIL; ! else ! { ! buf.cont.number = 0; ! if (read(fd, buf.cont.bytes, sizeof(UINT32_T)) ! != sizeof(UINT32_T)) ! dev_urandom_state = FAIL; ! else ! { ! dev_urandom_state = OK; ! list_append_number(rettv->vval.v_list, ! (varnumber_T)buf.cont.number); ! } ! close(fd); ! } ! ! } ! if (dev_urandom_state != OK) ! // Reading /dev/urandom doesn't work, fall back to time(). ! list_append_number(rettv->vval.v_list, (varnumber_T)vim_time()); ! } else { int error = FALSE; *************** *** 7107,7113 **** if (error) return; ! list_append_number(rettv->vval.v_list, x); } list_append_number(rettv->vval.v_list, 362436069); list_append_number(rettv->vval.v_list, 521288629); --- 7142,7148 ---- if (error) return; ! list_append_number(rettv->vval.v_list, (varnumber_T)x); } list_append_number(rettv->vval.v_list, 362436069); list_append_number(rettv->vval.v_list, 521288629); *** ../vim-8.1.2342/src/testdir/test_random.vim 2019-11-25 15:40:28.853612211 +0100 --- src/testdir/test_random.vim 2019-11-26 12:21:15.508414423 +0100 *************** *** 11,19 **** call test_settime(12341234) let s = srand() ! call assert_equal(s, srand()) ! call test_settime(12341235) ! call assert_notequal(s, srand()) call srand() let v = rand() --- 11,25 ---- call test_settime(12341234) let s = srand() ! if filereadable('/dev/urandom') ! " using /dev/urandom ! call assert_notequal(s, srand()) ! else ! " using time() ! call assert_equal(s, srand()) ! call test_settime(12341235) ! call assert_notequal(s, srand()) ! endif call srand() let v = rand() *************** *** 25,28 **** --- 31,36 ---- call assert_fails('echo rand([1, [2], 3, 4])', 'E475:') call assert_fails('echo rand([1, 2, [3], 4])', 'E475:') call assert_fails('echo rand([1, 2, 3, [4]])', 'E475:') + + call test_settime(0) endfunc *** ../vim-8.1.2342/src/version.c 2019-11-25 15:40:28.853612211 +0100 --- src/version.c 2019-11-26 12:22:24.704102512 +0100 *************** *** 739,740 **** --- 739,742 ---- { /* Add new patch number below this line */ + /**/ + 2343, /**/ -- hundred-and-one symptoms of being an internet addict: 122. You ask if the Netaholics Anonymous t-shirt you ordered can be sent to you via e-mail. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///