Skip to content

Commit

Permalink
i.atcorr: fix out of bound access in trunca, os and iso (#4493)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Update imagery/i.atcorr/computations.cpp

When plane layer couldn't be determined, throw out an error and exit.

Co-authored-by: Markus Metz <[email protected]>

* Update imagery/i.atcorr/computations.cpp

When plane layer couldn't be determined, throw out an error and exit.

Co-authored-by: Markus Metz <[email protected]>

* Fix clang-format issues

Signed-off-by: Mohan Yelugoti <[email protected]>

---------

Signed-off-by: Mohan Yelugoti <[email protected]>
Co-authored-by: Markus Metz <[email protected]>
  • Loading branch information
ymdatta and metzm authored Oct 18, 2024
1 parent 3c67825 commit d788eee
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions imagery/i.atcorr/computations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit d788eee

Please sign in to comment.