Bug 28099 - fcloseall doesn't close anything
Summary: fcloseall doesn't close anything
Status: UNCONFIRMED
Alias: None
Product: glibc
Classification: Unclassified
Component: stdio (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-07-18 16:10 UTC by Cristian Rodríguez
Modified: 2021-09-01 15:27 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Cristian Rodríguez 2021-07-18 16:10:37 UTC
Example to demonstrate de bug.

#define _GNU_SOURCE
#include <stdio.h>
#include <assert.h>

int main(void)
{
    FILE *fp, *fp1;
    fp = tmpfile();
    fp1 =tmpfile();
    assert ( fcloseall() == 0 );
    assert ( fputs("doh", fp) < 0 );
    assert ( fputs("bla", fp1) < 0 );
    return 0;
}

after the fcloseall() call all writes to the stream must fail..it really does not appear to be doing anything.
Comment 1 Andreas Schwab 2021-07-18 16:25:40 UTC
There is no way to find out because any use of any FILE pointer after fcloseall is undefined.
Comment 2 Cristian Rodríguez 2021-07-18 16:54:07 UTC
(In reply to Andreas Schwab from comment #1)
> There is no way to find out because any use of any FILE pointer after
> fcloseall is undefined.

yes, I guess I wrote the wrong test.. :-) my point is that fcloseall() is not closing streams, at least it is not doing what the manual says..

"This function causes all open streams of the process to be closed and the connections to corresponding files to be broken." the streams are NOT closed..fds are not closed either.
Comment 3 Ruud Harmsen 2021-09-01 08:02:21 UTC
Or perhaps the streams and file descriptors ARE closed, but fputs error checking isn't working properly? Cf. https://sourceware.org/bugzilla/show_bug.cgi?id=20632, where I found the first fputws to a closed stderr (that is, a close(2) was done) does return -1, as it should, but subsequent calls of  fputws return 1 as if they are OK. But they aren't, no text appears.
Comment 4 Cristian Rodríguez 2021-09-01 14:27:43 UTC
(In reply to Ruud Harmsen from comment #3)
> Or perhaps the streams and file descriptors ARE closed, but fputs error
> checking isn't working properly? Cf.
> https://sourceware.org/bugzilla/show_bug.cgi?id=20632, where I found the
> first fputws to a closed stderr (that is, a close(2) was done) does return
> -1, as it should, but subsequent calls of  fputws return 1 as if they are
> OK. But they aren't, no text appears.

In my test, file descriptors are not closed by a call to fcloseall()

Andreas is entirely right that doing anything with fp and fp1 after fcloseall() is UB. (it woould be incredible nice to get an error/assertion/fortify_fail or whatever in that case but that is again not required by anything) however when I tested nothing was closed in the first place.
Comment 5 Cristian Rodríguez 2021-09-01 15:27:22 UTC
expectation : fcloseall() sorts all file descriptors associated with FILE* s in the current thread, calls close_range(2) on them..

..and maybe..just maybe add a warning attribute to the function discouraging people to use it..