Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ func (d Decimal) quoRem(d2 Decimal, precision int32, bb *big.Int) (Decimal, Deci
}
scale := -precision
e := int64(d.exp) - int64(d2.exp) - int64(scale)
if e > math.MaxInt32 || e < math.MinInt32 {
if precision == math.MinInt32 || e > math.MaxInt32 || e < math.MinInt32 {
panic("overflow in decimal QuoRem")
}
if bb == nil {
Expand Down
10 changes: 10 additions & 0 deletions decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2542,6 +2542,16 @@ func TestDecimal_Overflow(t *testing.T) {
if !didPanic(func() { New(1, math.MaxInt32).Mul(New(1, math.MaxInt32)) }) {
t.Fatalf("should have gotten an overflow panic")
}
// -math.MinInt32 wraps back to itself as an int32.
if !didPanic(func() { New(1, math.MinInt32).QuoRem(New(1, 0), math.MinInt32) }) {
t.Fatalf("should have gotten an overflow panic")
}
if !didPanic(func() { New(1, math.MinInt32).DivRound(New(1, 0), math.MinInt32) }) {
t.Fatalf("should have gotten an overflow panic")
}
if q, _ := New(1, math.MaxInt32).QuoRem(New(1, 0), math.MinInt32+1); !q.Equal(New(1, math.MaxInt32)) {
t.Fatalf("precision math.MinInt32+1 should still work, got %s", q)
}
}

func TestDecimal_ExtremeValues(t *testing.T) {
Expand Down
Loading