NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

lib/41992: fgets returns non-NULL for a buffer size of 1 on an empty file



>Number:         41992
>Category:       lib
>Synopsis:       fgets returns non-NULL for a buffer size of 1 on an empty file
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Sep 04 23:30:00 +0000 2009
>Originator:     Kate F
>Release:        3.1
>Organization:
>Environment:
NetBSD clarion 3.1 NetBSD 3.1 (GENERIC) #0: Tue Oct 31 04:27:07 UTC 2006  
builds%b0.netbsd.org@localhost:/home/builds/ab/netbsd-3-1-RELEASE/i386/200610302053Z-obj/home/builds/ab/netbsd-3-1-RELEASE/src/sys/arch/i386/compile/GENERIC
 i386
>Description:
Here's a test case:

#include <stdio.h>

int main(void)
{
    FILE *f;
    char *p;
    char a[1];

    /* create an empty file. x should not exist beforehand */
    system("touch x");

    f = fopen("x", "r");
    if (f == NULL) {
        perror("fopen");
        return 1;
    }

    p = fgets(a, sizeof a, f);
    if (p == NULL) {
        perror("fgets");
        return 1;
    }

    /* i believe p should be NULL for an empty file where sizeof a is 1 */
    printf("p = %p\n", p);

    return 0;
}

The wording for C differs slightly from the wording for POSIX for fgets in this 
situation, but both are at EOF, so I believe both ought to return NULL.

This bug was described to me by Magnus Loef <magnus-swe%telia.com@localhost>, 
in ##c on freenode. He points out that this behaviour has the undesirable 
effect of causing infinite loops on empty files for the ubiquitous while 
(fgets(...) != NULL) ...; idiom.
>How-To-Repeat:

>Fix:



Home | Main Index | Thread Index | Old Index