PATCH: Allocate sufficient space for string buffer

H.J. Lu hjl.tools@gmail.com
Fri Jun 8 02:36:00 GMT 2012


On Thu, Jun 7, 2012 at 7:09 PM, Alan Modra <amodra@gmail.com> wrote:
> On Thu, Jun 07, 2012 at 11:58:45AM -0700, H.J. Lu wrote:
>>       * input-scrub.c (input_scrub_include_sb): Use sb_build to
>>       allocate sufficient space for from_sb.
>>       * read.c (do_repeat): Use sb_build to allocate sufficient space
>>       for many.
>>       * sb.c (sb_build): Remove static.
>>       * sb.h (sb_build): New prototype.
>
> Seems a reasonable optimisation.  OK, but
>
>> -  sb_new (&many);
>> +  sb_build (&many, count * one.len + 1);
>
> looks wrong to me.  sb_build already adds one to account for the
> string terminator.

We have

static void
sb_check (sb *ptr, size_t len)
{
  size_t max = ptr->max;

  while (ptr->len + len >= max)
    {
      max <<= 1;
      if (max == 0)
        as_fatal ("string buffer overflow");
    }
  if (max != ptr->max)
    {
      ptr->max = max;
      ptr->ptr = xrealloc (ptr->ptr, max + 1);
    }
}

If ptr->len + len == ptr->max, we double the size.
Should it be changed to

while (ptr->len + len > max)


-- 
H.J.



More information about the Binutils mailing list