Boost C++ Libraries: Ticket #10980: boost::random::random_device bug with /dev/random https://svn.boost.org/trac10/ticket/10980 <p> There is a bug in boost 1.56 random_device implementation where it can fail to read from /dev/random with an exception </p> <blockquote> <p> "boost::random_device: EOF while reading random-number pseudo-device /dev/random: Success" </p> </blockquote> <p> The boost random_device code attempts to read 4 bytes from /dev/random, gets fewer but &gt;0 bytes because /dev/random temporarily runs out of random data, and boost random_device incorrectly concludes that end of file has been reached and throws an exception, where it should just try to read more bytes. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/10980 Trac 1.4.3 Bastiaan Stougie <bastiaan.stougie@…> Thu, 29 Jan 2015 08:11:08 GMT <link>https://svn.boost.org/trac10/ticket/10980#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10980#comment:1</guid> <description> <p> Here's a patch: </p> <pre class="wiki"> diff --git a/libs/random/src/random_device.cpp b/libs/random/src/random_device.cpp --- a/libs/random/src/random_device.cpp +++ b/libs/random/src/random_device.cpp @@ -168,13 +168,17 @@ unsigned int next() { unsigned int result; - long sz = read(fd, reinterpret_cast&lt;char *&gt;(&amp;result), sizeof(result)); - if(sz == -1) - error("error while reading"); - else if(sz != sizeof(result)) { - errno = 0; - error("EOF while reading"); - } + size_t offset = 0; + do { + long sz = read(fd, reinterpret_cast&lt;char *&gt;(&amp;result) + offset, sizeof(result) - offset); + if(sz == -1) + error("error while reading"); + else if(sz == 0) { + errno = 0; + error("EOF while reading"); + } + offset += sz; + } while (offset &lt; sizeof(result)); return result; } </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Sat, 13 Feb 2016 20:58:09 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/10980#comment:2 https://svn.boost.org/trac10/ticket/10980#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Fixed in <a class="ext-link" href="https://github.com/boostorg/random/commit/2a04b0ba63aaa2b5240631bcad30d6a3f8b42f69"><span class="icon">​</span>https://github.com/boostorg/random/commit/2a04b0ba63aaa2b5240631bcad30d6a3f8b42f69</a> </p> Ticket