From f23f18ab686faa0ce91486143469bb58753b0843 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristian=20Rodr=C3=ADguez?= Date: Wed, 17 Feb 2016 09:02:04 -0500 Subject: [PATCH] cmSystemTools: Avoid excess entropy consumption by RandomSeed (#15976) Read `/dev/urandom` without buffering to avoid taking more than we need. --- Source/cmSystemTools.cxx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index c9670faf0..9af54bffc 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -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;