Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i.atcorr: fix out of bound access in trunca, os and iso #4493

Merged
merged 4 commits into from
Oct 18, 2024

Conversation

ymdatta
Copy link
Contributor

@ymdatta ymdatta commented Oct 11, 2024

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.

@ymdatta
Copy link
Contributor Author

ymdatta commented Oct 11, 2024

I want someone familiar with the implementation details to review this and see if this is alright. Most of the variables changed here are internal variables and are specific to the implementation. Thanks!

@github-actions github-actions bot added C++ Related code is in C++ module imagery labels Oct 11, 2024
@metzm
Copy link
Contributor

metzm commented Oct 15, 2024

The original code has been written in Fortran which uses 1 based indices. During translation to C, not everything was translated properly, particularly the adjusted of 1 based indices to zero-based indices. The correct fix would probably be to use k = i in https://github.com/OSGeo/grass/blob/main/imagery/i.atcorr/computations.cpp#L105 and kk = i in https://github.com/OSGeo/grass/blob/main/imagery/i.atcorr/computations.cpp#L112
Usage of kk then needs to be adjusted in https://github.com/OSGeo/grass/blob/main/imagery/i.atcorr/computations.cpp#L121 to for (i = kk; i < 83; i++) {

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]>
@ymdatta
Copy link
Contributor Author

ymdatta commented Oct 15, 2024

Made changes to reflect the suggestions.

ymdatta and others added 2 commits October 16, 2024 22:49
When plane layer couldn't be determined, throw out an error and exit.

Co-authored-by: Markus Metz <[email protected]>
When plane layer couldn't be determined, throw out an error and exit.

Co-authored-by: Markus Metz <[email protected]>
metzm
metzm previously approved these changes Oct 17, 2024
Signed-off-by: Mohan Yelugoti <[email protected]>
@echoix
Copy link
Member

echoix commented Oct 17, 2024

I'll proxy the approval once it passes later tonight

@echoix echoix merged commit d788eee into OSGeo:main Oct 18, 2024
26 checks passed
@github-actions github-actions bot added this to the 8.5.0 milestone Oct 18, 2024
@a0x8o a0x8o mentioned this pull request Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C++ Related code is in C++ imagery module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants