mirror of
https://sourceware.org/git/glibc.git
synced 2024-12-30 14:31:14 +00:00
5c112f1b62
Various glibc testcases use tmpnam in ways subject to race conditions (generate a temporary file name, then later open that file without O_EXCL). This patch fixes those tests to use mkstemp - generally a minimal local fix to use mkstemp instead of tmpnam, rather than a larger fix to use other testsuite infrastructure for temporary files. The unchanged use of tmpnam in posix/wordexp-test.c would fail safe in the event of a race (it's generating a name for use with mkdir rather than for a file to be opened for writing). Tested for x86_64. * grp/tst_fgetgrent.c: Include <unistd.h>. (main): Use mkstemp instead of tmpnam. * io/test-utime.c (main): Likewise. * posix/annexc.c (macrofile): Change to modifiable array. (get_null_defines): Use mkstemp instead of tmpnam. Do not remove macrofile here. * posix/bug-getopt1.c: Include <stdlib.h>. (do_test): Use mkstemp instead of tmpnam. * posix/bug-getopt2.c: Include <stdlib.h>. (do_test): Use mkstemp instead of tmpnam. * posix/bug-getopt3.c: Include <stdlib.h>. (do_test): Use mkstemp instead of tmpnam. * posix/bug-getopt4.c: Include <stdlib.h>. (do_test): Use mkstemp instead of tmpnam. * posix/bug-getopt5.c: Include <stdlib.h>. (do_test): Use mkstemp instead of tmpnam. * stdio-common/bug7.c: Include <stdlib.h> and <unistd.h>. (main): Use mkstemp instead of tmpnam. * stdio-common/tst-fdopen.c: Include <stdlib.h>. (main): Use mkstemp instead of tmpnam. * stdio-common/tst-ungetc.c: Include <stdlib.h>. (main): use mkstemp instead of tmpnam. * stdlib/isomac.c (macrofile): Change to modifiable array. (get_null_defines): Use mkstemp instead of tmpnam. Do not remove macrofile here.
85 lines
1.7 KiB
C
85 lines
1.7 KiB
C
/* BZ 11040 */
|
|
#include <getopt.h>
|
|
#include <unistd.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
static const struct option opts[] =
|
|
{
|
|
{ "alpha", no_argument, NULL, 'a' },
|
|
{ "beta", required_argument, NULL, 'b' },
|
|
{ NULL, 0, NULL, 0 }
|
|
};
|
|
|
|
static int
|
|
one_test (const char *fmt, int argc, char *argv[], int n, int expected[n],
|
|
int out[n])
|
|
{
|
|
optind = 1;
|
|
|
|
int res = 0;
|
|
for (int i = 0; i < n; ++i)
|
|
{
|
|
rewind (stderr);
|
|
if (ftruncate (fileno (stderr), 0) != 0)
|
|
{
|
|
puts ("cannot truncate file");
|
|
return 1;
|
|
}
|
|
|
|
int c = getopt_long (argc, argv, fmt, opts, NULL);
|
|
if (c != expected[i])
|
|
{
|
|
printf ("format '%s' test %d failed: expected '%c', got '%c'\n",
|
|
fmt, i, expected[i], c);
|
|
res = 1;
|
|
}
|
|
if ((ftell (stderr) != 0) != out[i])
|
|
{
|
|
printf ("format '%s' test %d failed: %sprinted to stderr\n",
|
|
fmt, i, out[i] ? "not " : "");
|
|
res = 1;
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
|
|
static int
|
|
do_test (void)
|
|
{
|
|
char fname[] = "/tmp/bug-getopt3.XXXXXX";
|
|
int fd = mkstemp (fname);
|
|
if (fd == -1)
|
|
{
|
|
printf ("mkstemp failed: %m\n");
|
|
return 1;
|
|
}
|
|
close (fd);
|
|
|
|
if (freopen (fname, "w+", stderr) == NULL)
|
|
{
|
|
puts ("cannot redirect stderr");
|
|
return 1;
|
|
}
|
|
|
|
remove (fname);
|
|
|
|
int ret = one_test ("ab:W;", 2,
|
|
(char *[2]) { (char *) "bug-getopt3", (char *) "-a;" },
|
|
2, (int [2]) { 'a', '?' }, (int [2]) { 0, 1 });
|
|
|
|
ret |= one_test ("ab:W;", 2,
|
|
(char *[2]) { (char *) "bug-getopt3", (char *) "-a:" }, 2,
|
|
(int [2]) { 'a', '?' }, (int [2]) { 0, 1 });
|
|
|
|
if (ret == 0)
|
|
puts ("all OK");
|
|
|
|
return ret;
|
|
}
|
|
|
|
#define TEST_FUNCTION do_test ()
|
|
#include "../test-skeleton.c"
|