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

Addressing clang-UBSan Undefined Behavior Memcheck #12

Merged
merged 1 commit into from
Nov 6, 2023

Conversation

pegeler
Copy link
Contributor

@pegeler pegeler commented Oct 14, 2023

Purpose

This PR fixes undefined behavior uncovered by the CRAN clang-UBSan memcheck raised earlier this week. cran_comments.md goes into detail on how this bug can be reproduced.

Explanation

The bug was found and fixed in src/prime_factors.cpp. The prime_factors_ helper function takes an int input that is cast to double, passed to std::sqrt, then cast back to int. The return value of std::sqrt when encountering domain error (input < 0) is undefined but expected to be NaN, which cannot be represented when cast back to int. Thus we have undefined behavior.

The Fix

I have added an early exit for values that would cause a domain error.

@Ironholds Ironholds merged commit d199153 into Ironholds:master Nov 6, 2023
@pegeler pegeler deleted the prime-factors branch December 14, 2023 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants