On Sun, Nov 23, 2014 at 06:59:59PM +0100, Nicolas Bedos wrote:
> Index: src/usr.bin/locate//code/locate.code.c
> ===================================================================
> RCS file: /cvs/src/usr.bin/locate/code/locate.code.c,v
> retrieving revision 1.17
> diff -u -p -u -r1.17 locate.code.c
> --- src/usr.bin/locate//code/locate.code.c    17 Nov 2013 20:19:36 -0000      
> 1.17
> +++ src/usr.bin/locate//code/locate.code.c    23 Nov 2014 16:49:47 -0000
> @@ -171,6 +171,14 @@ main(int argc, char *argv[])
>                       /* chop newline */
>                       if (*cp == '\n')
>                               *cp = '\0';
> +             }
> +
> +             /* skip truncated lines */
> +             if (cp == path + sizeof(buf2) - 1 && *(cp-1) != '\0') {
> +                     while (fgets(path, sizeof(buf2), stdin) != NULL)
> +                             if (strchr(path, '\n') != NULL)
> +                                     break;
> +                     continue;
>               }
>  
>               /* Skip longest common prefix. */

I would recommend using fgetln for the actual line parsing.  Then this
kinda fragile code can be avoided (fragile: fgets and its users have a
hard time to properly handle '\0' chars inside a file).

See also:
- fgetln(3)
- http://undeadly.org/cgi?action=article&sid=20061027031811


Tobias

Reply via email to