Quantcast

Query regarding offered alternative to asprintf

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Query regarding offered alternative to asprintf

Amy Lear
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):


#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

int asprintf( char **, char *, ... );
int vasprintf( char **, char *, va_list );

int vasprintf( char **sptr, char *fmt, va_list argv )
{
int wanted = vsnprintf( *sptr = NULL, 0, fmt, argv );
if( (wanted < 0) || ((*sptr = malloc( 1 + wanted )) == NULL) )
return -1;

return vsprintf( *sptr, fmt, argv );
}

int asprintf( char **sptr, char *fmt, ... )
{
int retval;
va_list argv;
va_start( argv, fmt );
retval = vasprintf( sptr, fmt, argv );
va_end( argv );
return retval;
}


------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today.  Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Query regarding offered alternative to asprintf

Keith Marshall
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

  http://linux.die.net/man/3/asprintf

--
Regards,
Keith.

------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today.  Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Query regarding offered alternative to asprintf

Amy Lear
On Wed, May 4, 2011 at 7:24 AM, Keith Marshall <[hidden email]> wrote:
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

 http://linux.die.net/man/3/asprintf

--
Regards,
Keith.

Thank you very much!


------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today.  Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Query regarding offered alternative to asprintf

mewalig
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?

This is occurring on my XP sp3 system running mingw32_NT-5.1
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Query regarding offered alternative to asprintf

mewalig
OK, I was able to get the problem I just posted about it by using vfprintf() instead of vsnprintf():

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

int asprintf( char **, char *, ... );
int vasprintf( char **, char *, va_list );
FILE *ftrash = NULL;

int vasprintf( char **sptr, char *fmt, va_list argv )
{
        if(!ftrash) ftrash = fopen("nul", "wb"); /* on windows, /dev/null = nul */
        if(!ftrash) fprintf(stderr, "this shouldn't happen\n");

/* NOTE: remember to fclose(ftrash) after the last call to vasprintf() has been made */

/****
old code:
        int wanted = vsnprintf( *sptr = NULL, 0, fmt, argv );

new code: */
        int wanted = vfprintf( ftrash, fmt, argv );
/****/

        if( (wanted < 0) || ((*sptr = malloc( 1 + wanted )) == NULL) )
        return -1;
       

/****
old code:
        return vsprintf( *sptr, fmt, argv );

new code: */
        (*sptr)[wanted] = '\0';
        return vsnprintf( *sptr, wanted, fmt, argv );
/****/
}

int asprintf( char **sptr, char *fmt, ... )
{
        int retval;
        va_list argv;
        va_start( argv, fmt );
        retval = vasprintf( sptr, fmt, argv );
               
        va_end( argv );
        return retval;
}
Loading...