[GRASS-dev] strange for loop bug

Hello,

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?

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

Thank you,
Yann

Hi again,

Well, this was a gcc-4.3.1 20080321 version for Debian

I tried gcc-4.2.3 also for Debian and it crashed while giving the
processed pixel to outrast[col]...

Finally went to gcc-4.1.3 20080308 version for Debian (4.1.2-21) which
is working fine...

maybe of any use
Yann

On 01/04/2008, Yann Chemin <yann.chemin@gmail.com> wrote:

Hello,

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?

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

Thank you,

Yann

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@gclements.plus.com>