[GRASS-dev] strange for loop bug

Glynn Clements glynn at gclements.plus.com
Tue Apr 1 08:14:33 EDT 2008


Yann Chemin wrote:

> I am writing a raster module for image processing, and the
> for(col=0;col<ncols;col++) loop behaves strangely (segfault), the
> number of lines of code inside is 239 lines. If i reduce the number of
> functions inside to a simple copy of an input raster, the number of
> line being 15 less, then it does not seg fault.
> 
> The error is basically the variable col becoming a very large negative
> number, which is constant for a compilation, may not be the same
> actually in all modifications of the code tested.
> 
> The interesting thing is that it processes through 77 rows of Null
> pixels (sending outrast[col] to G_set_d_null_value(..)) and when it
> comes to the first real data processing, it gives the output value to
> outrast[col], goes up the loop and at that moment, col passes from
> 1450 to -2085319823.
> 
> Anybody has any experience of similar event?
> Any idea what could corrupt a for() loop variable?

This looks very much like a buffer overflow, i.e. you're writing
outside of the bounds of a array and overwriting "col".

> I am also going to change gcc version in case it is a gcc bug.

Using a different compiler version may hide the bug. It's up to the
compiler how the function's stack frame is arranged, so if you use a
different compiler, you may end up overwriting a different variable,
possibly one which doesn't matter at the point that the corruption
occured.

While compilers do occasionally have bugs, its more likely that the
bug is in your code.

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-dev mailing list