[GRASS5] Re: string lengths

Paul Kelly paul-grass at stjohnspoint.co.uk
Sat Feb 12 12:19:49 EST 2005


On Sat, 12 Feb 2005, Markus Neteler wrote:

>> sprintf (colorstring, "%s", db_get_string(&valstr));
>>
>> is a classic buffer overflow if valstr is longer than sizeof(colorstring).
>
> Thanks for catching that. That are my personal problems with strings in C :-)
>
>> thus there is snprintf(), but IIRC that wasn't portable? so we have
>> G_asprintf(), which I'm not very familiar with.
>> shrug.

Declare as:
char *colorstring;

Then:
colorstring = G_store( db_get_string(&valstr) );

Note that:
G_asprintf( &colorstring, "%s", db_get_string(&valstr) );
would also work but no need to complicate things further than G_store() as 
you're just copying one string to another.

Then (after the last time you've used colorstring):
G_free(colorstring);

Should be freed if either G_store() or G_asprintf() have been used.

Hope this helps and is correct.

Paul




More information about the grass-dev mailing list