aboutsummaryrefslogtreecommitdiff
path: root/libio
AgeCommit message (Collapse)AuthorFilesLines
2025-09-05libio: Define AT_RENAME_* with the same tokens as LinuxFlorian Weimer1-3/+3
Linux uses different expressions for the RENAME_* and AT_RENAME_* constants. Mirror that in <stdio.h>, so that the macro redefinitions do not result in preprocessor warnings. Reviewed-by: Collin Funk <collin.funk1@gmail.com>
2025-09-05testsuite: Update tests for 'xfclose' useMaciej W. Rozycki1-1/+1
Convert (some) tests to use 'xfclose' rather than using plain 'fclose' call with no error checking or plain missing such a call. Reviewed-by: Florian Weimer <fweimer@redhat.com>
2025-09-05testsuite: Update tests for 'xfmemopen' useMaciej W. Rozycki1-2/+2
Convert tests to use 'xfmemopen' rather than open-coding error checks with 'fmemopen' or plain missing them, where 'fmemopen' itself is not the scope of testing. Leave 'fmemopen' tests alone. Reviewed-by: Florian Weimer <fweimer@redhat.com>
2025-08-22libio: Properly link in libio functions in static binariesH.J. Lu3-1/+39
commit 3020f72618e4f1d7338cd42b8bc7b2813e961b5a Author: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org> Date: Tue Dec 27 18:11:43 2022 -0300 libio: Remove the usage of __libc_IO_vtables added #define libio_static_fn_required(name) __asm (".globl " #name); to link in libio functions in static binaries. But there is no relocation in .globl _IO_file_open and "strip --strip-unneeded" will remove such unreferenced symbols which breaks static binaries. Redefine libio_static_fn_required to create a reference to the required function with static __typeof (name) *const name##_p __attribute__((used)) = name; This fixes BZ #33300. Signed-off-by: H.J. Lu <hjl.tools@gmail.com> Tested-by: Xi Ruoyao <xry111@xry111.site>
2025-06-17io: replace local_isatty() with a proper function __isatty_nostatus()H. Peter Anvin (Intel)1-11/+1
Replace local_isatty() inlined in libio with a proper function __isatty_nostatus(). This allows simpler system-specific implementations that don't need to touch errno at all. Note: I left the prototype in include/unistd.h (the internal header file.) It didn't much make sense to me to put it in a different header (not-cancel.h), but perhaps someone can elucidate the need. Add such an implementation for Linux, with a generic fallback. Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com> Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2025-04-22Add AT_* constants from Linux 6.12Joseph Myers1-0/+3
Linux 6.12 adds AT_RENAME_* aliases for RENAME_* flags for renameat2, and also AT_HANDLE_MNT_ID_UNIQUE. Add the first set of aliases to stdio.h alongside the RENAME_* names, and AT_HANDLE_MNT_ID_UNIQUE to bits/fcntl-linux.h. Tested for x86_64.
2025-04-14libio: Add test case for fflushFrédéric Bérat4-0/+208
Since one path uses _IO_SYNC and the other _IO_OVERFLOW, the newly added test cases verifies that `fflush (FILE)` and `fflush (NULL)` are semantically equivalent from the FILE perspective. Reviewed-by: Joseph Myers <josmyers@redhat.com>
2025-04-14libio: Synthesize ESPIPE error if lseek returns 0 after reading bytesFlorian Weimer1-0/+10
This is required so that fclose, when trying to seek to the right position after filling the input buffer, does not fail with EINVAL. This fclose code path only ignores ESPIPE errors. Reported by Petr Pisar on <https://bugzilla.redhat.com/show_bug.cgi?id=2358265>. Fixes commit be6818be31e756398e45f70e2819d78be0961223 ("Make fclose seek input file to right offset (bug 12724)"). Reviewed-by: Frédéric Bérat <fberat@redhat.com>
2025-03-03libio: Clean up fputc/putc commentsSamuel Zeter1-3/+0
Remove duplicate comments in stdio.h Signed-off-by: Samuel Zeter <samuelzeter@gmail.com> Reviewed-by: Arjun Shankar <arjun@redhat.com>
2025-02-13libio: Initialize _total_written for all kinds of streamsTulio Magno Quites Machado Filho2-1/+1
Move the initialization code to a general place instead of keeping it specific to file-backed streams. Fixes: 596a61cf6b (libio: Start to return errors when flushing fwrite's buffer [BZ #29459], 2025-01-28) Reported-by: Florian Weimer <fweimer@redhat.com> Reviewed-by: Arjun Shankar <arjun@redhat.com>
2025-02-05libio: Replace __LP64__ with __WORDSIZETulio Magno Quites Machado Filho1-2/+3
__LP64__ is a GCC extension and shouldn't be used in an installed header. Fixes: 596a61cf6b (libio: Start to return errors when flushing fwrite's buffer [BZ #29459], 2025-01-28) Reported-by: Florian Weimer <fweimer@redhat.com> Reviewed-by: Arjun Shankar <arjun@redhat.com>
2025-01-28Fix fflush handling for mmap files after ungetc (bug 32535)Joseph Myers1-4/+8
As discussed in bug 32535, fflush fails on files opened for reading using mmap after ungetc. Fix the logic to handle this case and still compute the file offset correctly. Tested for x86_64.
2025-01-28Fix fseek handling for mmap files after ungetc or fflush (bug 32529)Joseph Myers1-1/+8
As discussed in bug 32529, fseek fails on files opened for reading using mmap after ungetc. The implementation of fseek for such files has an offset computation that's also incorrect after fflush. A combined fix addresses both problems (with tests for both included as well) and it seems reasonable to consider them a single bug. Tested for x86_64.
2025-01-28Make fflush (NULL) flush input files (bug 32369)Joseph Myers1-0/+7
As discussed in bug 32369 and required by POSIX, the POSIX feature fflush (NULL) should flush input files, not just output files. The POSIX requirement is that "fflush() shall perform this flushing action on all streams for which the behavior is defined above", and the definition for input files is for "a stream open for reading with an underlying file description, if the file is not already at EOF, and the file is one capable of seeking". Implement this requirement in glibc. (The underlying flushing implementation is what deals with avoiding errors for seeking on an unseekable file.) Tested for x86_64.
2025-01-28Make fclose seek input file to right offset (bug 12724)Joseph Myers1-5/+38
As discussed in bug 12724 and required by POSIX, before an input file (based on an underlying seekable file descriptor) is closed, fclose is sometimes required to seek that file descriptor to the correct offset, so that any other file descriptors sharing the underlying open file description are left at that offset (as a motivating example, a script could call a sequence of commands each of which processes some data from (seekable) stdin using stdio; fclose needs to do this so that each successive command can read exactly the data not handled by previous commands), but glibc fails to do this. The precise POSIX wording has changed a few times; in the 2024 edition it's "If the file is not already at EOF, and the file is one capable of seeking, the file offset of the underlying open file description shall be set to the file position of the stream if the stream is the active handle to the underlying file description.". Add appropriate logic to _IO_new_file_close_it to handle this case. I haven't made any attempt to test or change things in this area for the "old" functions. Note that there was a previous attempt to fix bug 12724, reverted in commit eb6cbd249f4465b01f428057bf6ab61f5f0c07e3. The fix version here addresses the original test in that bug report without breaking the one given in a subsequent comment in that bug report (which works with glibc before the patch, but maybe was broken by the original fix that was reverted). The logic here tries to take care not to seek the file, even to its newly computed current offset, if at EOF / possibly not the active handle; even seeking to the current offset would be problematic because of a potential race (fclose computes the current offset, another thread or process with the active handle does its own seek, fclose does a seek (not permitted by POSIX in this case) that loses the effect of the seek on the active handle in another thread or process). There are tests included for various cases of being or not being the active handle, though there aren't tests for the potential race condition. Tested for x86_64.
2025-01-28Fix fflush after ungetc on input file (bug 5994)Joseph Myers1-0/+5
As discussed in bug 5994 (plus duplicates), POSIX requires fflush after ungetc to discard pushed-back characters but preserve the file position indicator. For this purpose, each ungetc decrements the file position indicator by 1; it is unspecified after ungetc at the start of the file, and after ungetwc, so no special handling is needed for either of those cases. This is fixed with appropriate logic in _IO_new_file_sync. I haven't made any attempt to test or change things in this area for the "old" functions; the case of files using mmap is addressed in a subsequent patch (and there seem to be no problems in this area with files opened with fmemopen). Tested for x86_64.
2025-01-28libio: Start to return errors when flushing fwrite's buffer [BZ #29459]Tulio Magno Quites Machado Filho3-6/+44
When an error happens, fwrite is expected to return a value that is less than nmemb. If this error happens while flushing its internal buffer, fwrite is in a complex scenario: all the data might have been written to the buffer, indicating a successful copy, but the buffer is expected to be flushed and it was not. POSIX.1-2024 states the following about errors on fwrite: If an error occurs, the resulting value of the file-position indicator for the stream is unspecified. The fwrite() function shall return the number of elements successfully written, which may be less than nitems if a write error is encountered. With that in mind, this commit modifies _IO_new_file_write in order to return the total number of bytes written via the file pointer. It also modifies fwrite in order to use the new information and return the correct number of bytes written even when sputn returns EOF. Add 2 tests: 1. tst-fwrite-bz29459: This test is based on the reproducer attached to bug 29459. In order to work, it requires to pipe stdout to another process making it hard to reuse test-driver.c. This code is more specific to the issue reported. 2. tst-fwrite-pipe: Recreates the issue by creating a pipe that is shared with a child process. Reuses test-driver.c. Evaluates a more generic scenario. Co-authored-by: Florian Weimer <fweimer@redhat.com> Reviewed-by: DJ Delorie <dj@redhat.com>
2025-01-01Update copyright dates with scripts/update-copyrightsPaul Eggert189-189/+189
2024-12-27libio: asprintf should write NULL upon failureFlorian Weimer3-9/+60
This was suggested most recently by Solar Designer, noting that code replacing vsprintf with vasprintf in a security fix was subtly wrong: Re: GStreamer Security Advisory 2024-0003: Orc compiler stack-based buffer overflow <https://www.openwall.com/lists/oss-security/2024/07/26/2> Previous libc-alpha discussions: I: [PATCH] asprintf error handling fix <https://inbox.sourceware.org/libc-alpha/20011205185828.GA8376@ldv.office.alt-linux.org/> asprintf() issue <https://inbox.sourceware.org/libc-alpha/CANSoFxt-cdc-+C4u-rTENMtY4X9RpRSuv+axDswSPxbDgag8_Q@mail.gmail.com/> I don't think we need a compatibility symbol for this. As the GStreamer example shows, this change is much more likely to fix bugs than cause compatibility issues. Suggested-by: Dmitry V. Levin <ldv@altlinux.org> Suggested-by: Archie Cobbs <archie.cobbs@gmail.com> Suggested-by: Solar Designer <solar@openwall.com> Reviewed-by: Sam James <sam@gentoo.org>
2024-12-23Check if TEST_CC supports -Wno-restrict before using itAdhemerval Zanella1-2/+4
Check if TEST_CC supports -Wno-restrict before using it to avoid Clang error: error: unknown warning option '-Wno-restrict' [-Werror,-Wunknown-warning-option] Signed-off-by: H.J. Lu <hjl.tools@gmail.com> Reviewed-by: Sam James <sam@gentoo.org>
2024-12-22Suppress Clang -Wgnu-folding-constant warningsH.J. Lu2-0/+8
Suppress Clang -Wgnu-folding-constant warnings, like tst-freopen.c:44:13: error: variable length array folded to constant array as an extension [-Werror,-Wgnu-folding-constant] 44 | char temp[strlen (test) + 1]; | ^~~~~~~~~~~~~~~~~ Signed-off-by: H.J. Lu <hjl.tools@gmail.com> Reviewed-by: Sam James <sam@gentoo.org>
2024-12-22Handle pragma GCC optimize for clangAdhemerval Zanella2-2/+10
Reviewed-by: Sam James <sam@gentoo.org>
2024-12-17ungetc: Guarantee single char pushbackSiddhesh Poyarekar6-23/+43
The C standard requires that ungetc guarantees at least one pushback, but the malloc call to allocate the pushback buffer could fail, thus violating that requirement. Fix this by adding a single byte pushback buffer in the FILE struct that the pushback can fall back to if malloc fails. The side-effect is that if the initial malloc fails and the 1-byte fallback buffer is used, future resizing (if it succeeds) will be 2-bytes, 4-bytes and so on, which is suboptimal but it's after a malloc failure, so maybe even desirable. A future optimization here could be to have the pushback code use the single byte buffer first and only fall back to malloc for subsequent calls. Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org> Reviewed-by: Maciej W. Rozycki <macro@redhat.com>
2024-12-17libio: Fix last NULL-as-0 issue in libioP.hSiddhesh Poyarekar1-1/+1
Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org> Reviewed-by: Maciej W. Rozycki <macro@redhat.com>
2024-12-17libio: Use NULL instead of 0 as a null pointer constantAlejandro Colomar1-4/+7
This was missed in a recent global change. Fixes: 53fcdf5f743a (2024-11-25, "Silence most -Wzero-as-null-pointer-constant diagnostics") Reported-by: "Maciej W. Rozycki" <macro@redhat.com> Cc: Siddhesh Poyarekar <siddhesh@sourceware.org> Cc: Bruno Haible <bruno@clisp.org> Cc: Martin Uecker <uecker@tugraz.at> Cc: Xi Ruoyao <xry111@xry111.site> Cc: Florian Weimer <fweimer@redhat.com> Cc: Joseph Myers <josmyers@redhat.com> Signed-off-by: Alejandro Colomar <alx@kernel.org> Reviewed-by: Maciej W. Rozycki <macro@redhat.com>
2024-11-28libio: make _IO_least_marker staticSiddhesh Poyarekar1-3/+1
Trivial cleanup to limit _IO_least_marker so that it's clear that it is unused outside of genops. Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
2024-11-25Silence most -Wzero-as-null-pointer-constant diagnosticsAlejandro Colomar6-9/+10
Replace 0 by NULL and {0} by {}. Omit a few cases that aren't so trivial to fix. Link: <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117059> Link: <https://software.codidact.com/posts/292718/292759#answer-292759> Signed-off-by: Alejandro Colomar <alx@kernel.org>
2024-10-25libio: Fix crash in fputws [BZ #20632]Peter Ammon4-4/+108
This fixes a buffer overflow in wide character string output, reproducing when output fails, such as if the output fd is closed or is redirected to a full device. Wide character output data attempts to maintain the invariant that `_IO_buf_base <= _IO_write_base <= _IO_write_end <= _IO_buf_end` (that is, that the write region is a sub-region of `_IO_buf`). Prior to this commit, this invariant is violated by the `_IO_wfile_overflow` function as so: 1. `_IO_wsetg` is called, assigning `_IO_write_base` to `_IO_buf_base` 2. `_IO_doallocbuf` is called, which jumps to `_IO_wfile_doallocate` via the _IO_wfile_jumps vtable. This function then assigns the wide data `_IO_buf_base` and `_IO_buf_end` to a malloc'd buffer. Thus the invariant is violated. The fix is simply to reverse the order: malloc the `_IO_buf` first and then assign `_IO_write_base` to it. We also take this opportunity to defensively guard the initialization of the number of unwritten characters via pointer arithmetic. We now check that the buffer end is not before the buffer beginning; this matches a similar defensive check in the narrow analogue `fileops.c`. Add a test which fails without the fix. Signed-off-by: Peter Ammon <corydoras@ridiculousfish.com> Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2024-10-25libio: Correctly link tst-popen-fork against libpthreadArjun Shankar1-0/+2
tst-popen-fork failed to build for Hurd due to not being linked with libpthread. This commit fixes that. Tested with build-many-glibcs.py for i686-gnu. Reviewed-by: Florian Weimer <fweimer@redhat.com>
2024-10-23libio: Fix a deadlock after fork in popenArjun Shankar4-0/+107
popen modifies its file handler book-keeping under a lock that wasn't being taken during fork. This meant that a concurrent popen and fork could end up copying the lock in a "locked" state into the fork child, where subsequently calling popen would lead to a deadlock due to the already (spuriously) held lock. This commit fixes the deadlock by appropriately taking the lock before fork, and releasing/resetting it in the parent/child after the fork. A new test for concurrent popen and fork is also added. It consistently hangs (and therefore fails via timeout) without the fix applied. Reviewed-by: Florian Weimer <fweimer@redhat.com>
2024-10-01libio: Set _vtable_offset before calling _IO_link_in [BZ #32148]H.J. Lu3-1/+95
Since _IO_vtable_offset is used to detect the old binaries, set it in _IO_old_file_init_internal before calling _IO_link_in which checks _IO_vtable_offset. Add a glibc 2.0 test with copy relocation on _IO_stderr_@GLIBC_2.0 to verify that fopen won't cause memory corruption. This fixes BZ #32148. Signed-off-by: H.J. Lu <hjl.tools@gmail.com> Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>
2024-09-20Add another test for fclose on an unopened fileAaron Merey4-3/+69
Add new file libio/tst-fclose-unopened2.c that tests whether fclose on an unopened file returns EOF. This test differs from tst-fclose-unopened.c by ensuring the file's buffer is allocated prior to double-fclose. A comment in tst-fclose-unopened.c now clarifies that it is testing a file with an unallocated buffer. Calling fclose on unopened files normally causes a use-after-free bug, however the standard streams are an exception since they are not deallocated by fclose. Tested for x86_64. Reviewed-by: Carlos O'Donell <carlos@redhat.com>
2024-09-05Fix freopen handling of ,ccs= (bug 23675)Joseph Myers2-6/+3
As reported in bug 23675 and shown up in the recently added tests of different cases of freopen (relevant part of the test currently conditioned under #if 0 to avoid a failure resulting from this bug), freopen wrongly forces the stream to unoriented even when a mode with ,ccs= is specified, though such a mode is supposed to result in a wide-oriented stream. Move the clearing of _mode to before the actual reopening occurs, so that the main fopen implementation can leave a wide-oriented stream in the ,ccs= case. Tested for x86_64.
2024-09-05Test fclose on an unopened file.Aaron Merey2-0/+41
Add new file libio/tst-fclosed-unopened.c that tests whether fclose on an unopened file returns EOF. Calling fclose on unopened files normally causes a use-after-free bug, however the standard streams are an exception since they are not deallocated by fclose. fclose returning EOF for unopened files is not part of the external contract but there are dependancies on this behaviour. For example, gnulib's close_stdout in lib/closeout.c. Tested for x86_64. Signed-off-by: Aaron Merey <amerey@redhat.com>
2024-09-05Fix memory leak on freopen error return (bug 32140)Joseph Myers2-0/+4
As reported in bug 32140, freopen leaks the FILE object when it returns NULL: there is no valid use of the FILE * pointer (including passing to freopen again or to fclose) after such an error return, so the underlying object should be freed. Add code to free it. Note 1: while I think it's clear from the relevant standards that the object should be freed and the FILE * can't be used after the call in this case (the stream is closed, which ends the lifetime of the FILE), it's entirely possible that some existing code does in fact try to use the existing FILE * in some way and could be broken by this change. (Though the most common case for freopen may be stdin / stdout / stderr, which _IO_deallocate_file explicitly checks for and does not deallocate.) Note 2: the deallocation is only done in the _IO_IS_FILEBUF case. Other kinds of streams bypass all the freopen logic handling closing the file, meaning a call to _IO_deallocate_file would neither be safe (the FILE might still be linked into the list of all open FILEs) nor sufficient (other internal memory allocations associated with the file would not have been freed). I think the validity of freopen for any other kind of stream will need clarifying with the Austin Group, but if it is valid in any such case (where "valid" means "not undefined behavior so required to close the stream" rather than "required to successfully associate the stream with the new file in cases where fopen would work"), more significant changes would be needed to ensure the stream gets fully closed. Tested for x86_64.
2024-09-05Clear flags2 flags set from mode in freopen (bug 32134)Joseph Myers2-0/+9
As reported in bug 32134, freopen does not clear the flags set in fp->_flags2 by the "e", "m" or "c" mode characters. Clear these so that they can be set or not as appropriate from the mode string passed to freopen. The relevant test for "e" in tst-freopen2-main.c is enabled accordingly; "c" is expected to be covered in a separately written test (and while tst-freopen2-main.c does include transitions to and from "m", that's not really a semantic flag intended to result in behaving in an observably different way). Tested for x86_64.
2024-09-04libio: Attempt wide backup free only for non-legacy codeSiddhesh Poyarekar1-1/+1
_wide_data and _mode are not available in legacy code, so do not attempt to free the wide backup buffer in legacy code. Resolves: BZ #32137 and BZ #27821 Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org> Reviewed-by: Florian Weimer <fweimer@redhat.com>
2024-08-15ungetc: Fix backup buffer leak on program exit [BZ #27821]Siddhesh Poyarekar2-2/+8
If a file descriptor is left unclosed and is cleaned up by _IO_cleanup on exit, its backup buffer remains unfreed, registering as a leak in valgrind. This is not strictly an issue since (1) the program should ideally be closing the stream once it's not in use and (2) the program is about to exit anyway, so keeping the backup buffer around a wee bit longer isn't a real problem. Free it anyway to keep valgrind happy when the streams in question are the standard ones, i.e. stdout, stdin or stderr. Also, the _IO_have_backup macro checks for _IO_save_base, which is a roundabout way to check for a backup buffer instead of directly looking for _IO_backup_base. The roundabout check breaks when the main get area has not been used and user pushes a char into the backup buffer with ungetc. Fix this to use the _IO_backup_base directly. Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org> Reviewed-by: Carlos O'Donell <carlos@redhat.com>
2024-08-15ungetc: Fix uninitialized read when putting into unused streams [BZ #27821]Siddhesh Poyarekar1-1/+1
When ungetc is called on an unused stream, the backup buffer is allocated without the main get area being present. This results in every subsequent ungetc (as the stream remains in the backup area) checking uninitialized memory in the backup buffer when trying to put a character back into the stream. Avoid comparing the input character with buffer contents when in backup to avoid this uninitialized read. The uninitialized read is harmless in this context since the location is promptly overwritten with the input character, thus fulfilling ungetc functionality. Also adjust wording in the manual to drop the paragraph that says glibc cannot do multiple ungetc back to back since with this change, ungetc can actually do this. Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org> Reviewed-by: Carlos O'Donell <carlos@redhat.com>
2024-08-14libio/tst-getdelim: Add new test covering NUL as a delimiterFrédéric Bérat1-1/+21
Add a new test to getdelim to verify that '\0' can be set as a delimiter. Reviewed-by: Florian Weimer <fweimer@redhat.com>
2024-08-05Fix name space violation in fortify wrappers (bug 32052)Andreas Schwab1-20/+20
Rename the identifier sz to __sz everywhere. Fixes: a643f60c53 ("Make sure that the fortified function conditionals are constant")
2024-07-09libio: handle opening a file when all files are closed (bug 31963)Andreas Schwab3-1/+37
_IO_list_all becomes NULL when all files (including standard files) are closed.
2024-07-01Fix conditionals on mtrace-based tests (bug 31892)Carlos O'Donell1-11/+31
The conditionals for several mtrace-based tests in catgets, elf, libio, malloc, misc, nptl, posix, and stdio-common were incorrect leading to test failures when bootstrapping glibc without perl. The correct conditional for mtrace-based tests requires three checks: first checking for run-built-tests, then build-shared, and lastly that PERL is not equal to "no" (missing perl). Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2024-06-11<stdio.h>: Acknowledge that getdelim/getline are in POSIXPhilip Kaludercic1-12/+2
These comments were written in 2003 (added in 2c008571c3a), predating the addition of getdelim(3)/getline(3) in POSIX.1-2008. Reviewed-by: Sam James <sam@gentoo.org> Reviewed-by: Florian Weimer <fweimer@redhat.com>
2024-06-04libio: Test for fdopen memory leak without SEEK_END support (bug 31840)Florian Weimer2-2/+64
The bug report used /dev/mem, but /proc/self/mem works as well (if available).
2024-06-04Remove memory leak in fdopen (bug 31840)Andreas Schwab1-1/+5
Deallocate the memory for the FILE structure when seeking to the end fails in append mode. Fixes: ea33158c96 ("Fix offset caching for streams and use it for ftell (BZ #16680)")
2024-05-21Change _IO_stderr_/_IO_stdin_/_IO_stdout to compat symbols [BZ #31766]H.J. Lu1-0/+4
Since Glibc never provides symbol binary compatibility for relocatable files, fix BZ #31766 by changing _IO_stderr_/_IO_stdin_/_IO_stdout to compat symbols. Signed-off-by: H.J. Lu <hjl.tools@gmail.com> Reviewed-by: Sunil K Pandey <skpgkp2@gmail.com>
2024-05-17Use a doubly-linked list for _IO_list_all (bug 27777)Alexandre Ferrieux3-2/+43
This patch fixes BZ #27777 "fclose does a linear search, takes ages when many FILE* are opened". Simply put, the master list of opened (FILE*), namely _IO_list_all, is a singly-linked list. As a consequence, the removal of a single element is in O(N), which cripples the performance of fclose(). The patch switches to a doubly-linked list, yielding O(1) removal. The one padding field in struct _IO_FILE, __pad5, is renamed to _prevchain for a doubly-linked list. Since fields in struct _IO_FILE after the _lock field are internal to glibc and opaque to applications. We can change them as long as the size of struct _IO_FILE is unchanged, which is checked as the part of glibc ABI with sizes of _IO_2_1_stdin_, _IO_2_1_stdout_ and _IO_2_1_stderr_. NB: When _IO_vtable_offset (fp) == 0, copy relocation will cover the whole struct _IO_FILE. Otherwise, only fields up to the _lock field will be copied to applications at run-time. It is used to check if the _prevchain field can be safely accessed. After opening 2 million (FILE*), the fclose() of 100 of them takes quite a few seconds without the patch, and under 2 seconds with it on a loaded machine. No test is added since there are no functional changes. Co-Authored-By: H.J. Lu <hjl.tools@gmail.com> Signed-off-by: Alexandre Ferrieux <alexandre.ferrieux@orange.com> Signed-off-by: H.J. Lu <hjl.tools@gmail.com> Reviewed-by: Carlos O'Donell <carlos@redhat.com>
2024-05-13libio/bug-wsetpos: Make the error message match the causing functionMaciej W. Rozycki1-1/+1
This test case calls `fopen': FILE *fp = fopen (temp_file, "r"); however if that fails it reports `fdopen' being the origin of the error. Adjust the message to say `fopen' then.
2024-05-06Add crt1-2.0.o for glibc 2.0 compatibility testsH.J. Lu2-0/+58
Starting from glibc 2.1, crt1.o contains _IO_stdin_used which is checked by _IO_check_libio to provide binary compatibility for glibc 2.0. Add crt1-2.0.o for tests against glibc 2.0. Define tests-2.0 for glibc 2.0 compatibility tests. Add and update glibc 2.0 compatibility tests for stderr, matherr and pthread_kill. Reviewed-by: Carlos O'Donell <carlos@redhat.com>