diff options
Diffstat (limited to 'stdio-common')
-rw-r--r-- | stdio-common/Makefile | 1 | ||||
-rw-r--r-- | stdio-common/tst-fclose-devzero.c | 50 | ||||
-rw-r--r-- | stdio-common/tst-setvbuf2.c | 39 |
3 files changed, 72 insertions, 18 deletions
diff --git a/stdio-common/Makefile b/stdio-common/Makefile index 3709222..64b3575 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -262,6 +262,7 @@ tests := \ tst-bz11319-fortify2 \ tst-cookie \ tst-dprintf-length \ + tst-fclose-devzero \ tst-fclose-offset \ tst-fdopen \ tst-fdopen2 \ diff --git a/stdio-common/tst-fclose-devzero.c b/stdio-common/tst-fclose-devzero.c new file mode 100644 index 0000000..1c7b39a --- /dev/null +++ b/stdio-common/tst-fclose-devzero.c @@ -0,0 +1,50 @@ +/* Test that always-zero lseek does not cause fclose failure after fread. + Copyright (C) 2025 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 <stdio.h> +#include <string.h> + +#include <support/check.h> +#include <support/xstdio.h> + +int +do_test (void) +{ + for (int do_ftello = 0; do_ftello < 2; ++do_ftello) + { + FILE *fp = xfopen ("/dev/zero", "r"); + char buf[17]; + memset (buf, 0xcc, sizeof (buf)); + xfread (buf, 1, sizeof (buf), fp); + static const char zeros[sizeof (buf)] = { 0 }; + TEST_COMPARE_BLOB (buf, sizeof (buf), zeros, sizeof (zeros)); + if (do_ftello) + { + errno = 0; + TEST_COMPARE (ftello (fp), -1); + TEST_COMPARE (errno, ESPIPE); + } + /* Do not use xfclose because it flushes first. */ + TEST_COMPARE (fclose (fp), 0); + } + + return 0; +} + +#include <support/test-driver.c> diff --git a/stdio-common/tst-setvbuf2.c b/stdio-common/tst-setvbuf2.c index 6cc8335..84d8b43 100644 --- a/stdio-common/tst-setvbuf2.c +++ b/stdio-common/tst-setvbuf2.c @@ -240,6 +240,21 @@ typedef struct { /* It's OK if this is static, we only run one at a time. */ ThreadData thread_data; +static void +end_thread (pthread_t *ptid) +{ + if (*ptid) + { + pthread_cancel (*ptid); + xpthread_join (*ptid); + /* The descriptor was passed in, or the helper thread made + sufficient progress and opened the file. */ + if (thread_data.fd >= 0) + xclose (thread_data.fd); + *ptid = 0; + } +} + static void * writer_thread_proc (void *closure) { @@ -306,7 +321,7 @@ static void start_writer_thread_n (const char *fname) { debug; - thread_data.fd = 0; + thread_data.fd = -1; thread_data.fname = fname; writer_thread_tid = xpthread_create (NULL, writer_thread_proc, (void *)&thread_data); @@ -316,13 +331,7 @@ static void end_writer_thread (void) { debug; - if (writer_thread_tid) - { - pthread_cancel (writer_thread_tid); - xpthread_join (writer_thread_tid); - xclose (thread_data.fd); - writer_thread_tid = 0; - } + end_thread (&writer_thread_tid); } static void @@ -339,7 +348,7 @@ static void start_reader_thread_n (const char *fname) { debug; - thread_data.fd = 0; + thread_data.fd = -1; thread_data.fname = fname; reader_thread_tid = xpthread_create (NULL, reader_thread_proc, (void *)&thread_data); @@ -349,13 +358,7 @@ static void end_reader_thread (void) { debug; - if (reader_thread_tid) - { - pthread_cancel (reader_thread_tid); - xpthread_join (reader_thread_tid); - xclose (thread_data.fd); - reader_thread_tid = 0; - } + end_thread (&reader_thread_tid); } /*------------------------------------------------------------*/ @@ -852,7 +855,7 @@ do_second_part (FILE *fp, } - fclose (fp); + xfclose (fp); return rv; } @@ -939,7 +942,7 @@ recurse (FILE *fp, break; default: /* parent */ - fclose (fp); + xfclose (fp); xwaitpid (pid, &status, 0); if (WIFEXITED (status) && WEXITSTATUS (status) == 0) |