From: Bruno Haible Date: Thu, 19 May 2011 19:49:01 +0000 (+0200) Subject: perror: Avoid clobbering the strerror buffer when possible. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=30f176b6e12f1a67070991ee7ac505f2fa8a605c;p=pspp 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 , 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. --- diff --git a/ChangeLog b/ChangeLog index f5e455b44f..8eaefefdd8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2011-05-19 Bruno Haible + + 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 , 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 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 #include +#include #include +#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 . */ + +#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: