From patchwork Thu Apr 27 10:30:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 88179 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp168958vqo; Thu, 27 Apr 2023 03:31:29 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ72NTzaP4L/OIR57mrIEiLqJFSIDVApx4eRBZo+vILGrgazNjnEHPXwa32HNwGaC/97Tjpo X-Received: by 2002:a17:907:3f04:b0:957:12b7:30b0 with SMTP id hq4-20020a1709073f0400b0095712b730b0mr1187089ejc.18.1682591489418; Thu, 27 Apr 2023 03:31:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682591489; cv=none; d=google.com; s=arc-20160816; b=ZPKoO/TQzFlFYNDUBjiZzq+q/9e0UHa4K+np+VN7rsyW3+mhXtV8Hlgon9P2/PZOgT 7p99emUn6KXgT0+DNfpAXbfq7ARUcnrFllxSYnbDBz2hmuAnyEIgtOenD9eYS/VgrPu6 UdMmV4LeokRQsthzlAC6VhX4WAjfQFWsxO/Vb8uigTkfP/WEH6rhtstqyDWtCjfkESMj uw6yFameA4fThfcrCAaxQYepri32tFxDoGFRdoLJq2A+Knin3CGUOYGIU8pLA7ApayUn kt16FKHBCb7UXFnJk/KZUp753szioW1qCgj3Hr/SB+Odp54zs51BjR86y6KEZNre2zSP N8NQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=TUHs6+kK9I0ZUfZV8LvOZYr/7XRwBYEoKLcChfRIoZs=; b=HIfiuY3mlGxhlfCv5cmEZlJqTENQMx7GQzxWFtsv5Mlzwv2HKgDRe1tGK22ddtQ74o c6+H+UNK9SHa39weMpeZqJqpeTlj2SpxtDLmU33Lo4/gviGJ530JrbaG53dO0JC+VSBr gFJDo9w5BVXq/ThWGLKVoFw7YAEyNUSvhyzF9gRgB5O1bpmaw/QOMdvjhzbrvHBZfMjB wxh5tD5BjvFByXEQ9CLhnL/TG4f/GXebXlrSSVqYBac9nEJWrS6OBDqrICTbhRQFYprV krYAoxYsLtgnPjf0jllHiA+0sknmlVx9ObvFJmKeqIiI1yQcocpFMPJlhJFtIVjGAMBZ GwkQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=QZgMqDp7; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id z19-20020a170906715300b009501e0d886csi11096440ejj.487.2023.04.27.03.31.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Apr 2023 03:31:29 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=QZgMqDp7; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E99473857728 for ; Thu, 27 Apr 2023 10:31:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E99473857728 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682591475; bh=TUHs6+kK9I0ZUfZV8LvOZYr/7XRwBYEoKLcChfRIoZs=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=QZgMqDp7J93E9ZNYDZ/RPXznLqmVup4dZtdmKcx+GXC9+KkPu1lhLkN10PGnnJmKj RhgAc2T84/IHCjWters+npeITczR5RBKwNHtyZhIbaC0CYP4OS5myoW3yx34Dj0pIA YIldFeSuDZnnlROH0jQN7JMaDbJJxYQA3DrTydrg= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 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) 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 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764325053198290511?= X-GMAIL-MSGID: =?utf-8?q?1764325053198290511?= 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; } }