Subject: bug#13530: head: memory exhausted when printing all from stdin but last P/E bytes

From: Lei Zhang <[email-masked]>
Date: Tue, 22 Jan 2013 21:32:57 -0500

Hi All,

We found a bug in the `head' program of coreutils 8.20:

Invoking `head -c -P' or `head -c -E' will cause memory exhaustion.

However, smaller units (e.g., b, K, M) work fine; bigger units (e.g., Z, Y)
fail properly
(by outputing "number of bytes is so large that it is not representable").
And `-n' also
works fine.

A bit dig reveals that the bug is introduced at line 322 of head.c
(coreutils 8.20):

204: elide_tail_bytes_pipe (const char *filename, int fd, uintmax_t
n_elide_0)
205: {
322:     b = xcalloc (n_bufs, sizeof *b); /** this statement fails **/
398: }

We also examined n_elide and n_bufs before that statement. Actually, they
are very
large:

n_elide: 1125899906842624
n_bufs: 137438953474

According to the following comment in the source file:

> CAUTION: do not fail (out of memory) when asked to elide
> a ridiculous amount, but when given only a small input.  */

We think this is a bug and bring this issue to your attention. Thanks!

Environments:

$ uname -a
Linux anti-think 3.7.3-1-ARCH #1 SMP PREEMPT Thu Jan 17 18:52:30 CET 2013
x86_64 GNU/Linux

$ pacman -Qi coreutils
Name           : coreutils
Version        : 8.20-1
URL            : http://www.gnu.org/software/coreutils
Licenses       : GPL3
Groups         : base
Provides       : None
Depends On     : glibc  pam  acl  gmp  libcap
Optional Deps  : None
Required By    : ca-certificates  dbus  filesystem  linux  mkinitcpio  perl
 sysvinit-tools  util-linux
Conflicts With : None
Replaces       : None
Installed Size : 13820.00 KiB
Packager       : Allan McRae <[email-masked]>
Architecture   : x86_64
Build Date     : Wed 24 Oct 2012 03:57:11 AM EDT
Install Date   : Sun 28 Oct 2012 01:51:06 PM EDT
Install Reason : Explicitly installed
Install Script : Yes
Description    : The basic file, shell and text manipulation utilities of
the GNU operating system

CPU: Intel(R) Core(TM) i5-3320M CPU @ 2.60GHz CPU
memory: 4GB

Best regards,

-- 
Lei Zhang
Department of Electrical and Computer Engineering
University of Waterloo
 200 University Avenue West
Waterloo, Ontario, Canada N2L 3G1