zsh-workers
 help / color / mirror / code / Atom feed
From: tharvik@gmail.com
To: zsh-workers@zsh.org
Subject: $RANDOM in function in pipe do not get reseeded
Date: Thu, 30 Mar 2017 13:25:23 +0200	[thread overview]
Message-ID: <20170330112523.GA28110@t420s.epfl.ch> (raw)

[-- Attachment #1: Type: text/plain, Size: 406 bytes --]

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

[-- Attachment #2: 0001-reseed-srand-when-forking.patch --]
[-- Type: text/x-diff, Size: 700 bytes --]

>From 9d9430cb430dd5a7ceaf498fbdfe197c0964e5eb Mon Sep 17 00:00:00 2001
From: tharvik <tharvik@users.noreply.github.com>
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


             reply	other threads:[~2017-03-30 11:25 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-30 11:25 tharvik [this message]
2017-03-30 13:25 ` Eric Cook

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170330112523.GA28110@t420s.epfl.ch \
    --to=tharvik@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).