summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2011-05-19 21:49:01 +0200
committerBruno Haible <bruno@clisp.org>2011-05-19 21:49:01 +0200
commit30f176b6e12f1a67070991ee7ac505f2fa8a605c (patch)
treef2dbb7e3407b9365ff218a86524e637299892b0b
parentb79d34835799d78d05b99916bfc363850905601c (diff)
downloadgnulib-30f176b6e12f1a67070991ee7ac505f2fa8a605c.tar.gz
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.
-rw-r--r--ChangeLog12
-rw-r--r--lib/perror.c16
-rw-r--r--lib/strerror-impl.h41
-rw-r--r--lib/strerror.c22
-rw-r--r--modules/perror7
-rw-r--r--modules/strerror1
6 files changed, 75 insertions, 24 deletions
diff --git a/ChangeLog b/ChangeLog
index f5e455b44f..8eaefefdd8 100644
--- 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
diff --git a/lib/perror.c b/lib/perror.c
index 2c13a8941a..29af3c5a56 100644
--- 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);
diff --git a/lib/strerror-impl.h b/lib/strerror-impl.h
new file mode 100644
index 0000000000..6be2b2a8f3
--- /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;
+ }
+}
diff --git a/lib/strerror.c b/lib/strerror.c
index 6137552efd..f0e03df95d 100644
--- 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
diff --git a/modules/perror b/modules/perror
index d2147b6b66..af79a0d8e7 100644
--- a/modules/perror
+++ b/modules/perror
@@ -3,12 +3,15 @@ perror() function: print a message describing error code.
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
diff --git a/modules/strerror b/modules/strerror
index 6e4c9c5463..42a476caca 100644
--- a/modules/strerror
+++ b/modules/strerror
@@ -3,6 +3,7 @@ strerror() function: return string describing error code.
Files:
lib/strerror.c
+lib/strerror-impl.h
m4/strerror.m4
Depends-on: