From patchwork Sat Nov 4 08:44:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 161558 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp1555674vqu; Sat, 4 Nov 2023 02:49:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEa1nYpiR/fnJ2ZJ+OaxPJqxAi21dytofV4ALsvLVqsrwbeWFeWBXmXJ/yNnESFjUAFUS/E X-Received: by 2002:a05:620a:4621:b0:76f:f64:58b4 with SMTP id br33-20020a05620a462100b0076f0f6458b4mr26043075qkb.31.1699091342633; Sat, 04 Nov 2023 02:49:02 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1699091342; cv=pass; d=google.com; s=arc-20160816; b=bR/520b4waRg4Dhaw7+fCj/+zIEB8d7SbB2A4og4vXDxBYNVc1+jftnpU0SDF19JXl mJF3tV0CtgStTZarOzOi9an4L/4Ornbn8kHliTiRRfncXQS8z2Lm/FSIcjnrsHlEyobK UGtqS/Eppxah8QRHtxXQxaY5tBKhCJj6Zzz/QGHWZudPQ36QuFRmfTrKVSvopViuuPmA Cf4PhW6t0DuRi0v/NrfsZWT1Gmjyam9AsJXTlOQuffaetAf1QBK82oHLJb7BaC9VfiKw PcPU7sgLmGbrcl42eQj/yFdy4M5UsZUTWC1jmGJ1e2z2B0fOoTtehCzR8XqcO8F+tCHX aKCw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=qZgsWWRH/u02pQR65T/74w2T6F8VGgZwAGb6n8tp2MM=; fh=sJ+2/4g29YdyXkoRrFZSpsL2zxijepB7X/1rB0LDDh8=; b=iCoUnJK0UzMFj95aMT7E/WSvXhfdUh1aKuOBWSE5fnmvFHou/ctG/2geKpGdJXOv2W sQIepLZf5EmQhgm+Ah9t/T1d3Vo8PTNMlDQxTSmTVw3rIW5XGfTVpIv8Jt2oGUXI91Ls 2fwNUE454ZgvyVSbnhCBMNQEdMHxRS1P1SAEWZg0eSist1tLJ2VC3sFvlUDqnhsZlWCl pC5kGhcBLhsCaATbTUx/QAKgl5gN0IPG2hF0wEPtHD94GjHxD6KkhStu+2sLZrGRvZnK XLuihRV7rPCydHZhu3QaWiqJLNHhtnoVftyf1HtngAnia6WOzGExTZfcHCIeZfH2WUgm +k/g== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MZXEiRzZ; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id j6-20020a05620a410600b0076ccac03bf4si2965010qko.54.2023.11.04.02.49.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Nov 2023 02:49:02 -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=@redhat.com header.s=mimecast20190719 header.b=MZXEiRzZ; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 09F28385E021 for ; Sat, 4 Nov 2023 08:45:13 +0000 (GMT) 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.133.124]) by sourceware.org (Postfix) with ESMTPS id EA7A13858C31 for ; Sat, 4 Nov 2023 08:44:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EA7A13858C31 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org EA7A13858C31 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699087486; cv=none; b=SnHHKbLOnwhy1oLg2CwaE6yQ8ibRvSjOvZ5XELPeV4Jw1YykbjBwqoCkhLR/XMePyb8DjHp0SbyVdZMRQezc+ln16sN6J8j6zbpbbEhoNbn3ltWqX9epZi2FLsVnoiQdz66IQXYbnA1QkmLqjT5nq1EOESDYNDN0+J7iix5ocUg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699087486; c=relaxed/simple; bh=Wd7xvQB52dYxjCjvnhl33Ix3uFfHYWFug2thfSxgXOs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=E6DYDGI6tY2gkuYyTKCFDy7a/QIV1DbJsMCXIlOHMgRRtDEnYel0uX+pDWe1DgW1Vo8jrWpPALTDmQEmApMwD7xNxA2qHl8ZmyflPd1/uJi0Wdwn6AZzmJ/dLoI2/iE4kK4c/Pe/1zGgvBqKzxAowHJrInFJMvPN8bxlreQI0FE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699087484; 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=qZgsWWRH/u02pQR65T/74w2T6F8VGgZwAGb6n8tp2MM=; b=MZXEiRzZPMLynO0QrVdA105HPMaKWWZkO4oViq6mQG4I4logwpfZqIrHvE/6Xe88+WZTPP U+q/ZMDqDEKiZinKTf/cRBJfioQJAlQv7qp4Ga7Ogqy8zFjo77/qkNIaW68FAbdD6+2Zqc KMdok5X9Y5At/iE6Kc0gToOR91XT+Fk= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-486-ziuzvbMdObK31n0d5mmAUA-1; Sat, 04 Nov 2023 04:44:43 -0400 X-MC-Unique: ziuzvbMdObK31n0d5mmAUA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3C1F380C343; Sat, 4 Nov 2023 08:44:43 +0000 (UTC) Received: from localhost (unknown [10.42.28.65]) by smtp.corp.redhat.com (Postfix) with ESMTP id 072AF40C6EC0; Sat, 4 Nov 2023 08:44:42 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Use strerror_r in std::generic_category()::message(int) [PR110133] Date: Sat, 4 Nov 2023 08:44:11 +0000 Message-ID: <20231104084442.3016638-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.8 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_H3, RCVD_IN_MSPIKE_WL, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781626403875199225 X-GMAIL-MSGID: 1781626403875199225 Tested x86_64-linux. Pushed to trunk. Probably worth backporting after some time on trunk. -- >8 -- Use strerror_r instead of strerror when available, due to the latter not being thread-safe. This is complicated by Glibc providing a GNU-specific strerror_r which is not compatible with POSIX strerror_r, so we need to dispatch on the return type. We can use the recently-added std::string::__resize_and_overwrite to write directly into the string buffer when possible. Because we estimate the initial buffer size we might end up with excess capacity in the returned std::string. We can slightly tweak the std::system_error constructors to make use of that excess capacity, so that in some cases we require fewer allocations to construct the std::system_error::what() string. libstdc++-v3/ChangeLog: PR libstdc++/110133 * include/std/system_error (system_error::system_error): Group arguments so that concatenation can reuse rvalue's capacity. * src/c++11/system_error.cc (strerror_string): New function. [_GLIBCXX_HAVE_STRERROR_R] (use_strerror_result): New functions. (generic_error_category::message): Use strerror_string. (system_error_category::message): Likewise. --- libstdc++-v3/include/std/system_error | 4 +- libstdc++-v3/src/c++11/system_error.cc | 78 +++++++++++++++++++++++--- 2 files changed, 72 insertions(+), 10 deletions(-) diff --git a/libstdc++-v3/include/std/system_error b/libstdc++-v3/include/std/system_error index d71abc9766b..e26472bb0bf 100644 --- a/libstdc++-v3/include/std/system_error +++ b/libstdc++-v3/include/std/system_error @@ -563,7 +563,7 @@ namespace __adl_only : runtime_error(__ec.message()), _M_code(__ec) { } system_error(error_code __ec, const string& __what) - : runtime_error(__what + ": " + __ec.message()), _M_code(__ec) { } + : runtime_error(__what + (": " + __ec.message())), _M_code(__ec) { } system_error(error_code __ec, const char* __what) : runtime_error(__what + (": " + __ec.message())), _M_code(__ec) { } @@ -576,7 +576,7 @@ namespace __adl_only _M_code(__v, __ecat) { } system_error(int __v, const error_category& __ecat, const string& __what) - : runtime_error(__what + ": " + error_code(__v, __ecat).message()), + : runtime_error(__what + (": " + error_code(__v, __ecat).message())), _M_code(__v, __ecat) { } #if __cplusplus >= 201103L diff --git a/libstdc++-v3/src/c++11/system_error.cc b/libstdc++-v3/src/c++11/system_error.cc index 748eee94168..876e2bb44fc 100644 --- a/libstdc++-v3/src/c++11/system_error.cc +++ b/libstdc++-v3/src/c++11/system_error.cc @@ -46,6 +46,74 @@ namespace { using std::string; +#if _GLIBCXX_HAVE_STRERROR_R + // Handle the result of POSIX strerror_r. + inline std::size_t + use_strerror_result(int res, char* buf, std::size_t bufsz, + std::size_t& nextbufsz) + { + if (res == 0) // Success. + return std::strlen(buf); + + if (res == ERANGE) // Buffer too small to hold result string. + { + nextbufsz = 2 * bufsz; + return 0; + } + // else res == EINVAL, unknown error. + if (*buf == '\0') // No result string written to buffer. + { + const char msg[] = "Unknown error"; + std::memcpy(buf, msg, sizeof(msg) - 1); + return sizeof(msg) - 1; + } + else // An "unknown error" string was already written to the buffer. + return std::strlen(buf); + } + + // Handle the result of GNU strerror_r. + inline std::size_t + use_strerror_result(char* res, char* buf, std::size_t bufsz, + std::size_t& nextbufsz) + { + if (res == buf) // Result string written to the buffer. + return std::strlen(res); + + // Static result string returned, must be copied to the buffer. + std::size_t len = std::strlen(res); + if (len <= bufsz) + { + std::strcpy(buf, res); + return len; + } + + // Reallocate and try again: + nextbufsz = len; + return 0; + } + + string strerror_string(int err) + { + // Estimate maximum length of strerror strings (including "Unknown error"). + // Any excess capacity here can be used by std::system_error constructors + // when concatenating strings. + std::size_t len = 60; + string s; + do + s.__resize_and_overwrite(len, [err, &len](char* p, std::size_t n) { + *p = '\0'; + return use_strerror_result(strerror_r(err, p, n), p, n, len); + }); + while (s.empty()); + return s; + } +#else + string strerror_string(int err) + { + return strerror(err); // XXX Not thread-safe. + } +#endif + template struct constant_init { @@ -66,11 +134,7 @@ namespace _GLIBCXX_DEFAULT_ABI_TAG string message(int i) const final - { - // XXX locale issues: how does one get or set loc. - // _GLIBCXX_HAVE_STRERROR_L, strerror_l(i, cloc) - return string(strerror(i)); - } + { return strerror_string(i); } // Override this to avoid a virtual call to default_error_condition(i). bool @@ -113,9 +177,7 @@ namespace } return string("Unknown error code"); #else - // XXX locale issues: how does one get or set loc. - // _GLIBCXX_HAVE_STRERROR_L, strerror_l(i, cloc) - return string(strerror(i)); + return strerror_string(i); #endif }