[GRASS5] Re: [bug #2767] (grass) r.stats bug (due to recent G_store() fix?)

tlaronde at polynum.com tlaronde at polynum.com
Thu Dec 9 12:47:29 EST 2004


Hello Paul,

On Wed, Dec 08, 2004 at 08:51:04PM +0000, Paul Kelly wrote:
> Hello Thierry
> 
> 
[snipped]

> The first line here is more or less what G_store() does. But the recent 
> change added
> if ( s == NULL ) return NULL ;
> to G_store.
> 
> Perhaps (going by what you suggest above) it should look more like:
> char *G_store (char *s)
> {
>   char *buf;
> 
>   if ( s == NULL )
>     *buf = '\0';

You have to allocate place for buf to point to even if you only want to
store '\0'. Here buf has some random value. If, on some systems, even
the automatic variables are initialized to zero, or if it happens that
the value of buf is 0, the program will core dump (SEGFAULT). If the
(random since not initialized) value of buf happens to be a valid
address pointing to some free space or not used values, you will be able
to store the '\0' without segfaulting, but for a long run program, it
will probably happen at some moment that another piece of code use
willingly this place (that has not be reserved by *alloc() ) and stores
something else (no more '\0' and you will generate a random output).

so here too :

if (s == NULL) 
	buf = (char *) calloc(1,1); /* alloc 1 byte and zeroes */

but may I suggest that the purpose of G_store() is not to transform a
NULL in a zero length string and that probably, depending on the bug you
are trying to fix, it will be better to keep G_store in its present
state and use for example a test in the caller (if G_store returns NULL,
print an empty string or whatever---I haven't follow the history of the
problem so I don't know the context).

>[..] 
> The funny thing is I am unable to reproduce this bug so cannot test. 
> Running 'r.stats -anC fields' in Spearfish with yesterday's 5.7 CVS on IRIX 
> produces normal-looking output.

Precisely due to the random value of buf, which depends on the
environment and the compiler.

Cheers,
-- 
Thierry Laronde (Alceste) <tlaronde +AT+ polynum +dot+ com>
http://www.kergis.org/  |  http://www.kergis.com/
Key fingerprint = 0FF7 E906 FBAF FE95 FD89  250D 52B1 AE95 6006 F40C




More information about the grass-dev mailing list