Code

sstrerror: Be even more cautios with the return value of `strerror_r'..
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Mon, 3 Sep 2007 12:44:53 +0000 (14:44 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Mon, 3 Sep 2007 12:44:53 +0000 (14:44 +0200)
I don't trust this GNU implementation one bit.. ;)

src/common.c

index 7555b7ef99d26150d81193ce7e1d4d1a8e3a4b33..951bae306e834d15f3b7d1e66fb217740bedaff0 100644 (file)
@@ -82,13 +82,23 @@ char *sstrerror (int errnum, char *buf, size_t buflen)
                temp = strerror_r (errnum, buf, buflen);
                if (buf[0] == '\0')
                {
-                       strncpy (buf, temp, buflen);
+                       if ((temp != NULL) && (temp != buf) && (temp[0] != '\0'))
+                               strncpy (buf, temp, buflen);
+                       else
+                               strncpy (buf, "strerror_r did not return "
+                                               "an error message", buflen);
                        buf[buflen - 1] = '\0';
                }
        }
 #else
-       strerror_r (errnum, buf, buflen);
+       if (strerror_r (errnum, buf, buflen) != 0)
+       {
+               snprintf (buf, buflen, "Error #%i; "
+                               "Additionally, strerror_r failed.",
+                               errnum);
+       }
 #endif /* STRERROR_R_CHAR_P */
+       buf[buflen - 1] = '\0';
        return (buf);
 } /* char *sstrerror */