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 2016-02-17 09:02:04 -05:00 committed by Brad King
parent b13a74b35b
commit f23f18ab68
1 changed files with 4 additions and 2 deletions

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;