Fix edgecase segfault of BigDecimal#remainder#349
Merged
tompng merged 1 commit intoruby:masterfrom Jun 10, 2025
Merged
Conversation
There is a possibility where coerced remainder returns Qnil. NIL_P(BigDecimal_divremain(self, r, &d, &rv)) doesn't mean d and rv has a calculated value.
2ae530f to
ba4e15d
Compare
tompng
commented
Jun 10, 2025
Comment on lines
+2091
to
+2094
| if (BigDecimal_divremain(self, r, &d, &rv)) { | ||
| return VpCheckGetValue(rv); | ||
| } | ||
| return DoSomeOne(self, r, rb_intern("remainder")); |
Member
Author
There was a problem hiding this comment.
Same interface as BigDecimal_DoDivmod
if (BigDecimal_DoDivmod(self, b, &div, &mod)) {
return BigDecimal_to_i(VpCheckGetValue(div));
}
return DoSomeOne(self, b, rb_intern("div"));
tompng
commented
Jun 10, 2025
| } | ||
|
|
||
| if (!b) return DoSomeOne(self, r, rb_intern("remainder")); | ||
| if (!b) return Qfalse; |
Member
Author
There was a problem hiding this comment.
I don't think using Qfalse and Qtrue instead of false and true, but this pattern is frequently used in bigdecimal.c even if the function name starts with BigDecimal_.
But this pattern is used in is_zero, is_one and BigDecimal_DoDivmod.
I think it's better to refactor in a separate pull request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There is a possibility where coerced remainder returns
Qnil.NIL_P(BigDecimal_divremain(self, r, &d, &rv))doesn't meandandrvhas a calculated value.This pull request is kind of a refactoring, but also fixes this segmentation fault.