Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
$RANDOM in function in pipe do not get reseeded
- X-seq: zsh-workers 40916
- From: tharvik@xxxxxxxxx
- To: zsh-workers@xxxxxxx
- Subject: $RANDOM in function in pipe do not get reseeded
- Date: Thu, 30 Mar 2017 13:25:23 +0200
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:mime-version:content-disposition :content-transfer-encoding:user-agent; bh=UgcC0o6wwnwX+NmhlEPF+7wrAaRv5XkGCFtnZVgKmnc=; b=PTDxSYk5T8e27PpUDJY72Dg2Y9GFw8dl9s96d3LFfPzQVH2PECU1xr4TbRsktdKd+Q 87BIS7Qou57Q36TCBsuweINklYVz3g/+gqXJvCQe0IdWXP0ET+BB7MUDoinUS297dTMP kkP5SVcdVBRswJXcqWieSbPjSd+jwoWqgYL7IZQhg36oVx6q9VjtsDozGBKd53Y4LJmM I5PwrcxyEd5ekCyAQwnlEL4xIQDN1LCvajQs16oUNK4m87pU7lXdxCjQTsPE0oMdA1dC AvcyG0R6eqYf+hJxnMYUpGbGNojoiOsU9U3LlmTUzaStQ9+9ZZmozUS5Xp54Vr18a6WO biig==
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
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