[pull] master from mruby:master#167
Merged
pull[bot] merged 6 commits intosysfce2:masterfrom Dec 30, 2025
Merged
Conversation
Fix two functions that could create bigints with sn != 0 but value of 0: - mpz_mod_limb: single-limb case set r->sn = x->sn even when result was 0 - mpz_mul_2exp: set z->sn = sn unconditionally after zero-producing ops This inconsistent state caused GCD loop (!zero_p(&b)) to continue with a zero divisor, eventually causing FPE in mpz_mod_limb with m = 0. Co-authored-by: Claude <noreply@anthropic.com>
Fixed three bugs that caused infinite loops in GCD calculations: 1. mpz_set_int() didn't shrink sz when setting a smaller value. mpz_realloc() only grows allocations, so setting a 1-limb value to an mpz_t with sz=3 would leave sz=3, breaking algorithms that depend on correct sz values. 2. mpz_set_uint64() had the same issue. 3. mpz_gcd() used mpz_init_set() which preserves the sign. GCD should work with absolute values since gcd(a,b) = gcd(|a|,|b|). With negative inputs, the sign would oscillate during mod operations, preventing the Euclidean algorithm from converging. 4. mpz_div_2exp() when e==0 and z==x would corrupt data by calling mpz_init_heap() which overwrites z->p before copying from x. These bugs were discovered via ClusterFuzz with complex rational number calculations. Co-authored-by: Claude <noreply@anthropic.com>
In rational_new_f(), the code performed ((mrb_int)1)<<exp without checking if exp >= MRB_INT_BIT. Shifting by a value >= bit width is undefined behavior in C. Also fixed the negative exponent case which incorrectly used deno >>= exp (right-shift by negative is UB). The correct logic is deno <<= -exp to multiply denominator by 2^(-exp). Both cases now check for overflow before shifting and fall back to bigint operations when necessary. Discovered via ClusterFuzz with input "92r**11". Co-authored-by: Claude <noreply@anthropic.com>
When right-shifting by more bits than the number contains, the loop condition `i < x->sz - digs` would underflow (since size_t is unsigned), causing out-of-bounds memory access. Fixed by checking if digs >= x->sz upfront and returning zero in that case, since shifting right by more bits than the number has always yields zero. Discovered via ClusterFuzz with input "7<<78<<-772". Co-authored-by: Claude <noreply@anthropic.com>
Support negative modulus in Integer#pow(exp, mod) with proper Ruby semantics. Previously, negative modulus caused an infinite loop in Barrett reduction. Now: - Use absolute value of modulus for computation - Apply signed modulo adjustment (result + m for non-zero result when m is negative) - Add early return for zero base with positive exponent (0^n = 0) Co-authored-by: Claude <noreply@anthropic.com>
Use self.begin/self.end instead of first/last to compute hash for ranges. The first/last methods raise RangeError for endless/beginless ranges, but the internal begin/end accessors return nil safely. Co-authored-by: Claude <noreply@anthropic.com>
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
See Commits and Changes for more details.
Created by
pull[bot] (v2.0.0-alpha.4)
Can you help keep this open source service alive? 💖 Please sponsor : )