From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id A69123857715 for ; Thu, 27 Apr 2023 10:30:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A69123857715 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682591429; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=TUHs6+kK9I0ZUfZV8LvOZYr/7XRwBYEoKLcChfRIoZs=; b=aqEbYIT1W6elUzT7Wnrg7uZ+Ml2n7UUWtYXBPjMlb4BqSzj7HuiKhwQ42Pfh0XyTMy3ZO8 9bSIRV1B59IIjRro4FYBGXLj478xp4sYtfYG2SUTJivX30xygmE1ONq2473SpoLs/Wm61/ U/2K42bzcFoUnRJ54N0Ts/gyXj2y/uY= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-329-jfJL8J6rPQqSGwQE1lVcSg-1; Thu, 27 Apr 2023 06:30:28 -0400 X-MC-Unique: jfJL8J6rPQqSGwQE1lVcSg-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CC4651C051A5; Thu, 27 Apr 2023 10:30:27 +0000 (UTC) Received: from localhost (unknown [10.42.28.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id 82100492B03; Thu, 27 Apr 2023 10:30:27 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Make std::random_device throw std::system_error [PR105081] Date: Thu, 27 Apr 2023 11:30:26 +0100 Message-Id: <20230427103026.1725758-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Tested powerpc64le-linux. Pushed to trunk. -- >8 -- 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. --- 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; } } -- 2.40.0