mirror of git://gcc.gnu.org/git/gcc.git
Make std::random_device retry after short reads
PR libstdc++/65142 * src/c++11/random.cc (random_device::_M_getval()): Retry after short reads. From-SVN: r227872
This commit is contained in:
parent
2eb57e545b
commit
a2b4d73daf
|
|
@ -1,5 +1,9 @@
|
||||||
2015-09-17 Jonathan Wakely <jwakely@redhat.com>
|
2015-09-17 Jonathan Wakely <jwakely@redhat.com>
|
||||||
|
|
||||||
|
PR libstdc++/65142
|
||||||
|
* src/c++11/random.cc (random_device::_M_getval()): Retry after short
|
||||||
|
reads.
|
||||||
|
|
||||||
* include/std/system_error (error_code::operator bool(),
|
* include/std/system_error (error_code::operator bool(),
|
||||||
error_condition::operator bool()): Remove redundant conditional
|
error_condition::operator bool()): Remove redundant conditional
|
||||||
expression.
|
expression.
|
||||||
|
|
|
||||||
|
|
@ -130,16 +130,26 @@ namespace std _GLIBCXX_VISIBILITY(default)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
result_type __ret;
|
result_type __ret;
|
||||||
|
void* p = &__ret;
|
||||||
|
size_t n = sizeof(result_type);
|
||||||
#ifdef _GLIBCXX_HAVE_UNISTD_H
|
#ifdef _GLIBCXX_HAVE_UNISTD_H
|
||||||
auto e = read(fileno(static_cast<FILE*>(_M_file)),
|
do
|
||||||
static_cast<void*>(&__ret), sizeof(result_type));
|
{
|
||||||
|
const int e = read(fileno(static_cast<FILE*>(_M_file)), p, n);
|
||||||
|
if (e > 0)
|
||||||
|
{
|
||||||
|
n -= e;
|
||||||
|
p = static_cast<char*>(p) + e;
|
||||||
|
}
|
||||||
|
else if (e != -1 || errno != EINTR)
|
||||||
|
__throw_runtime_error(__N("random_device could not be read"));
|
||||||
|
}
|
||||||
|
while (n > 0);
|
||||||
#else
|
#else
|
||||||
auto e = std::fread(static_cast<void*>(&__ret), sizeof(result_type),
|
const size_t e = std::fread(p, n, 1, static_cast<FILE*>(_M_file));
|
||||||
1, static_cast<FILE*>(_M_file));
|
if (e != 1)
|
||||||
|
__throw_runtime_error(__N("random_device could not be read"));
|
||||||
#endif
|
#endif
|
||||||
if (e != sizeof(result_type))
|
|
||||||
__throw_runtime_error(__N("random_device could not read enough bytes"));
|
|
||||||
|
|
||||||
return __ret;
|
return __ret;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue