diff options
author | Zack Weinberg <zackw@panix.com> | 2017-04-01 10:17:44 -0400 |
---|---|---|
committer | Zack Weinberg <zackw@panix.com> | 2017-04-07 07:48:57 -0400 |
commit | 544ce845def4540de11d9484df888df94876b14e (patch) | |
tree | dbd8eae480b47ab43e70bafb636a83c1963abb86 /stdio-common | |
parent | getopt: fix fencepost error in ambiguous-W-option handling (diff) | |
download | glibc-544ce845def4540de11d9484df888df94876b14e.tar.gz glibc-544ce845def4540de11d9484df888df94876b14e.tar.bz2 glibc-544ce845def4540de11d9484df888df94876b14e.zip |
getopt: clean up error reporting
getopt can print a whole bunch of error messages, and when used
standalone (from gnulib) it uses fprintf to do that. But fprintf is a
cancellation point and getopt isn't, and also applying fprintf to a
stream in wide-character mode is not allowed.
glibc has an internal function called __fxprintf that writes a narrow
format string to a stream regardless of mode, but it only handles
ASCII format strings, and it's still a cancellation point. getopt's
messages are translated, so they might not be ASCII. So getopt has an
error message to an asprintf buffer, monkeys with internal flag bits
on stderr to disable cancellation, and then calls
__fxprintf(stderr, "%s", buffer). There isn't even a helper function,
the code is duplicated every time.
This patch fixes __fxprintf to handle arbitrary multibyte format
strings, and adds a variant __fxprintf_nocancel that does the same
thing but also isn't a cancellation point. (It still _works_ by
monkeying with internal flag bits on the FILE, but that's not really a
layering violation for code in stdio-common.) All of the #ifdef _LIBC
blocks can then be reduced to their standalone versions with a little
help from some macros at the top of the file.
I also wrote a test case to verify that getopt really isn't a
cancellation point, and I'm glad I did, because it found two bugs, one
of which wasn't even to do with cancellation (see previous patch).
* stdio-common/fxprintf.c (__fxprintf_nocancel): New function.
(locked_vfxprintf): New helper function. Handle arbitrary
multibyte strings, not just ASCII.
* include/stdio.h: Declare __fxprintf_nocancel.
* posix/getopt.c: When _LIBC is defined, define fprintf to
__fxprintf_nocancel, flockfile to _IO_flockfile, and
funlockfile to _IO_funlockfile. When neither _LIBC nor
_POSIX_THREAD_SAFE_FUNCTIONS is defined, define flockfile and
funlockfile as no-ops. (_getopt_internal_r): Remove all
internal #ifdef _LIBC blocks; the standalone error-printing
code can now be used for libc as well. Add an
flockfile/funlockfile pair around one case where the error
message is printed in several chunks. Don't use fputc.
* posix/tst-getopt-cancel.c: New test.
* posix/Makefile: Run it.
Diffstat (limited to 'stdio-common')
-rw-r--r-- | stdio-common/fxprintf.c | 78 |
1 files changed, 61 insertions, 17 deletions
diff --git a/stdio-common/fxprintf.c b/stdio-common/fxprintf.c index 5ed0bb033b..82d4f2e235 100644 --- a/stdio-common/fxprintf.c +++ b/stdio-common/fxprintf.c @@ -16,14 +16,50 @@ License along with the GNU C Library; if not, see <http://www.gnu.org/licenses/>. */ -#include <assert.h> -#include <ctype.h> #include <stdarg.h> #include <stdio.h> -#include <wchar.h> +#include <stdlib.h> #include <string.h> +#include <wchar.h> #include <libioP.h> +static int +locked_vfxprintf (FILE *fp, const char *fmt, va_list ap) +{ + if (_IO_fwide (fp, 0) <= 0) + return _IO_vfprintf (fp, fmt, ap); + + /* We must convert the narrow format string to a wide one. + Each byte can produce at most one wide character. */ + wchar_t *wfmt; + mbstate_t mbstate; + int res; + int used_malloc = 0; + size_t len = strlen (fmt) + 1; + + if (__glibc_unlikely (len > SIZE_MAX / sizeof (wchar_t))) + { + __set_errno (EOVERFLOW); + return -1; + } + if (__libc_use_alloca (len * sizeof (wchar_t))) + wfmt = alloca (len * sizeof (wchar_t)); + else if ((wfmt = malloc (len * sizeof (wchar_t))) == NULL) + return -1; + else + used_malloc = 1; + + memset (&mbstate, 0, sizeof mbstate); + res = __mbsrtowcs (wfmt, &fmt, len, &mbstate); + + if (res != -1) + res = _IO_vfwprintf (fp, wfmt, ap); + + if (used_malloc) + free (wfmt); + + return res; +} int __fxprintf (FILE *fp, const char *fmt, ...) @@ -33,23 +69,31 @@ __fxprintf (FILE *fp, const char *fmt, ...) va_list ap; va_start (ap, fmt); + _IO_flockfile (fp); - int res; - if (_IO_fwide (fp, 0) > 0) - { - size_t len = strlen (fmt) + 1; - wchar_t wfmt[len]; - for (size_t i = 0; i < len; ++i) - { - assert (isascii (fmt[i])); - wfmt[i] = fmt[i]; - } - res = __vfwprintf (fp, wfmt, ap); - } - else - res = _IO_vfprintf (fp, fmt, ap); + int res = locked_vfxprintf (fp, fmt, ap); + _IO_funlockfile (fp); va_end (ap); + return res; +} +int +__fxprintf_nocancel (FILE *fp, const char *fmt, ...) +{ + if (fp == NULL) + fp = stderr; + + va_list ap; + va_start (ap, fmt); + _IO_flockfile (fp); + int save_flags2 = ((_IO_FILE *)fp)->_flags2; + ((_IO_FILE *)fp)->_flags2 |= _IO_FLAGS2_NOTCANCEL; + + int res = locked_vfxprintf (fp, fmt, ap); + + ((_IO_FILE *)fp)->_flags2 = save_flags2; + _IO_funlockfile (fp); + va_end (ap); return res; } |