libc6: fread(buf, 1, 1) from urandom consumes 4096 bytes of	entropy
- Subject: libc6: fread(buf, 1, 1) from urandom consumes 4096 bytes of	entropy
 
- From: jrnieder@gmail.com (Jonathan Nieder)
 
- Date: Wed, 3 Aug 2011 12:52:26 +0200
 
- Message-id: <[🔎] 20110803105226.GA3444@elie.Hillsp>
 
- In-reply-to: <201108030833.01525.philipp.marek@linbit.com>
 
- References: <20110801204809.23586.66023.reportbug@cacao>	<20110802141615.GB3397@elie>	<201108030833.01525.philipp.marek@linbit.com>
 
Philipp Marek wrote:
> I think that on sockets, pipes, character devices, and similar there should 
> be _no_ readahead.
On the contrary, on sockets and pipes, readahead is very useful.
Luckily buffering there works a little closer to what you would expect
--- read() returns early with what is available and fgetc happily
makes use of the result without reading again.
So I agree with Petr on this one.  How about this patch?  It will also
help on every other unix supporting /dev/random I know of.
Signed-off-by: Jonathan Nieder <jrnieder at gmail.com>
---
 src/c/num_rand.d            |    1 +
 src/gmp/tests/rand/findlc.c |    1 +
 2 files changed, 2 insertions(+), 0 deletions(-)
diff --git i/src/c/num_rand.d w/src/c/num_rand.d
index 61caa315..1394af36 100644
--- i/src/c/num_rand.d
+++ w/src/c/num_rand.d
@@ -69,6 +69,7 @@ init_random_state()
 	int j;
 #if !defined(ECL_MS_WINDOWS_HOST)
 	FILE *fp = fopen("/dev/urandom","r");
 	if (fp) {
+		setbuf(fp, NULL);
 		fread(mt, sizeof(*mt), MT_N, fp);
 		for (j=0; j < MT_N; j++){
diff --git i/src/gmp/tests/rand/findlc.c w/src/gmp/tests/rand/findlc.c
index c432095d..63f2bdae 100644
--- i/src/gmp/tests/rand/findlc.c
+++ w/src/gmp/tests/rand/findlc.c
@@ -78,6 +78,7 @@ mpz_true_random (mpz_t s, unsigned long int nbits)
 	{
 	  int nread;
 	  fs = fopen ("/dev/random", "r");
+	  setbuf(fs, NULL);
 	  nread = fread (c, 1, 1, fs);
 	  fclose (fs);
 	  if (nread != 0)
-- 
1.7.6
Reply to: