mirror of
https://sourceware.org/git/glibc.git
synced 2024-11-27 07:20:11 +00:00
e0f3bf10ac
Add tests of special cases for freopen that were omitted from the more general tests of different modes and similar issues. The special cases in the three tests here are logically unconnected, it was simply convenient to put these tests in one patch. * Test freopen with a NULL path to the new file, in a chroot. Rather than asserting that this fails (logically, failure in this case is an implementation detail; it's not required for freopen to rely on /proc), verify that either it fails (without memory leaks) or that it succeeds and behaves as expected on success. There is no check for file descriptor leaks because the machinery for that also depends on /proc, so can't be used in a chroot. * Test that freopen and freopen64 are genuinely different in configurations with 32-bit off_t by checking for an EFBIG trying to write past 2GB in a file opened with freopen in such a configuration but no error with 64-bit off_t or when opening with freopen64. * Test freopen of stdin, stdout and stderr. Tested for x86_64 and x86.
145 lines
3.9 KiB
C
145 lines
3.9 KiB
C
/* Test freopen and freopen64 with large offsets.
|
|
Copyright (C) 2024 Free Software Foundation, Inc.
|
|
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
|
|
<https://www.gnu.org/licenses/>. */
|
|
|
|
#include <errno.h>
|
|
#include <mcheck.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include <support/check.h>
|
|
#include <support/descriptors.h>
|
|
#include <support/support.h>
|
|
#include <support/temp_file.h>
|
|
#include <support/test-driver.h>
|
|
#include <support/xstdio.h>
|
|
|
|
#define START_TEST(DESC) \
|
|
do \
|
|
{ \
|
|
fds = support_descriptors_list (); \
|
|
verbose_printf (DESC); \
|
|
} \
|
|
while (0)
|
|
|
|
#define END_TEST \
|
|
do \
|
|
{ \
|
|
support_descriptors_check (fds); \
|
|
support_descriptors_free (fds); \
|
|
} \
|
|
while (0)
|
|
|
|
int
|
|
do_test (void)
|
|
{
|
|
mtrace ();
|
|
struct support_descriptors *fds;
|
|
FILE *fp;
|
|
int ret;
|
|
|
|
char *temp_dir = support_create_temp_directory ("tst-freopen5");
|
|
/* This file is removed at the end of each test rather than left
|
|
around between tests to avoid problems with subsequent tests
|
|
reopening it as a large (2GB + 1 byte) file. */
|
|
char *file1 = xasprintf ("%s/file1", temp_dir);
|
|
|
|
/* fopen with freopen64: large offsets OK. */
|
|
START_TEST ("testing fopen with freopen64\n");
|
|
fp = fopen ("/dev/null", "r");
|
|
TEST_VERIFY_EXIT (fp != NULL);
|
|
fp = freopen64 (file1, "w", fp);
|
|
TEST_VERIFY_EXIT (fp != NULL);
|
|
setbuf (fp, NULL);
|
|
ret = fseeko64 (fp, 1LL << 32, SEEK_SET);
|
|
TEST_COMPARE (ret, 0);
|
|
ret = fputc ('x', fp);
|
|
TEST_COMPARE (ret, 'x');
|
|
xfclose (fp);
|
|
ret = remove (file1);
|
|
TEST_COMPARE (ret, 0);
|
|
END_TEST;
|
|
|
|
/* fopen64 with freopen64: large offsets OK. */
|
|
START_TEST ("testing fopen64 with freopen64\n");
|
|
fp = fopen64 ("/dev/null", "r");
|
|
TEST_VERIFY_EXIT (fp != NULL);
|
|
fp = freopen64 (file1, "w", fp);
|
|
TEST_VERIFY_EXIT (fp != NULL);
|
|
setbuf (fp, NULL);
|
|
ret = fseeko64 (fp, 1LL << 32, SEEK_SET);
|
|
TEST_COMPARE (ret, 0);
|
|
ret = fputc ('x', fp);
|
|
TEST_COMPARE (ret, 'x');
|
|
xfclose (fp);
|
|
ret = remove (file1);
|
|
TEST_COMPARE (ret, 0);
|
|
END_TEST;
|
|
|
|
/* fopen with freopen: large offsets not OK on 32-bit systems. */
|
|
START_TEST ("testing fopen with freopen\n");
|
|
fp = fopen ("/dev/null", "r");
|
|
TEST_VERIFY_EXIT (fp != NULL);
|
|
fp = freopen (file1, "w", fp);
|
|
TEST_VERIFY_EXIT (fp != NULL);
|
|
setbuf (fp, NULL);
|
|
ret = fseeko64 (fp, 1LL << 32, SEEK_SET);
|
|
TEST_COMPARE (ret, 0);
|
|
errno = 0;
|
|
ret = fputc ('x', fp);
|
|
if (sizeof (off_t) == 4)
|
|
{
|
|
TEST_COMPARE (ret, EOF);
|
|
TEST_COMPARE (errno, EFBIG);
|
|
}
|
|
else
|
|
TEST_COMPARE (ret, 'x');
|
|
fclose (fp);
|
|
ret = remove (file1);
|
|
TEST_COMPARE (ret, 0);
|
|
END_TEST;
|
|
|
|
/* fopen64 with freopen: large offsets not OK on 32-bit systems. */
|
|
START_TEST ("testing fopen64 with freopen\n");
|
|
fp = fopen64 ("/dev/null", "r");
|
|
TEST_VERIFY_EXIT (fp != NULL);
|
|
fp = freopen (file1, "w", fp);
|
|
TEST_VERIFY_EXIT (fp != NULL);
|
|
setbuf (fp, NULL);
|
|
ret = fseeko64 (fp, 1LL << 32, SEEK_SET);
|
|
TEST_COMPARE (ret, 0);
|
|
errno = 0;
|
|
ret = fputc ('x', fp);
|
|
if (sizeof (off_t) == 4)
|
|
{
|
|
TEST_COMPARE (ret, EOF);
|
|
TEST_COMPARE (errno, EFBIG);
|
|
}
|
|
else
|
|
TEST_COMPARE (ret, 'x');
|
|
fclose (fp);
|
|
ret = remove (file1);
|
|
TEST_COMPARE (ret, 0);
|
|
END_TEST;
|
|
|
|
free (temp_dir);
|
|
free (file1);
|
|
return 0;
|
|
}
|
|
|
|
#include <support/test-driver.c>
|