[PATCH] libstdc++: Handle strerror returning null
Jonathan Wakely
jwakely@redhat.com
Wed Jul 31 13:24:15 GMT 2024
As discussed a couple of weeks ago, I'm going to push this.
Tested x86_64-linux (where this #else isn't even used, but I checked it
does at least compile when the #if isn't true).
-- >8 --
The linux man page for strerror says that some systems return NULL for
an unknown error number. That violates the C and POSIX standards, but we
can esily handle it to avoid a segfault.
libstdc++-v3/ChangeLog:
* src/c++11/system_error.cc (strerror_string): Handle
non-conforming NULL return from strerror.
---
libstdc++-v3/src/c++11/system_error.cc | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/libstdc++-v3/src/c++11/system_error.cc b/libstdc++-v3/src/c++11/system_error.cc
index d01451ba1ef..38bc0446110 100644
--- a/libstdc++-v3/src/c++11/system_error.cc
+++ b/libstdc++-v3/src/c++11/system_error.cc
@@ -110,7 +110,11 @@ namespace
#else
string strerror_string(int err)
{
- return strerror(err); // XXX Not thread-safe.
+ auto str = strerror(err); // XXX Not thread-safe.
+ if (str) [[__likely__]]
+ return str;
+ // strerror should not return NULL, but some implementations do.
+ return "Unknown error";
}
#endif
--
2.45.2
More information about the Libstdc++
mailing list