Bug 24940

Summary: fgets get skipped after EOF
Product: glibc Reporter: pascal.liedtke
Component: stdioAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED INVALID    
Severity: critical CC: fweimer
Priority: P2 Flags: fweimer: security-
Version: 2.29   
Target Milestone: ---   
See Also: https://sourceware.org/bugzilla/show_bug.cgi?id=1190
Host: Target:
Build: Last reconfirmed:

Description pascal.liedtke 2019-08-27 08:57:51 UTC
Here a sample code:

#include <stdio.h>
int main() {
    char input[80];
    while (fgets(input, 80, stdin) != NULL) {
        // nop
    }
    fgets(input, 80, stdin);
    printf("%s\n", input);
    return 0;
}

After sending EOF to exit the loop, the second fgets returns immediately return NULL, no second input possible. Compiling on 2.27 works as expected. Compiling on a 2.27 system with -static and executing on a 2.29 System also works.

I have no more glibc versions to check more. Only can say bugged on 2.29, works on 2.27.
Comment 1 Andreas Schwab 2019-08-27 09:16:42 UTC
This is correct behaviour.  EOF is supposed to be sticky, see commit 2cc7bad0ae.