glibc/posix/tst-getopt-cancel.c

286 lines
8.3 KiB
C
Raw Normal View History

/* Copyright (C) 2017-2023 Free Software Foundation, Inc.
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.
2017-04-01 14:17:44 +00:00
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
Prefer https to http for gnu.org and fsf.org URLs Also, change sources.redhat.com to sourceware.org. This patch was automatically generated by running the following shell script, which uses GNU sed, and which avoids modifying files imported from upstream: sed -ri ' s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g ' \ $(find $(git ls-files) -prune -type f \ ! -name '*.po' \ ! -name 'ChangeLog*' \ ! -path COPYING ! -path COPYING.LIB \ ! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \ ! -path manual/texinfo.tex ! -path scripts/config.guess \ ! -path scripts/config.sub ! -path scripts/install-sh \ ! -path scripts/mkinstalldirs ! -path scripts/move-if-change \ ! -path INSTALL ! -path locale/programs/charmap-kw.h \ ! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \ ! '(' -name configure \ -execdir test -f configure.ac -o -f configure.in ';' ')' \ ! '(' -name preconfigure \ -execdir test -f preconfigure.ac ';' ')' \ -print) and then by running 'make dist-prepare' to regenerate files built from the altered files, and then executing the following to cleanup: chmod a+x sysdeps/unix/sysv/linux/riscv/configure # Omit irrelevant whitespace and comment-only changes, # perhaps from a slightly-different Autoconf version. git checkout -f \ sysdeps/csky/configure \ sysdeps/hppa/configure \ sysdeps/riscv/configure \ sysdeps/unix/sysv/linux/csky/configure # Omit changes that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines git checkout -f \ sysdeps/powerpc/powerpc64/ppc-mcount.S \ sysdeps/unix/sysv/linux/s390/s390-64/syscall.S # Omit change that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
<https://www.gnu.org/licenses/>. */
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.
2017-04-01 14:17:44 +00:00
/* fprintf is a cancellation point, but getopt is not supposed to be a
cancellation point, even when it prints error messages. */
/* Note: getopt.h must be included first in this file, so we get the
GNU getopt rather than the POSIX one. */
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>
#include <support/support.h>
#include <support/temp_file.h>
#include <support/xthread.h>
#include <support/xunistd.h>
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.
2017-04-01 14:17:44 +00:00
static bool
check_stderr (bool expect_errmsg, FILE *stderr_trapped)
{
static char *lineptr = 0;
static size_t linesz = 0;
bool got_errmsg = false;
rewind (stderr_trapped);
while (getline (&lineptr, &linesz, stderr_trapped) > 0)
{
got_errmsg = true;
fputs (lineptr, stdout);
}
rewind (stderr_trapped);
xftruncate (fileno (stderr_trapped), 0);
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.
2017-04-01 14:17:44 +00:00
return got_errmsg == expect_errmsg;
}
struct test_short
{
const char *label;
const char *opts;
const char *const argv[8];
int argc;
bool expect_errmsg;
};
struct test_long
{
const char *label;
const char *opts;
const struct option longopts[4];
const char *const argv[8];
int argc;
bool expect_errmsg;
};
#define DEFINE_TEST_DRIVER(test_type, getopt_call) \
struct test_type##_tdata \
{ \
pthread_mutex_t *sync; \
const struct test_type *tcase; \
bool ok; \
}; \
\
static void * \
test_type##_threadproc (void *data) \
{ \
struct test_type##_tdata *tdata = data; \
const struct test_type *tc = tdata->tcase; \
\
xpthread_mutex_lock (tdata->sync); \
xpthread_mutex_unlock (tdata->sync); \
\
/* At this point, this thread has a cancellation pending. \
We should still be able to get all the way through a getopt \
loop without being cancelled. \
Setting optind to 0 forces getopt to reinitialize itself. */ \
optind = 0; \
opterr = 1; \
optopt = 0; \
while (getopt_call != -1) \
; \
tdata->ok = true; \
\
pthread_testcancel(); \
return 0; \
} \
\
static bool \
do_##test_type (const struct test_type *tcase, FILE *stderr_trapped) \
{ \
pthread_mutex_t sync; \
struct test_type##_tdata tdata; \
\
printf("begin: %s\n", tcase->label); \
\
xpthread_mutex_init (&sync, 0); \
xpthread_mutex_lock (&sync); \
\
tdata.sync = &sync; \
tdata.tcase = tcase; \
tdata.ok = false; \
\
pthread_t thr = xpthread_create (0, test_type##_threadproc, \
(void *)&tdata); \
xpthread_cancel (thr); \
xpthread_mutex_unlock (&sync); \
void *rv = xpthread_join (thr); \
\
xpthread_mutex_destroy (&sync); \
\
bool ok = true; \
if (!check_stderr (tcase->expect_errmsg, stderr_trapped)) \
{ \
ok = false; \
printf("FAIL: %s: stderr not as expected\n", tcase->label); \
} \
if (!tdata.ok) \
{ \
ok = false; \
printf("FAIL: %s: did not complete loop\n", tcase->label); \
} \
if (rv != PTHREAD_CANCELED) \
{ \
ok = false; \
printf("FAIL: %s: thread was not cancelled\n", tcase->label); \
} \
if (ok) \
printf ("pass: %s\n", tcase->label); \
return ok; \
}
DEFINE_TEST_DRIVER (test_short,
getopt (tc->argc, (char *const *)tc->argv, tc->opts))
DEFINE_TEST_DRIVER (test_long,
getopt_long (tc->argc, (char *const *)tc->argv,
tc->opts, tc->longopts, 0))
/* Caution: all option strings must begin with a '+' or '-' so that
getopt does not attempt to permute the argument vector (which is in
read-only memory). */
const struct test_short tests_short[] = {
{ "no errors",
"+ab:c", { "program", "-ac", "-b", "x", 0 }, 4, false },
{ "invalid option",
"+ab:c", { "program", "-d", 0 }, 2, true },
{ "missing argument",
"+ab:c", { "program", "-b", 0 }, 2, true },
{ 0 }
};
const struct test_long tests_long[] = {
{ "no errors (long)",
"+ab:c", { { "alpha", no_argument, 0, 'a' },
{ "bravo", required_argument, 0, 'b' },
{ "charlie", no_argument, 0, 'c' },
{ 0 } },
{ "program", "-a", "--charlie", "--bravo=x", 0 }, 4, false },
{ "invalid option (long)",
"+ab:c", { { "alpha", no_argument, 0, 'a' },
{ "bravo", required_argument, 0, 'b' },
{ "charlie", no_argument, 0, 'c' },
{ 0 } },
{ "program", "-a", "--charlie", "--dingo", 0 }, 4, true },
{ "unwanted argument",
"+ab:c", { { "alpha", no_argument, 0, 'a' },
{ "bravo", required_argument, 0, 'b' },
{ "charlie", no_argument, 0, 'c' },
{ 0 } },
{ "program", "-a", "--charlie=dingo", "--bravo=x", 0 }, 4, true },
{ "missing argument",
"+ab:c", { { "alpha", no_argument, 0, 'a' },
{ "bravo", required_argument, 0, 'b' },
{ "charlie", no_argument, 0, 'c' },
{ 0 } },
{ "program", "-a", "--charlie", "--bravo", 0 }, 4, true },
{ "ambiguous options",
"+uvw", { { "veni", no_argument, 0, 'u' },
{ "vedi", no_argument, 0, 'v' },
{ "veci", no_argument, 0, 'w' } },
{ "program", "--ve", 0 }, 2, true },
{ "no errors (long W)",
"+ab:cW;", { { "alpha", no_argument, 0, 'a' },
{ "bravo", required_argument, 0, 'b' },
{ "charlie", no_argument, 0, 'c' },
{ 0 } },
{ "program", "-a", "-W", "charlie", "-W", "bravo=x", 0 }, 6, false },
{ "missing argument (W itself)",
"+ab:cW;", { { "alpha", no_argument, 0, 'a' },
{ "bravo", required_argument, 0, 'b' },
{ "charlie", no_argument, 0, 'c' },
{ 0 } },
{ "program", "-a", "-W", "charlie", "-W", 0 }, 5, true },
{ "missing argument (W longopt)",
"+ab:cW;", { { "alpha", no_argument, 0, 'a' },
{ "bravo", required_argument, 0, 'b' },
{ "charlie", no_argument, 0, 'c' },
{ 0 } },
{ "program", "-a", "-W", "charlie", "-W", "bravo", 0 }, 6, true },
{ "unwanted argument (W longopt)",
"+ab:cW;", { { "alpha", no_argument, 0, 'a' },
{ "bravo", required_argument, 0, 'b' },
{ "charlie", no_argument, 0, 'c' },
{ 0 } },
{ "program", "-a", "-W", "charlie=dingo", "-W", "bravo=x", 0 }, 6, true },
{ "ambiguous options (W)",
"+uvwW;", { { "veni", no_argument, 0, 'u' },
{ "vedi", no_argument, 0, 'v' },
{ "veci", no_argument, 0, 'w' } },
{ "program", "-W", "ve", 0 }, 3, true },
{ 0 }
};
static int
do_test (void)
{
int stderr_trap = create_temp_file ("stderr", 0);
if (stderr_trap < 0)
{
perror ("create_temp_file");
return 1;
}
FILE *stderr_trapped = fdopen(stderr_trap, "r+");
if (!stderr_trapped)
{
perror ("fdopen");
return 1;
}
int old_stderr = dup (fileno (stderr));
if (old_stderr < 0)
{
perror ("dup");
return 1;
}
if (dup2 (stderr_trap, 2) < 0)
{
perror ("dup2");
return 1;
}
rewind (stderr);
bool success = true;
for (const struct test_short *tcase = tests_short; tcase->label; tcase++)
success = do_test_short (tcase, stderr_trapped) && success;
for (const struct test_long *tcase = tests_long; tcase->label; tcase++)
success = do_test_long (tcase, stderr_trapped) && success;
dup2 (old_stderr, 2);
close (old_stderr);
fclose (stderr_trapped);
return success ? 0 : 1;
}
#include <support/test-driver.c>