Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

$RANDOM in function in pipe do not get reseeded



Hello,

There is some strange behavior with $RANDOM, because you don't
seed after forking.

For a testcase, here you go
--
Define a function echoing the value of $RANDOM
```
get_random() {
        echo "${RANDOM}"
}
```
Run it trough a pipe
```
get_random | cat
```
If you run it multiple times, the output is the same.
--

There is the patch linked, which fixes it.

Have a nice day!
-- 
Valérian Rousset
From 9d9430cb430dd5a7ceaf498fbdfe197c0964e5eb Mon Sep 17 00:00:00 2001
From: tharvik <tharvik@xxxxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 30 Mar 2017 11:55:58 +0200
Subject: [PATCH] reseed srand when forking

---
 Src/exec.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/Src/exec.c b/Src/exec.c
index 137130e..7f77252 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -312,9 +312,11 @@ zfork(struct timeval *tv)
 	return -1;
     }
 #ifdef HAVE_GETRLIMIT
-    if (!pid)
+    if (!pid) {
 	/* set resource limits for the child process */
 	setlimits(NULL);
+	srand((unsigned int)(tv->tv_sec + tv->tv_usec)); /* newly seed RANDOM in child */
+    }
 #endif
     return pid;
 }
-- 
2.10.2



Messages sorted by: Reverse Date, Date, Thread, Author