]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Handle strerror returning null
authorJonathan Wakely <jwakely@redhat.com>
Wed, 31 Jul 2024 12:56:14 +0000 (13:56 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Wed, 31 Jul 2024 16:07:11 +0000 (17:07 +0100)
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

index d01451ba1ef68a58e16cc0607f304f0eb071079a..38bc04461101cb41d5e70a951d8897869930ec19 100644 (file)
@@ -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