From f0acd204c60fda556babbc7c32d1e5298d7acaeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C4=81ris=20Narti=C5=A1s?= Date: Tue, 3 Jan 2023 08:55:51 +0200 Subject: [PATCH] i.maxlik: fix crash when classification result is NULL (#2724) If any input cell from imagery group is NULL, the result of classification is also NULL. The implementation of original cat restoration (79f9500) did not check for this corner case and thus caused an out of bounds value access. --- imagery/i.maxlik/main.c | 3 ++- imagery/i.maxlik/testsuite/test_i_maxlik.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/imagery/i.maxlik/main.c b/imagery/i.maxlik/main.c index 5b083b29e70..1ffdbf260f9 100644 --- a/imagery/i.maxlik/main.c +++ b/imagery/i.maxlik/main.c @@ -124,7 +124,8 @@ int main(int argc, char *argv[]) for (int col = 0; col < ncols; col++) { /* Predicted classes start at 1 but signature array is 0 based */ - class_cell[col] = S.sig[class_cell[col] - 1].oclass; + if (Rast_is_c_null_value(&class_cell[col]) == 0) + class_cell[col] = S.sig[class_cell[col] - 1].oclass; } } Rast_put_row(class_fd, class_cell, CELL_TYPE); diff --git a/imagery/i.maxlik/testsuite/test_i_maxlik.py b/imagery/i.maxlik/testsuite/test_i_maxlik.py index e6ad2aca2d7..1f6829e92b4 100644 --- a/imagery/i.maxlik/testsuite/test_i_maxlik.py +++ b/imagery/i.maxlik/testsuite/test_i_maxlik.py @@ -66,7 +66,7 @@ def setUpClass(cls): ) cls.runModule( "r.mapcalc", - expression=f"{cls.b2}=5.0+rand(-1.0,1.0)", + expression=f"{cls.b2}=if(row() == 3 && col() == 3, null(), 5.0+rand(-1.0,1.0))", flags="s", quiet=True, )