[GRASS5] Re: string lengths

Markus Neteler neteler at itc.it
Sat Feb 12 10:13:02 EST 2005


On Sat, Feb 12, 2005 at 06:56:54PM +1300, Hamish wrote:
> http://grass.itc.it/pipermail/grass-commit/2005-February/016839.html
> 
> -    char buf[2000], colorstring[8]; /* RR:GG:BB */
> 
> 
> re. string arrays- fyi
> 
> you usually need to leave room in a string array for a terminating null,
> 
> so if the space needed to be for "RR:GG:BB", you'd need to set
> "char colorstring[9];", which would be addressable as colorstring[0] to
> colorstring[8].
> 
> C lets you access colorstring[912334565] if you want.. but this usually
> segfaults as (non)random memory access...
> 
> 
> and
> 
> 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.
> 
> Hamish

I have cc'ed to the list. The experts may suggest.

Markus




More information about the grass-dev mailing list