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

util: Adjust Fraction class addition overload overflow tests #2748

Conversation

jamescowens
Copy link
Member

@jamescowens jamescowens commented Mar 4, 2024

@Tahvok, the arch maintainer, caught a subtle error in the extreme overflow tests for the newly augmented Fraction class in the 5.4.6.0 release. Arch, unlike other distributions, uses the C++ compiler flag -D_GLIBCXX_ASSERTIONS, and this caught the error.

Essentially the problem is in the fraction addition overflow tests at the extreme negative case. When the fraction addition overload operator goes to add fractions of the same denominator, where both addends are std::numeric_limits<int64_t>::min() / 2, the result on the numerator will be std::numeric_limits<int64_t>::min(). When simplification is called, it will call std::gcd, which calls std::abs. The std::abs of std::numeric_limits<int64_t>::min() is an overflow, because it would be std::numeric_limits<int64_t>::max() + 1, but this is handled as an assert in glibc. So to prevent this, we tightened the negative side overflow constraint in overflow_add by 1 to ensure the numerator will never be less than std::numeric_limits<int64_t>::min() +1, so that the std::abs of that will succeed (i.e. will be std::numeric_limits<int64_t>::max()).

This is really esoteric and will not be reached by any actual wallet operation, but it is being treated as a hotfix to get Arch compiles to work.

Thanks to @div72 for helping me troubleshoot this.

@jamescowens jamescowens requested a review from div72 March 4, 2024 21:58
@jamescowens jamescowens self-assigned this Mar 4, 2024
@jamescowens jamescowens added the bug label Mar 4, 2024
@jamescowens jamescowens added this to the Miss Piggy milestone Mar 4, 2024
Copy link
Member

@div72 div72 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK 1269669.

@jamescowens jamescowens merged commit 325a127 into gridcoin-community:hotfix Mar 4, 2024
17 checks passed
@jamescowens
Copy link
Member Author

This PR has been merged also into master and tagged as 5.4.6.0-hotfix-1.

jamescowens added a commit to jamescowens/Gridcoin-Research that referenced this pull request Mar 13, 2024
Added
 - net, consensus: Ban nodes 5.4.5.0 and below gridcoin-community#2751 (@jamescowens)

Changed

Removed

Fixed
 - util: Adjust Fraction class addition overload overflow tests gridcoin-community#2748 (@jamescowens)
@jamescowens jamescowens deleted the fix_fraction_class_addition_overflow branch July 21, 2024 15:48
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.

2 participants