2005-09-26  Ulrich Drepper  <drepper@redhat.com>
	[BZ #713]
	* libio/iofgets.c: Treat N==1 correctly.
	* libio/iofgets_u.c: Likewise.
	* libio/iofgetws.c: Likewise.
	* libio/iofgetws_u.c: Likewise.
	* stdio-common/Makefile (tests): Add tst-fgets.
	* stdio-common/tst-fgets.c: New file.
This commit is contained in:
Ulrich Drepper 2005-09-27 00:00:42 +00:00
parent 9a5933834b
commit b5d839c996
8 changed files with 96 additions and 32 deletions

View File

@ -1,3 +1,13 @@
2005-09-26 Ulrich Drepper <drepper@redhat.com>
[BZ #713]
* libio/iofgets.c: Treat N==1 correctly.
* libio/iofgets_u.c: Likewise.
* libio/iofgetws.c: Likewise.
* libio/iofgetws_u.c: Likewise.
* stdio-common/Makefile (tests): Add tst-fgets.
* stdio-common/tst-fgets.c: New file.
2005-01-11 Thorsten Kukuk <kukuk@suse.de>
[BZ #652]

View File

@ -1,4 +1,4 @@
/* Copyright (C) 1993, 1995, 1996, 1997, 1998, 1999, 2002, 2003
/* Copyright (C) 1993, 1995, 1996, 1997, 1998, 1999, 2002, 2003, 2005
Free Software Foundation, Inc.
This file is part of the GNU C Library.
@ -41,6 +41,14 @@ _IO_fgets (buf, n, fp)
CHECK_FILE (fp, NULL);
if (n <= 0)
return NULL;
if (__builtin_expect (n == 1, 0))
{
/* Another irregular case: since we have to store a NUL byte and
there is only room for exactly one byte, we don't have to
read anything. */
buf[0] = '\0';
return buf;
}
_IO_acquire_lock (fp);
/* This is very tricky since a file descriptor may be in the
non-blocking mode. The error flag doesn't mean much in this

View File

@ -1,4 +1,4 @@
/* Copyright (C) 1993,1995,1996,1997,1998,2002 Free Software Foundation, Inc.
/* Copyright (C) 1993,1995-1998,2002,2005 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
@ -40,6 +40,14 @@ fgets_unlocked (buf, n, fp)
CHECK_FILE (fp, NULL);
if (n <= 0)
return NULL;
if (__builtin_expect (n == 1, 0))
{
/* Another irregular case: since we have to store a NUL byte and
there is only room for exactly one byte, we don't have to
read anything. */
buf[0] = '\0';
return buf;
}
/* This is very tricky since a file descriptor may be in the
non-blocking mode. The error flag doesn't mean much in this
case. We return an error only when there is a new error. */

View File

@ -1,4 +1,4 @@
/* Copyright (C) 1993, 1995, 1996, 1997, 1998, 1999, 2001
/* Copyright (C) 1993, 1995, 1996, 1997, 1998, 1999, 2001, 2005
Free Software Foundation, Inc.
This file is part of the GNU C Library.
@ -41,6 +41,14 @@ fgetws (buf, n, fp)
CHECK_FILE (fp, NULL);
if (n <= 0)
return NULL;
if (__builtin_expect (n == 1, 0))
{
/* Another irregular case: since we have to store a NUL byte and
there is only room for exactly one byte, we don't have to
read anything. */
buf[0] = L'\0';
return buf;
}
_IO_acquire_lock (fp);
/* This is very tricky since a file descriptor may be in the
non-blocking mode. The error flag doesn't mean much in this

View File

@ -1,4 +1,4 @@
/* Copyright (C) 1993, 95, 96, 97, 98, 99 Free Software Foundation, Inc.
/* Copyright (C) 1993, 95, 96, 97, 98, 99, 2005 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
@ -40,6 +40,14 @@ fgetws_unlocked (buf, n, fp)
CHECK_FILE (fp, NULL);
if (n <= 0)
return NULL;
if (__builtin_expect (n == 1, 0))
{
/* Another irregular case: since we have to store a NUL byte and
there is only room for exactly one byte, we don't have to
read anything. */
buf[0] = L'\0';
return buf;
}
/* This is very tricky since a file descriptor may be in the
non-blocking mode. The error flag doesn't mean much in this
case. We return an error only when there is a new error. */

View File

@ -1,4 +1,5 @@
/* Copyright (C) 1993,1997,1998,2000,2001,2002 Free Software Foundation, Inc.
/* Copyright (C) 1993,1997,1998,2000,2001,2002,2005
Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
@ -73,7 +74,8 @@ _IO_getline_info (fp, buf, n, delim, extract_delim, eof)
int c = __uflow (fp);
if (c == EOF)
{
if (eof) *eof = c;
if (eof)
*eof = c;
break;
}
if (c == delim)
@ -89,31 +91,31 @@ _IO_getline_info (fp, buf, n, delim, extract_delim, eof)
*ptr++ = c;
n--;
}
else
{
char *t;
if ((_IO_size_t) len >= n)
len = n;
t = (char *) memchr ((void *) fp->_IO_read_ptr, delim, len);
if (t != NULL)
{
_IO_size_t old_len = ptr-buf;
len = t - fp->_IO_read_ptr;
if (extract_delim >= 0)
{
++t;
if (extract_delim > 0)
++len;
}
memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
fp->_IO_read_ptr = t;
return old_len + len;
}
memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
fp->_IO_read_ptr += len;
ptr += len;
n -= len;
}
else
{
char *t;
if ((_IO_size_t) len >= n)
len = n;
t = (char *) memchr ((void *) fp->_IO_read_ptr, delim, len);
if (t != NULL)
{
_IO_size_t old_len = ptr-buf;
len = t - fp->_IO_read_ptr;
if (extract_delim >= 0)
{
++t;
if (extract_delim > 0)
++len;
}
memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
fp->_IO_read_ptr = t;
return old_len + len;
}
memcpy ((void *) ptr, (void *) fp->_IO_read_ptr, len);
fp->_IO_read_ptr += len;
ptr += len;
n -= len;
}
}
return ptr - buf;
}

View File

@ -53,7 +53,7 @@ tests := tstscanf test_rdwr test-popen tstgetln test-fseek \
scanf11 scanf12 tst-tmpnam tst-cookie tst-obprintf tst-sscanf \
tst-swprintf tst-fseek tst-fmemopen test-vfprintf tst-gets \
tst-perror tst-sprintf tst-rndseek tst-fdopen tst-fphex bug14 bug15 \
tst-popen tst-unlockedio tst-fmemopen2 tst-put-error
tst-popen tst-unlockedio tst-fmemopen2 tst-put-error tst-fgets
test-srcs = tst-unbputc tst-printf

20
stdio-common/tst-fgets.c Normal file
View File

@ -0,0 +1,20 @@
/* Derived from the test case in
http://sourceware.org/bugzilla/show_bug.cgi?id=713. */
#include <stdio.h>
static int
do_test (void)
{
FILE *fp = fmemopen ("hello", 5, "r");
char buf[2];
char *bp = fgets (buf, sizeof (buf), fp);
printf ("fgets: %s\n", bp == buf ? "OK" : "ERROR");
int res = bp != buf;
bp = fgets_unlocked (buf, sizeof (buf), fp);
printf ("fgets_unlocked: %s\n", bp == buf ? "OK" : "ERROR");
res |= bp != buf;
return res;
}
#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"