Bad handling of EOF in Stdio.FILE.gets()
Imported from http://bugzilla.roxen.com/bugzilla/show_bug.cgi?id=5
Reported by @grubba
From: Aleph One <aleph1@underground.org>
To: pike@idonex.se
Date: Tue, 11 Jul 2000 12:28:15 -0700
Subject: Stdio.FILE.gets()
There is a bug in the Stdio.FILE.gets() functions. The standard C library gets() function will read bytes until a terminating newline or an EOF. If it reaches the EOF it will replace the EOF by a null char and return the last "unterminated" last line. If you read again it returns NULL.
Pike's Stdio.FILE.gets() fails to return the last line of a file unless its terminated by a newline character.
string gets()
{
int p,tmp=0;
while((p=search(b, "\n", bpos+tmp)) == -1)
{
tmp=strlen(b)-bpos;
if(!get_data()) return 0;
}
return extract(p-bpos, 1);
}
should be something like:
string gets()
{
int p,tmp=0;
while((p=search(b, "\n", bpos+tmp)) == -1)
{
tmp=strlen(b)-bpos;
if(!get_data())
{
if (bpos == sizeof(b))
return 0;
else
return extract(sizeof(b)-bpos, 0);
}
}
return extract(p-bpos, 1);
}
Comments?
-- Aleph One / aleph1@underground.org http://underground.org/ KeyID 1024/948FD6B5 Fingerprint EE C9 E8 AA CB AF 09 61 8C 39 EA 47 A8 6A B8 01