From d788eee193b3e3a4d10194775cec9d4429802403 Mon Sep 17 00:00:00 2001 From: Mohan Yelugoti Date: Fri, 18 Oct 2024 00:31:28 -0400 Subject: [PATCH] i.atcorr: fix out of bound access in trunca, os and iso (#4493) * i.atcorr: fix out of bound access in trunca, os and iso In trunca(), under 2 if conditionals (if rumu[1] > 0.8 and rmu[1] > 0.94), k and kk values are set to -1 respectively. When these values are used to access an array, it leads to out of bound access, which is undefined behavior in c++. Similarly in os() and iso(), if `taup < h[i]` for all values, iplane would still be -1 and we would be accessing an array with this as an index, which is undefined behavior. In both cases, to avoid that, set that index to zero. This was found using cppcheck tool. Signed-off-by: Mohan Yelugoti * Update imagery/i.atcorr/computations.cpp When plane layer couldn't be determined, throw out an error and exit. Co-authored-by: Markus Metz <33666869+metzm@users.noreply.github.com> * Update imagery/i.atcorr/computations.cpp When plane layer couldn't be determined, throw out an error and exit. Co-authored-by: Markus Metz <33666869+metzm@users.noreply.github.com> * Fix clang-format issues Signed-off-by: Mohan Yelugoti --------- Signed-off-by: Mohan Yelugoti Co-authored-by: Markus Metz <33666869+metzm@users.noreply.github.com> --- imagery/i.atcorr/computations.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/imagery/i.atcorr/computations.cpp b/imagery/i.atcorr/computations.cpp index 53f8664ee06..6e5a38c5b07 100644 --- a/imagery/i.atcorr/computations.cpp +++ b/imagery/i.atcorr/computations.cpp @@ -102,14 +102,14 @@ double trunca() for (i = 0; i < 83; i++) { if (rmu[i] > 0.8) break; - k = i - 1; + k = i; } int kk = 0; for (i = 0; i < 83; i++) { if (rmu[i] > 0.94) break; - kk = i - 1; + kk = i; } double aa = @@ -118,7 +118,7 @@ double trunca() double x1 = (double)(log10(sixs_trunc.pha[kk])); double x2 = (double)acos(rmu[kk]); - for (i = kk + 1; i < 83; i++) { + for (i = kk; i < 83; i++) { double a; if (fabs(rmu[i] - 1) <= 1e-08) a = x1 - aa * x2; @@ -445,9 +445,14 @@ void os(const double tamoy, const double trmoy, const double pizmoy, /* compute position of the plane layer */ double taup = tap + trp; iplane = -1; - for (int i = 0; i <= ntp; i++) + for (int i = 0; i <= ntp; i++) { if (taup >= h[i]) iplane = i; + } + if (iplane == -1) { + G_fatal_error( + _("Position of the plane layer could not be determined")); + } /* update the layer from the end to the position to update if necessary */ @@ -1006,9 +1011,14 @@ void iso(const double tamoy, const double trmoy, const double pizmoy, /* compute position of the plane layer */ double taup = tap + trp; iplane = -1; - for (int i = 0; i <= ntp; i++) + for (int i = 0; i <= ntp; i++) { if (taup >= h[i]) iplane = i; + } + if (iplane == -1) { + G_fatal_error( + _("Position of the plane layer could not be determined")); + } /* update the layer from the end to the position to update if necessary */