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.133.124]) by sourceware.org (Postfix) with ESMTPS id E40F13858D32 for ; Sat, 4 Nov 2023 08:44:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E40F13858D32 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 E40F13858D32 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 Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.1 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=ham 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 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 } -- 2.41.0