libstdc++: Use strerror_r in std::generic_category()::message(int) [PR110133]
commit51f94778b45514992a716b0b2d7a87244e6f0018
authorJonathan Wakely <jwakely@redhat.com>
Fri, 3 Nov 2023 13:59:48 +0000 (3 13:59 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Sat, 4 Nov 2023 08:31:45 +0000 (4 08:31 +0000)
tree5953c1d8bdbcfd56a59d5a65c5ea0bd0ba3dfab7
parent6933c05ade7cf9872e2c953339a5d2867e81bdcb
libstdc++: Use strerror_r in std::generic_category()::message(int) [PR110133]

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
libstdc++-v3/src/c++11/system_error.cc