bug-gnulib
[Top][All Lists]
Advanced

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

Re: getline() behaviour change


From: Bruno Haible
Subject: Re: getline() behaviour change
Date: Wed, 22 Aug 2007 23:25:47 +0200
User-agent: KMail/1.5.4

Hi Eric,

> The patch includes a couple of bug fixes in getdelim.c:
> 
> It is valid on entrance for *lineptr to be non-NULL but *n to be 0 (ie.
> via malloc(0)), but your implementation was leaking that memory.  Also,
> your implementation failed to set errno to EOVERFLOW when it detects that
> it needs to malloc more than SSIZE_MAX bytes.

> --- lib/getdelim.c    29 Oct 2006 21:52:55 -0000      1.7
> +++ lib/getdelim.c    22 Aug 2007 00:04:01 -0000
> @@ -62,10 +67,10 @@ getdelim (char **lineptr, size_t *n, int
>  
>    flockfile (fp);
>  
> -  if (*lineptr == NULL || *n == 0)
> +  if (*n == 0)

This is a behaviour change: Previously when *lineptr == NULL, *n did not
need to be initialized. Now it needs to be initialized to 0. Should be
mentioned in NEWS.

>      {
>        *n = 120;
> -      *lineptr = (char *) malloc (*n);
> +      *lineptr = (char *) realloc (*lineptr, 120);
>        if (*lineptr == NULL)
>       {
>         result = -1;

It's more maintainable to write *n instead of 120. Compilers optimize this
memory access anyway.

> @@ -97,6 +102,7 @@ getdelim (char **lineptr, size_t *n, int
>         if (cur_len + 1 >= needed)
>           {
>             result = -1;
> +           errno = EOVERFLOW;
>             goto unlock_return;
>           }
 
The POSIX spec implies that errno should be set when getdelim fails. Therefore
errno needs to be set not only here, with EOVERFLOW. In lines 77 and 113
errno should be set to ENOMEM, since errno is undefined upon return from
malloc() or realloc().

Bruno





reply via email to

[Prev in Thread] Current Thread [Next in Thread]