+2011-05-21 Eric Blake <eblake@redhat.com>
+
+ strerror_r: avoid clobbering strerror on cygwin
+ * lib/strerror_r.c (strerror_r): Don't use cygwin's strerror_r;
+ fall back instead to sys_errlist.
+ * modules/strerror (configure.ac): Add witness.
+ * tests/test-strerror_r.c (main): Enhance test.
+ * doc/posix-functions/strerror_r.texi (strerror_r): Document it.
+ * tests/test-perror2.c (main): Free memory before exit.
+
2011-05-21 Bruno Haible <bruno@clisp.org>
mkdtemp: Use gnulib naming conventions.
# define USE_XPG_STRERROR_R 1
-#elif HAVE_DECL_STRERROR_R && !(__GLIBC__ >= 2 || defined __UCLIBC__)
+#elif HAVE_DECL_STRERROR_R && !(__GLIBC__ >= 2 || defined __UCLIBC__ || defined __CYGWIN__)
/* The system's strerror_r function is OK, except that its third argument
is 'int', not 'size_t', or its return type is wrong. */
# define USE_SYSTEM_STRERROR_R 1
-#else /* (__GLIBC__ >= 2 || defined __UCLIBC__ ? !HAVE___XPG_STRERROR_R : !HAVE_DECL_STRERROR_R) */
+#else /* (__GLIBC__ >= 2 || defined __UCLIBC__ || defined __CYGWIN__ ? !HAVE___XPG_STRERROR_R : !HAVE_DECL_STRERROR_R) */
-/* Use the system's strerror(). */
+/* Use the system's strerror(). Exclude glibc and cygwin because the
+ system strerror_r has the wrong return type, and cygwin 1.7.9
+ strerror_r clobbers strerror. */
# undef strerror
# define USE_SYSTEM_STRERROR 1
-# if defined __NetBSD__ || defined __hpux || ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __sgi || (defined __sun && !defined _LP64)
+# if defined __NetBSD__ || defined __hpux || ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __sgi || (defined __sun && !defined _LP64) || defined __CYGWIN__
/* No locking needed. */
extern int sys_nerr;
# endif
-/* Get sys_nerr, sys_errlist on native Windows. */
+/* Get sys_nerr, sys_errlist on native Windows and Cygwin. */
# include <stdlib.h>
# else
else
ret = strerror_r (errnum, buf, buflen);
}
-# elif defined __CYGWIN__
- /* Cygwin <= 1.7.7 only provides the glibc interface, is thread-safe, and
- always succeeds (although it may truncate). In Cygwin >= 1.7.8, for
- valid errnum values, instead of truncating, it leaves the buffer
- untouched. */
- {
- char stackbuf[256];
-
- if (buflen < sizeof (stackbuf))
- {
- size_t len;
-
- stackbuf[0] = '\0'; /* in case strerror_r does nothing */
- strerror_r (errnum, stackbuf, sizeof (stackbuf));
- len = strlen (stackbuf);
- if (len < buflen)
- {
- memcpy (buf, stackbuf, len + 1);
- ret = 0;
- }
- else
- ret = ERANGE;
- }
- else
- {
- buf[0] = '\0'; /* in case strerror_r does nothing */
- strerror_r (errnum, buf, buflen);
- ret = 0;
- }
- }
# else
ret = strerror_r (errnum, buf, buflen);
# endif
/* Try to do what strerror (errnum) does, but without clobbering the
buffer used by strerror(). */
-# if defined __NetBSD__ || defined __hpux || ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) /* NetBSD, HP-UX, native Win32 */
+# if defined __NetBSD__ || defined __hpux || ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __CYGWIN__ /* NetBSD, HP-UX, native Win32, Cygwin */
/* NetBSD: sys_nerr, sys_errlist are declared through _NETBSD_SOURCE
and <errno.h> above.
HP-UX: sys_nerr, sys_errlist are declared explicitly above.
- native Win32: sys_nerr, sys_errlist are declared in <stdlib.h>. */
+ native Win32: sys_nerr, sys_errlist are declared in <stdlib.h>.
+ Cygwin: sys_nerr, sys_errlist are declared in <stdlib.h>. */
if (errnum >= 0 && errnum < sys_nerr)
{
# if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux)
ASSERT (errno == 0);
}
+#if GNULIB_STRERROR
+ /* Test that strerror_r does not clobber strerror buffer. On some
+ platforms, this test can only succeed if gnulib also replaces
+ strerror. */
+ {
+ const char *msg1;
+ const char *msg2;
+ const char *msg3;
+ const char *msg4;
+ char *str1;
+ char *str2;
+ char *str3;
+ char *str4;
+
+ msg1 = strerror (ENOENT);
+ ASSERT (msg1);
+ str1 = strdup (msg1);
+ ASSERT (str1);
+
+ msg2 = strerror (ERANGE);
+ ASSERT (msg2);
+ str2 = strdup (msg2);
+ ASSERT (str2);
+
+ msg3 = strerror (-4);
+ ASSERT (msg3);
+ str3 = strdup (msg3);
+ ASSERT (str3);
+
+ msg4 = strerror (1729576);
+ ASSERT (msg4);
+ str4 = strdup (msg4);
+ ASSERT (str4);
+
+ strerror_r (EACCES, buf, sizeof buf);
+ strerror_r (-5, buf, sizeof buf);
+ ASSERT (msg1 == msg2 || msg1 == msg4 || STREQ (msg1, str1));
+ ASSERT (msg2 == msg4 || STREQ (msg2, str2));
+ ASSERT (msg3 == msg4 || STREQ (msg3, str3));
+ ASSERT (STREQ (msg4, str4));
+
+ free (str1);
+ free (str2);
+ free (str3);
+ free (str4);
+ }
+#endif
+
return 0;
}