Check read() result in std::random_device.
authorJonathan Wakely <jwakely@redhat.com>
Fri, 11 Sep 2015 13:44:26 +0000 (14:44 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Fri, 11 Sep 2015 13:44:26 +0000 (14:44 +0100)
PR libstdc++/65142
* src/c++11/random.cc (random_device::_M_getval()): Check read result.

From-SVN: r227687

libstdc++-v3/ChangeLog
libstdc++-v3/src/c++11/random.cc

index 42f6a54b94ebac1f30afa0ac2c277f6eed876851..b4618effe80d35505b189b5194a75c94b2208021 100644 (file)
@@ -1,3 +1,8 @@
+2015-09-11  Jonathan Wakely  <jwakely@redhat.com>
+
+       PR libstdc++/65142
+       * src/c++11/random.cc (random_device::_M_getval()): Check read result.
+
 2015-09-11  John Marino  <gnugcc@marino.st>
            Jonathan Wakely  <jwakely@redhat.com>
 
index edf900f6bb73729219ecb1807279e9ada4027854..1d102c755b2eb8fbeabb8d30bc0958a7715e4992 100644 (file)
@@ -130,13 +130,17 @@ namespace std _GLIBCXX_VISIBILITY(default)
 #endif
 
     result_type __ret;
+
 #ifdef _GLIBCXX_HAVE_UNISTD_H
-    read(fileno(static_cast<FILE*>(_M_file)),
-        static_cast<void*>(&__ret), sizeof(result_type));
+    auto e = read(fileno(static_cast<FILE*>(_M_file)),
+                 static_cast<void*>(&__ret), sizeof(result_type));
 #else
-    std::fread(static_cast<void*>(&__ret), sizeof(result_type),
-              1, static_cast<FILE*>(_M_file));
+    auto e = std::fread(static_cast<void*>(&__ret), sizeof(result_type),
+                       1, static_cast<FILE*>(_M_file));
 #endif
+    if (e != sizeof(result_type))
+      __throw_runtime_error(__N("random_device could not read enough bytes"));
+
     return __ret;
   }