Skip to content

Commit

Permalink
math.big: fix 1/11579208923731619542357098500868790785326998466564056…
Browse files Browse the repository at this point in the history
…4039457584007908834671663 leading to panic (fix #23771)
  • Loading branch information
spytheman committed Feb 20, 2025
1 parent c5b26c4 commit 6d017f3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
11 changes: 6 additions & 5 deletions vlib/math/big/big_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ const sub_test_data = [
SubTest{ 2, 100, -98},
SubTest{ -100, -2, -98},
SubTest{ 100, -2, 102},
SubTest{ 2, -100, 102},
SubTest{ 2, -100, 102},
//
SubTest{ 2, 3, -1 },
SubTest{ 3, 2, 1 },
Expand All @@ -218,10 +218,10 @@ struct MulTest {
// vfmt off
const mul_test_data = [
MulTest{ 2, 3, 6 },
MulTest{ -2, 0, 0},
MulTest{ 2, 0, 0},
MulTest{ 0, -2, 0},
MulTest{ 0, -2, 0},
MulTest{ -2, 0, 0},
MulTest{ 2, 0, 0},
MulTest{ 0, -2, 0},
MulTest{ 0, -2, 0},
MulTest{ -869, 789, -685641 },
MulTest{ 869, -789, -685641 },
MulTest{ -869, -789, 685641 },
Expand Down Expand Up @@ -264,6 +264,7 @@ const div_mod_test_data = [
'629648864382619361826',
'2724578611525334851445652767465274410979805962941953382558409365935061481311529445551691298696266856092833571769883246719',
},
DivModTest{'1', '115792089237316195423570985008687907853269984665640564039457584007908834671663', '0', '1'},
]
// vfmt on

Expand Down
5 changes: 3 additions & 2 deletions vlib/math/big/integer.v
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ pub fn integer_from_bytes(oinput []u8, config IntegerConfig) Integer {
}
input := oinput[first_non_zero_index..]
// pad input
mut padded_input := []u8{len: ((input.len + 3) & ~0x3) - input.len, cap: (input.len + 3) & ~0x3}
mut padded_input := []u8{len: int_max(0, ((input.len + 3) & ~0x3) - input.len), cap: (
input.len + 3) & ~0x3}
padded_input << input
mut digits := []u32{len: padded_input.len / 4}
// combine every 4 bytes into a u32 and insert into n.digits
Expand Down Expand Up @@ -421,7 +422,7 @@ fn (dividend Integer) div_mod_internal(divisor Integer) (Integer, Integer) {
}
}
// Division for positive integers
mut q := []u32{cap: dividend.digits.len - divisor.digits.len + 1}
mut q := []u32{cap: int_max(1, dividend.digits.len - divisor.digits.len + 1)}
mut r := []u32{cap: dividend.digits.len}
divide_digit_array(dividend.digits, divisor.digits, mut q, mut r)
quotient := Integer{
Expand Down

0 comments on commit 6d017f3

Please sign in to comment.