[master] fb5477b Don't ignore the return value of the read(2) call on a random dev.

Poul-Henning Kamp phk at varnish-cache.org
Tue Sep 20 15:45:17 CEST 2011


commit fb5477bec8dad639f3db0bd2ac6900b3010a61a6
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Sep 20 13:44:59 2011 +0000

    Don't ignore the return value of the read(2) call on a random dev.

diff --git a/lib/libvarnishcompat/srandomdev.c b/lib/libvarnishcompat/srandomdev.c
index 79f774d..d0332c9 100644
--- a/lib/libvarnishcompat/srandomdev.c
+++ b/lib/libvarnishcompat/srandomdev.c
@@ -40,6 +40,22 @@
 
 #include "compat/srandomdev.h"
 
+static int
+trydev(const char *fn, unsigned long *seed)
+{
+	int fd;
+	ssize_t sz;
+
+	fd = open(fn, O_RDONLY);
+	if (fd < 0)
+		return (-1);
+	sz = read(fd, seed, sizeof *seed);
+	(void)close(fd);
+	if (sz != sizeof *seed)
+		return (-1);
+	return (0);
+}
+
 void
 srandomdev(void)
 {
@@ -47,13 +63,11 @@ srandomdev(void)
 	unsigned long seed;
 	int fd;
 
-	if ((fd = open("/dev/urandom", O_RDONLY)) >= 0 ||
-	    (fd = open("/dev/random", O_RDONLY)) >= 0) {
-		read(fd, &seed, sizeof seed);
-		close(fd);
-	} else {
-		gettimeofday(&tv, NULL);
-		seed = (getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec;
+	if (trydev("/dev/urandom", &seed)) {
+		if (trydev("/dev/random", &seed)) {
+			gettimeofday(&tv, NULL);
+			seed = (getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec;
+		}
 	}
 	srandom(seed);
 }



More information about the varnish-commit mailing list