Subject: Re: Implementation of asprintf()/vasprintf()
To: Matthias Scheler <tron@zhadum.de>
From: Jaromír <jdolecek@netbsd.org>
List: tech-userlevel
Date: 09/30/2001 12:14:56
Yes, I suppose that would be okay. Though I wonder if realloc(3)
ever fails if the result is smaller than the original.
Jaromir
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.
>
> Kind regards
>
> --
> Matthias Scheler http://scheler.de/~matthias/
>
--
Jaromir Dolecek <jdolecek@NetBSD.org> http://www.ics.muni.cz/~dolecek/
NetBSD - just plain best OS! -=*=- Got spare MCA cards or docs? Hand me them!