changeset 14767:ff7953936223

perror: Avoid clobbering the strerror buffer when possible. * lib/strerror-impl.h: New file, extracted from lib/strerror.c. * lib/strerror.c: Include it. * modules/strerror (Files): Add lib/strerror-impl.h. * lib/perror.c: Include <stdlib.h>, intprops.h, verify.h. (my_strerror): New function, defined through lib/strerror-impl.h. (perror): Use it instead of strerror. * modules/perror (Files): Add lib/strerror-impl.h. (Depends-on): Remove strerror. Add intprops, verify, strerror_r-posix.
author Bruno Haible <bruno@clisp.org>
date Thu, 19 May 2011 21:49:01 +0200
parents 3618a75b519d
children a12deda870d9
files ChangeLog lib/perror.c lib/strerror-impl.h lib/strerror.c modules/perror modules/strerror
diffstat 6 files changed, 75 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2011-05-19  Bruno Haible  <bruno@clisp.org>
+
+	perror: Avoid clobbering the strerror buffer when possible.
+	* lib/strerror-impl.h: New file, extracted from lib/strerror.c.
+	* lib/strerror.c: Include it.
+	* modules/strerror (Files): Add lib/strerror-impl.h.
+	* lib/perror.c: Include <stdlib.h>, intprops.h, verify.h.
+	(my_strerror): New function, defined through lib/strerror-impl.h.
+	(perror): Use it instead of strerror.
+	* modules/perror (Files): Add lib/strerror-impl.h.
+	(Depends-on): Remove strerror. Add intprops, verify, strerror_r-posix.
+
 2011-05-19  Eric Blake  <eblake@redhat.com>
 
 	strerror_r: fix on newer cygwin
--- a/lib/perror.c
+++ b/lib/perror.c
@@ -21,12 +21,26 @@
 #include <stdio.h>
 
 #include <errno.h>
+#include <stdlib.h>
 #include <string.h>
 
+#include "intprops.h"
+#include "verify.h"
+
+/* Use the system functions, not the gnulib overrides in this file.  */
+#undef sprintf
+
+/* my_strerror (errnum) is equivalent to strerror (errnum).
+   But it uses its own buffer, not the one from strerror().  */
+#define STATIC static
+#undef strerror
+#define strerror my_strerror
+#include "strerror-impl.h"
+
 void
 perror (const char *string)
 {
-  const char *errno_description = strerror (errno);
+  const char *errno_description = my_strerror (errno);
 
   if (string != NULL && *string != '\0')
     fprintf (stderr, "%s: %s\n", string, errno_description);
new file mode 100644
--- /dev/null
+++ b/lib/strerror-impl.h
@@ -0,0 +1,41 @@
+/* strerror-impl.h --- Implementation of POSIX compatible strerror() function.
+
+   Copyright (C) 2007-2011 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifdef STATIC
+STATIC
+#endif
+char *
+strerror (int n)
+{
+  static char buf[256];
+
+  int ret = strerror_r (n, buf, sizeof (buf));
+
+  if (ret == 0)
+    return buf;
+
+  if (ret == ERANGE)
+    /* If this happens, increase the size of buf.  */
+    abort ();
+
+  {
+    static char const fmt[] = "Unknown error (%d)";
+    verify (sizeof (buf) >= sizeof (fmt) + INT_STRLEN_BOUND (n));
+    sprintf (buf, fmt, n);
+    return buf;
+  }
+}
--- a/lib/strerror.c
+++ b/lib/strerror.c
@@ -32,26 +32,6 @@
 /* Use the system functions, not the gnulib overrides in this file.  */
 # undef sprintf
 
-char *
-strerror (int n)
-{
-  static char buf[256];
-
-  int ret = strerror_r (n, buf, sizeof (buf));
-
-  if (ret == 0)
-    return buf;
-
-  if (ret == ERANGE)
-    /* If this happens, increase the size of buf.  */
-    abort ();
-
-  {
-    static char const fmt[] = "Unknown error (%d)";
-    verify (sizeof (buf) >= sizeof (fmt) + INT_STRLEN_BOUND (n));
-    sprintf (buf, fmt, n);
-    return buf;
-  }
-}
+# include "strerror-impl.h"
 
 #endif
--- a/modules/perror
+++ b/modules/perror
@@ -3,12 +3,15 @@
 
 Files:
 lib/perror.c
+lib/strerror-impl.h
 m4/perror.m4
 
 Depends-on:
 stdio
-errno           [test $REPLACE_PERROR = 1]
-strerror        [test $REPLACE_PERROR = 1]
+errno            [test $REPLACE_PERROR = 1]
+intprops         [test $REPLACE_PERROR = 1]
+verify           [test $REPLACE_PERROR = 1]
+strerror_r-posix [test $REPLACE_PERROR = 1]
 
 configure.ac:
 gl_FUNC_PERROR
--- a/modules/strerror
+++ b/modules/strerror
@@ -3,6 +3,7 @@
 
 Files:
 lib/strerror.c
+lib/strerror-impl.h
 m4/strerror.m4
 
 Depends-on: