Przeglądaj źródła

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

Read `/dev/urandom` without buffering to avoid taking more than we need.
Cristian Rodríguez 9 lat temu
rodzic
commit
f23f18ab68
1 zmienionych plików z 4 dodań i 2 usunięć
  1. 4 2
      Source/cmSystemTools.cxx

+ 4 - 2
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;