Query regarding offered alternative to asprintf

Query regarding offered alternative to asprintf

Back in December 2008, Keith Marshall responded to a request in this mailing list for how to deal with the lack of asprintf in mingw with a bit of code that provides equivalent function.

I wanted to double-check before integrating this into a project (a unix package I am also hoping to use in win32) a) that this is considered a viable way to provide this functionality, and b) what license terms might apply. I know some portions of mingw are public domain, but it's important to me that I know everything that goes into this is well documented.

Disclaimer: I'm not actually very competent with C, I'm just trying to get infrastructure working. As far as I can tell it SEEMS to work, but I wanted to double-check with you guys here.

Here's what I found in the two posts to the mailing list (including a revision in a followup to the original response):

Re: Query regarding offered alternative to asprintf

On 4 May 2011 03:16, Amy Lear wrote:
> Back in December 2008, Keith Marshall responded to a request in this mailing
> list for how to deal with the lack of asprintf in mingw with a bit of code
> that provides equivalent function.
>
> I wanted to double-check before integrating this into a project (a unix
> package I am also hoping to use in win32) a) that this is considered a
> viable way to provide this functionality,

I think it remains so.

> and b) what license terms might
> apply. I know some portions of mingw are public domain, but it's important
> to me that I know everything that goes into this is well documented.

Any code posted on a public mailing list -- unless it is evident that
it has been misappropriated from a copyrighted source, or the posting
itself declares it to be otherwise -- is effectively public domain.
Feel free to use any code I post here, as if it is your own; (thus,
*you* assume full responsibility for any deployment issues).

Just for the (licensing) record: I'll have written that code fragment
entirely from scratch, without reference to any other source code, to
deliver functionality as decribed in, and per my own interpretation of,
a publicly available Linux manpage, such as

Re: Query regarding offered alternative to asprintf

Just for the (licensing) record: I'll have written that code fragment
entirely from scratch, without reference to any other source code, to
deliver functionality as decribed in, and per my own interpretation of,
a publicly available Linux manpage, such as

Re: Query regarding offered alternative to asprintf

The above code appears to sometimes underestimate the amount of memory required. The offending code appears to be:

wanted = vsnprintf( *sptr = NULL, 0, fmt, argv );

for example, if the inputs are "%lf" with double value of 12345678901, this code will return 9, whereas the vsprintf() statement below it will write 18 bytes (12345678901.000000). This in turn leads to vsprintf writing into un-allocated memory space which at some point will crash the program.

The un-allocated memory write problem is probably fixable by using vsnprintf(*sptr, wanted, ...). However, that doesn't fix the problem of "wanted" being too small to begin with. anyone have any ideas on how to fix that?