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

From: Bernhard Voelker <[email-masked]>
Date: Wed, 23 Jan 2013 14:21:29 +0100

On 01/23/2013 02:03 PM, Pádraig Brady wrote:
> On 01/23/2013 12:53 PM, Bernhard Voelker wrote:
>> head doesn't "allocate everything up front" - instead, it only
>> allocates the pointer array which would hold the actual data.
> 
> Sure. I was wondering whether that should change
> to allocate everything up front so as to exit early.

I think that's not a good idea because head would ENOMEM
even if it doesn't wouldn't need the memory (depending on the
input). Why should the following fail?

  $ echo 123 | head -c -T

I think there's no reason to fail until we really know it would
fail. In this case, we can only know it when we actually come
to the point when we need the memory.

It doesn't matter if we have
  $ echo 123 | head -c -10
or
  $ echo 123 | head -c -P

>>>        free_mem:
>>> -      for (i = 0; i < n_bufs; i++)
>>> +      for (i = 0; i < n_alloc; i++)
>>>            free (b[i]);
>>>          free (b);
>>
>> BTW: both in the old and the new version, the loop can break
>> if (b[i] == 0) because the array is filled from the beginning.
> 
> The new version only frees what's allocated and so gets this benefit too.

ah yes, sorry, I didn't look close enough.

Have a nice day,
Berny