diff options
author | Joseph Myers <josmyers@redhat.com> | 2024-09-05 11:16:59 +0000 |
---|---|---|
committer | Joseph Myers <josmyers@redhat.com> | 2024-09-05 11:16:59 +0000 |
commit | 9c0d6f7a1046aba111e25e34ec07242853e859dc (patch) | |
tree | 009d98634d716edc7326820f5b3ea7dfb674b550 /stdio-common/tst-freopen3-main.c | |
parent | f512634ddef242ef0ff025ddeba64ce51035040f (diff) | |
download | glibc-9c0d6f7a1046aba111e25e34ec07242853e859dc.tar.gz glibc-9c0d6f7a1046aba111e25e34ec07242853e859dc.tar.xz glibc-9c0d6f7a1046aba111e25e34ec07242853e859dc.zip |
Fix memory leak on freopen error return (bug 32140)
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.
Diffstat (limited to 'stdio-common/tst-freopen3-main.c')
-rw-r--r-- | stdio-common/tst-freopen3-main.c | 2 |
1 files changed, 2 insertions, 0 deletions
diff --git a/stdio-common/tst-freopen3-main.c b/stdio-common/tst-freopen3-main.c index 5107e1f98e..990a6e5921 100644 --- a/stdio-common/tst-freopen3-main.c +++ b/stdio-common/tst-freopen3-main.c @@ -18,6 +18,7 @@ #include <errno.h> #include <fcntl.h> +#include <mcheck.h> #include <stdio.h> #include <stdlib.h> @@ -48,6 +49,7 @@ int do_test (void) { + mtrace (); struct support_descriptors *fds; char *temp_dir = support_create_temp_directory ("tst-freopen3"); char *file1 = xasprintf ("%s/file1", temp_dir); |