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

Fix buffer overflow from scoring bin rounding #864

Merged
merged 1 commit into from
Jun 27, 2022

Conversation

mxxo
Copy link
Contributor

@mxxo mxxo commented Apr 4, 2022

Before this change, there was a small risk of buffer overflow in the
tutor7pp fluence scoring arrays. The value r2 was checked to be less
than 400, and then the scoring bin number (from 0 to 199) is calculated
using the expression (int)(sqrt(r2)*10.). The problem with this check
is that certain double precision values less than 400 can nevertheless
have a square root equal to exactly 20.0 due to floating-point rounding.

For example, taking the square root of the double 399.99999999999994
results in 20.0 under certain conditions, not a value slightly less than
20.0 as would be expected. The default g++ rounding mode is
round-to-nearest, so even with the square root's true value being under
20.0, the resulting double can be rounded upwards to 20.0 if the true
value is closer to 20.0 than the previous representable float. Then,
scoring in bin 200 results in buffer overflow in the scoring array.

This change ensures the resulting scoring array bin integer is in bounds
(< 200), eliminating the potential for buffer overflow.

Fixes #863.

@mxxo mxxo requested a review from a team as a code owner April 4, 2022 22:18
@rtownson rtownson self-assigned this May 12, 2022
@rtownson rtownson added the bug label May 12, 2022
Before this change, there was a small risk of buffer overflow in the
tutor7pp fluence scoring arrays. The value r2 was checked to be less
than 400, and then the scoring bin number (from 0 to 199) is calculated
using the expression (int)(sqrt(r2)*10.). The problem with this check is
that certain double precision values less than 400 can nevertheless have
a square root equal to exactly 20.0 due to floating-point rounding.

For example, taking the square root of the double 399.99999999999994
results in 20.0 under certain conditions, not a value slightly less than
20.0 as would be expected. The default g++ rounding mode is
round-to-nearest, so even with the square root's true value being under
20.0, the resulting double can be rounded upwards to 20.0 if the true
value is closer to 20.0 than the previous representable float. Then,
scoring in bin 200 results in buffer overflow in the scoring array.

This change ensures the resulting scoring array bin integer is in bounds
(< 200), eliminating the potential for buffer overflow.
@ftessier ftessier force-pushed the tutor7pp-oob-fluence-scoring branch from e30ccda to 067f00a Compare June 27, 2022 18:39
@ftessier ftessier merged commit 7d5e2ce into nrc-cnrc:develop Jun 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants