[Top] [All Lists]

Re: [Nmh-workers] GCC 8 pre-releases have escaped...

2018-02-05 19:03:02
Yes, I like asprintf(), but it's not POSIX.  snprintf(NULL, 0, fmt, ...)
is POSIX and returns the strlen() of what would have been written,
allowing a second call to do the work, again, for real this time.

A quick glance suggests to me that even while asprintf() is not part of
POSIX, it is on *BSD systems, newer versions of MacOS X, and is in
glibc.  That sounds like pretty good coverage to me.  I'd personally be
willing to live with the inefficiency of a simple {v}asprintf()
implementation that just called {v}snprintf() with a size of 0 to get
the real size and then called it again with a correctly-allocated buffer.

We could avoid the double effort on most occasions by tracking the size
of the existing allocation and only realloc() when it's not big enough.
vsnprintf(3) exists so our function can pass on the va_list to it.

My reading of asprintf() is that it always allocates a new buffer, even
if the pointer you pass in is already allocated.

I'm fine with all of the changes you mentioned, Ralph, with one caveat ...
can we document them all in a header file or the source code so we
know which ones we should use under which circumstances?  I know we
might come to agreement on the mailing list, but for me that stuff pages
out over time and I'd like to have a canonical reference to look at.



<Prev in Thread] Current Thread [Next in Thread>