Subject: Re: Implementation of asprintf()/vasprintf()
To: Matthias Scheler <tron@zhadum.de>
From: Charles M. Hannum <abuse@spamalicious.com>
List: tech-userlevel
Date: 10/01/2001 03:06:23
On Sun, 2001-09-30 at 07:07, Matthias Scheler wrote:
> Hello,
>
> after calling vfprintf() our implementations of asprintf() and vasprintf()
> both do this:
>
> _base = realloc(f._bf._base, (size_t)(ret + 1));
> if (_base == NULL)
> goto err;
> *str = (char *)_base;
> return (ret);
>
> It appears to me that this code is supposed to avoid wasting memory if
> the buffer is bigger than the result string. I only wonder if the
> error handling should look like this:
>
> _base = realloc(f._bf._base, (size_t)(ret + 1));
> *str = (_base == NULL) ? f._bf._base : _base;
> return (ret);
>
> This would avoid an unnecessary failure if the realloc() call fails.
This seems like navel-gazing to me, but in principal it's okay.