diff mbox

libstdc++/65142 Check read() result in std::random_device.

Message ID 20150911134418.GK2631@redhat.com
State New
Headers show

Commit Message

Jonathan Wakely Sept. 11, 2015, 1:44 p.m. UTC
We should not silently ignore a failure to read from the random
device.

Tested powerpc64le-linux, committed to trunk. I'm going to commit this
to the gcc-5 branch too.
diff mbox

Patch

commit 2d2f7012dc3744dafef0de94dd845bd190253dbd
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Feb 20 17:29:50 2015 +0000

    Check read() result in std::random_device.
    
    	PR libstdc++/65142
    	* src/c++11/random.cc (random_device::_M_getval()): Check read result.

diff --git a/libstdc++-v3/src/c++11/random.cc b/libstdc++-v3/src/c++11/random.cc
index edf900f..1d102c7 100644
--- a/libstdc++-v3/src/c++11/random.cc
+++ b/libstdc++-v3/src/c++11/random.cc
@@ -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;
   }