cmSystemTools: Avoid excess entropy consumption by RandomSeed (#15976)

Read `/dev/urandom` without buffering to avoid taking more than we need.
This commit is contained in:
Cristian Rodríguez authored and Brad King committed 2016-02-19 09:44:59 -05:00
1 parent b13a74b35b
commit f23f18ab68
1 file changed
+4 -2
+4 -2
View File
@@ -2183,8 +2183,10 @@ unsigned int cmSystemTools::RandomSeed()
} seed;
// Try using a real random source.
cmsys::ifstream fin("/dev/urandom");
if(fin && fin.read(seed.bytes, sizeof(seed)) &&
cmsys::ifstream fin;
fin.rdbuf()->pubsetbuf(0, 0); // Unbuffered read.
fin.open("/dev/urandom");
if(fin.good() && fin.read(seed.bytes, sizeof(seed)) &&
fin.gcount() == sizeof(seed))
{
return seed.integer;