Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 31 Mar 2009 02:50:24 -0400
From:      David Schultz <das@FreeBSD.ORG>
To:        Joe Marcus Clarke <marcus@FreeBSD.ORG>
Cc:        current <current@FreeBSD.ORG>
Subject:   Re: getline incompatibility with Linux
Message-ID:  <20090331065024.GA9671@zim.MIT.EDU>
In-Reply-To: <1238362728.73736.165.camel@shumai.marcuscom.com>
References:  <1238362728.73736.165.camel@shumai.marcuscom.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sun, Mar 29, 2009, Joe Marcus Clarke wrote:
> The new getline() function in FreeBSD is not completely compatible with
> Linux's implementation.  The result is that programs which assume Linux
> getline may enter a tight infinite loop.
> 
> According to the Linux getline(3) manpage, getline(3) returns -1 on
> error (including EOF).  Our implementation returns 0 on EOF.  Would it
> be possible to return -1 on EOF in our implementation?

Good catch; even POSIX says (in its usual roundabout way) that
getline() is supposed to return -1 on both error and EOF, and
never return 0. Of course POSIX merely inherited this braindeadness
from glibc...

The following patch should fix it. I'll commit this soonish, but
ENOTIME right now. Thanks for pointing this out.

Index: getline.3
===================================================================
--- getline.3	(revision 190425)
+++ getline.3	(working copy)
@@ -24,7 +24,7 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd February 28, 2009
+.Dd March 29, 2009
 .Dt GETLINE 3
 .Os
 .Sh NAME
@@ -79,7 +79,7 @@
 functions return the number of characters written, excluding the
 terminating
 .Dv NUL .
-The value \-1 is returned if an error occurs.
+The value \-1 is returned if an error occurs, or if end-of-file is reached.
 .Sh EXAMPLES
 The following code fragment reads lines from a file and
 writes them to standard output.
Index: getdelim.c
===================================================================
--- getdelim.c	(revision 190425)
+++ getdelim.c	(working copy)
@@ -120,7 +120,6 @@
 		goto error;
 	}
 
-	linelen = 0;
 	if (*linecapp == 0)
 		*linep = NULL;
 
@@ -128,9 +127,12 @@
 		/* If fp is at EOF already, we just need space for the NUL. */
 		if (__sferror(fp) || expandtofit(linep, 1, linecapp))
 			goto error;
-		goto done;
+		FUNLOCKFILE(fp);
+		(*linep)[0] = '\0';
+		return (-1);
 	}
 
+	linelen = 0;
 	while ((endp = memchr(fp->_p, delim, fp->_r)) == NULL) {
 		if (sappend(linep, &linelen, linecapp, fp->_p, fp->_r))
 			goto error;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20090331065024.GA9671>