From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 60FBB3858D32; Thu, 27 Apr 2023 10:29:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 60FBB3858D32 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682591393; bh=0KBneP22/S+twMsDHuS2aK8y7LBlzd3Ls/QWRmEoRfs=; h=From:To:Subject:Date:From; b=XKcJaq4Ozcxei2/gAXhpM//E5f9TSeqiEdS3EfleHD4XbNx7QH3/4oyQZWc/n5Wqz QmBQGCrOO8XPO37PniyIgESYg9Vszow7LpTlATM+qYYJUxkZaPoOfynt9nOzyuGBIL DafHhRSA1AIds4mf8B976MFQap0Kz/sExbkGn3vo= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r14-289] libstdc++: Make std::random_device throw std::system_error [PR105081] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: d8842271ebf9a81128df9ae80e1d3b688749eac8 X-Git-Newrev: f9412cedd6c0e7417b30d9a80d3f45c8746223b4 Message-Id: <20230427102953.60FBB3858D32@sourceware.org> Date: Thu, 27 Apr 2023 10:29:53 +0000 (GMT) List-Id: https://gcc.gnu.org/g:f9412cedd6c0e7417b30d9a80d3f45c8746223b4 commit r14-289-gf9412cedd6c0e7417b30d9a80d3f45c8746223b4 Author: Jonathan Wakely Date: Wed Apr 26 15:23:57 2023 +0100 libstdc++: Make std::random_device throw std::system_error [PR105081] This changes std::random_device constructors to throw std::system_error (with EINVAL as the error code) when the constructor argument is invalid. We can also throw std::system_error when read(2) fails so that the exception includes the additional information provided by errno. As noted in the PR, this is consistent with libc++, and doesn't break any existing code which catches std::runtime_error, because those handlers will still catch std::system_error. libstdc++-v3/ChangeLog: PR libstdc++/105081 * src/c++11/random.cc (__throw_syserr): New function. (random_device::_M_init, random_device::_M_init_pretr1): Use new function for bad tokens. (random_device::_M_getval): Use new function for read errors. * testsuite/util/testsuite_random.h (random_device_available): Change catch handler to use std::system_error. Diff: --- libstdc++-v3/src/c++11/random.cc | 18 ++++++++++++------ libstdc++-v3/testsuite/util/testsuite_random.h | 3 ++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/libstdc++-v3/src/c++11/random.cc b/libstdc++-v3/src/c++11/random.cc index ed2db4aef57..daf934808cc 100644 --- a/libstdc++-v3/src/c++11/random.cc +++ b/libstdc++-v3/src/c++11/random.cc @@ -26,6 +26,7 @@ #define _CRT_RAND_S // define this before including to get rand_s #include +#include #ifdef _GLIBCXX_USE_C99_STDINT_TR1 @@ -94,6 +95,11 @@ namespace std _GLIBCXX_VISIBILITY(default) { namespace { + [[noreturn]] + inline void + __throw_syserr([[maybe_unused]] int e, [[maybe_unused]] const char* msg) + { _GLIBCXX_THROW_OR_ABORT(system_error(e, std::generic_category(), msg)); } + #if USE_RDRAND unsigned int __attribute__ ((target("rdrnd"))) @@ -365,9 +371,9 @@ namespace std _GLIBCXX_VISIBILITY(default) which = prng; #endif else - std::__throw_runtime_error( - __N("random_device::random_device(const std::string&):" - " unsupported token")); + std::__throw_syserr(EINVAL, __N("random_device::random_device" + "(const std::string&):" + " unsupported token")); #ifdef _GLIBCXX_USE_CRT_RAND_S if (which & rand_s) @@ -508,8 +514,8 @@ namespace std _GLIBCXX_VISIBILITY(default) char* endptr; seed = std::strtoul(nptr, &endptr, 0); if (*nptr == '\0' || *endptr != '\0') - std::__throw_runtime_error(__N("random_device::_M_init_pretr1" - "(const std::string&)")); + std::__throw_syserr(EINVAL, __N("random_device::_M_init_pretr1" + "(const std::string&)")); } _M_mt.seed(seed); #else @@ -582,7 +588,7 @@ namespace std _GLIBCXX_VISIBILITY(default) p = static_cast(p) + e; } else if (e != -1 || errno != EINTR) - __throw_runtime_error(__N("random_device could not be read")); + __throw_syserr(errno, __N("random_device could not be read")); } while (n > 0); #else // USE_POSIX_FILE_IO diff --git a/libstdc++-v3/testsuite/util/testsuite_random.h b/libstdc++-v3/testsuite/util/testsuite_random.h index 840294d01e1..763707bbfac 100644 --- a/libstdc++-v3/testsuite/util/testsuite_random.h +++ b/libstdc++-v3/testsuite/util/testsuite_random.h @@ -26,6 +26,7 @@ #include #include +#include #include namespace __gnu_test @@ -204,7 +205,7 @@ namespace __gnu_test try { std::random_device dev(token); return true; - } catch (...) { + } catch (const std::system_error& /* See PR libstdc++/105081 */) { return false; } }