Bug 107824

Summary: /usr/bin/head does not work with files over 2GB.
Product: Base System Reporter: Brian Cornell <briancornell>
Component: binAssignee: Brooks Davis <brooks>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Unspecified   
Hardware: Any   
OS: Any   

Description Brian Cornell 2007-01-11 14:20:13 UTC
When the '-c' option in head is passed an integer greater than 2147483647 it will not stop output at requested value but will instead continue output until EOF.

Fix: 

Change byte counters in head.c from long to double types.  This has been tested & works.
Output of diff from release to modified source code:
diff head.c head.61freebsd
64c64
< static void head_bytes(FILE *, double);
---
> static void head_bytes(FILE *, size_t);
73,74c73
<       int first, linecnt = -1, eval = 0;
<       double bytecnt = -1;
---
>       int first, linecnt = -1, bytecnt = -1, eval = 0;
81c80
<                       bytecnt = strtod(optarg, &ep);
---
>                       bytecnt = strtol(optarg, &ep, 10);
142c141
< head_bytes(FILE *fp, double cnt)
---
> head_bytes(FILE *fp, size_t cnt)
145c144
<       double readlen;
---
>       size_t readlen;

END OUTPUT.
How-To-Repeat: Where <file> is a file that is larger than 2.5GB use the following syntax:

head -c 2200000000 <file>

Head will output the entire file instead of the first 2.2GB.
Comment 1 brooks 2007-01-11 16:03:37 UTC
On Thu, Jan 11, 2007 at 02:19:04PM +0000, Brian Cornell wrote:
> 
> Change byte counters in head.c from long to double types.  This has been tested & works.
> Output of diff from release to modified source code:
> diff head.c head.61freebsd
> 64c64
> < static void head_bytes(FILE *, double);
> ---
> > static void head_bytes(FILE *, size_t);
> 73,74c73
> <       int first, linecnt = -1, eval = 0;
> <       double bytecnt = -1;
> ---
> >       int first, linecnt = -1, bytecnt = -1, eval = 0;
> 81c80
> <                       bytecnt = strtod(optarg, &ep);
> ---
> >                       bytecnt = strtol(optarg, &ep, 10);
> 142c141
> < head_bytes(FILE *fp, double cnt)
> ---
> > head_bytes(FILE *fp, size_t cnt)
> 145c144
> <       double readlen;
> ---
> >       size_t readlen;

This is bogus.  Using a floating point type to hold an interger quantity
is wrong.  The correct type here is off_t.  I'd suggest using strtoll or
strtonum for the conversion.

-- Brooks
Comment 2 dfilter service freebsd_committer freebsd_triage 2007-01-11 17:04:09 UTC
brooks      2007-01-11 17:03:51 UTC

  FreeBSD src repository

  Modified files:
    usr.bin/head         Makefile head.c 
  Log:
  Fix head -c ### where ### is greater than 2^31.  Unlike the submitted
  patch this uses off_t.
  
  WARNSify and add $FreeBSD$ to Makefile.
  
  PR:             bin/107824
  Submitted by:   Brian Cornell <briancornell at earthlink dot net>
  MFC after:      3 days
  
  Revision  Changes    Path
  1.2       +2 -0      src/usr.bin/head/Makefile
  1.19      +6 -4      src/usr.bin/head/head.c
_______________________________________________
cvs-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/cvs-all
To unsubscribe, send any mail to "cvs-all-unsubscribe@freebsd.org"
Comment 3 Brooks Davis freebsd_committer freebsd_triage 2007-01-11 17:47:50 UTC
State Changed
From-To: open->patched

Fixed in HEAD. 


Comment 4 Brooks Davis freebsd_committer freebsd_triage 2007-01-11 17:47:50 UTC
Responsible Changed
From-To: freebsd-bugs->brooks

Fixed in HEAD.
Comment 5 dfilter service freebsd_committer freebsd_triage 2007-01-15 17:11:47 UTC
brooks      2007-01-15 17:11:38 UTC

  FreeBSD src repository

  Modified files:        (Branch: RELENG_6)
    usr.bin/head         Makefile head.c 
  Log:
  MFC: head.c revs 1.19-20, Makefile rev 1.2
  
  Fix head -c ### where ### is greater than 2^31.  Unlike the submitted
  patch this uses off_t.
  
  WARNSify and add $FreeBSD$ to Makefile.
  
  PR:             bin/107824
  Submitted by:   Brian Cornell <briancornell at earthlink dot net>
  
  Revision      Changes    Path
  1.1.1.1.46.1  +2 -0      src/usr.bin/head/Makefile
  1.18.14.1     +7 -5      src/usr.bin/head/head.c
_______________________________________________
cvs-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/cvs-all
To unsubscribe, send any mail to "cvs-all-unsubscribe@freebsd.org"
Comment 6 Brooks Davis freebsd_committer freebsd_triage 2007-01-15 17:11:51 UTC
State Changed
From-To: patched->closed

Merged to RELENG_6.  Will be in 6.3-RELEASE.