Bug 266171 - Writing to stdout after moving file descriptor 1
Summary: Writing to stdout after moving file descriptor 1
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 13.1-RELEASE
Hardware: amd64 Any
: --- Affects Some People
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-09-02 10:49 UTC by JunT
Modified: 2022-09-13 23:55 UTC (History)
2 users (show)

See Also:


Attachments
minimum reproducer (397 bytes, text/plain)
2022-09-02 10:49 UTC, JunT
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description JunT 2022-09-02 10:49:53 UTC
Created attachment 236309 [details]
minimum reproducer

The attached C program gives different output on 13.0 and 13.1.
On 13.1, it gives:

fflush: Bad file descriptor
foo
end

But on 13.0 (and on Linux and macOS) the line 'foo' is not output.
I think this is the correct behavior since file descriptor 1 is closed when printf("foo\n") is called.

I've noticed this problem while testing zsh. If you build zsh (9.0) and run 'make check' then test A04 will fail on 13.1 due to this problem.
Comment 1 Konstantin Belousov freebsd_committer freebsd_triage 2022-09-02 14:04:24 UTC
https://reviews.freebsd.org/D36427 should fix it
Comment 2 commit-hook freebsd_committer freebsd_triage 2022-09-03 01:16:53 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=44cf1e5eb470380442fa8e240e213a71b8fe81d4

commit 44cf1e5eb470380442fa8e240e213a71b8fe81d4
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2022-09-02 13:39:38 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-09-03 01:06:41 +0000

    libc/stdio: only roll FILE state back on EINTR

    Reported by:    JunT <takimoto-j@kba.biglobe.ne.jp>
    PR:     266171
    Reviewed by:    emaste, markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D36427

 lib/libc/stdio/fflush.c  | 3 ++-
 lib/libc/stdio/fvwrite.c | 5 +++--
 lib/libc/stdio/wbuf.c    | 2 +-
 3 files changed, 6 insertions(+), 4 deletions(-)
Comment 3 JunT 2022-09-07 02:34:34 UTC
Thank you for the quick patch. I've tested the 'main' git branch and I think the problem is fixed; The C program does not output 'foo', and the zsh's test now passes.

Will this bug report be closed automatically?
Comment 4 Konstantin Belousov freebsd_committer freebsd_triage 2022-09-07 08:01:37 UTC
(In reply to JunT from comment #3)
No, it will not.  Close it yourself after I merge the change to stable/13.
Comment 5 commit-hook freebsd_committer freebsd_triage 2022-09-09 17:02:37 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=2a92854d81a10f02a8e244fe0ea449104bda5400

commit 2a92854d81a10f02a8e244fe0ea449104bda5400
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2022-09-02 13:39:38 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2022-09-09 17:01:53 +0000

    libc/stdio: only roll FILE state back on EINTR

    PR:     266171

    (cherry picked from commit 44cf1e5eb470380442fa8e240e213a71b8fe81d4)

 lib/libc/stdio/fflush.c  | 3 ++-
 lib/libc/stdio/fvwrite.c | 5 +++--
 lib/libc/stdio/wbuf.c    | 2 +-
 3 files changed, 6 insertions(+), 4 deletions(-)