Description
Describe the bug
The i.cca
module crashes with a segmentation fault (exit code -11
) when run with valid signature and group inputs. This is caused by off-by-one indexing errors in the C source code: arrays allocated with size n
are accessed using 1-based indexing (1..n
), instead of the correct 0-based indexing (0..n-1
), leading to invalid memory access.
To reproduce
- Set region with sufficient size
g.region n=20 s=0 e=20 w=0 rows=200 cols=200
- Generate input rasters and a training map with at least 3 classes.
r.mapcalc expression="band1 = 50 + 30 * sin(row() / 20.0) + 20 * cos(col() / 15.0)"
r.mapcalc expression="band2 = 40 + 25 * exp(-(((row() - 100)^2 + (col() - 100)^2) / 2000.0))"
r.mapcalc expression="band3 = 30 + (row() + col()) / 10.0"
r.mapcalc "cca_training = if(row() < 60 && col() < 60, 1, \
if(row() >= 60 && row() < 140 && col() >= 60 && col() < 140, 2, \
if(row() >= 140 && col() >= 140, 3, null())))"
- Create an imagery group and subgroup with 3 raster bands.
i.group group=cca_group subgroup=cca_subgroup input=band1,band2,band3
- Generate a signature file using
i.gensig
.
i.gensig trainingmap=cca_training group=cca_group subgroup=cca_subgroup signaturefile=cca_sig
-
Run
i.cca
with the generated group, subgroup, and signature file:i.cca group=cca_group subgroup=cca_subgroup signature=cca_sig output=cca_output
-
Observe a segmentation fault (crash).
Expected behavior
The module should successfully compute canonical components and write output rasters, as long as valid inputs and signatures are provided.
Screenshots
System description
- Operating System: Windows Subsystem for Linux (WSL)
- GRASS GIS version: 8.4
- details about further software components
- GRASS 8.5.0dev
- Python verison: 3.10.12
- wxPython version: 4.2.2
Possible Solution
After debugging, I found out that the core issue lies in main.c
of i.cca
, where vectors and matrices (e.g., mu
, cov
, nsamp
) are allocated for nclass
or bands
, but loop indices start from 1
to n
, exceeding bounds. This causes invalid memory writes and reads, leading to segmentation faults on inputs.
The fix is implemented in #5948, which closes this issue.