Skip to content

Commit 4b82977

Browse files
committed
public/schema: bound fractional digits in canonical decimal parser
len(fracPart) is a 64-bit int. The downstream casts to int32 (the scale type) in ParseDecimal and ParseBigDecimal would wrap silently on a fractional part longer than math.MaxInt32 — the BigDecimal path would return a negative scale and the Decimal path would short-circuit its "exceeds scale" check. Bound the fractional length in parseCanonicalDecimal with an explicit error so both parsers fail loudly rather than returning corrupt output.
1 parent 9b6f8c4 commit 4b82977

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

public/schema/decimal.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package schema
55
import (
66
"errors"
77
"fmt"
8+
"math"
89
"math/big"
910
"strings"
1011
)
@@ -168,6 +169,14 @@ func parseCanonicalDecimal(s string) (sign, intPart, fracPart string, err error)
168169
return "", "", "", err
169170
}
170171

172+
// Cap the fractional length so callers downstream can safely cast it to
173+
// the int32 scale type without silent wrap-around. The integer part is
174+
// not bounded here — its length only feeds big.Int.SetString, which
175+
// handles arbitrary lengths correctly (if slowly).
176+
if len(fracPart) > math.MaxInt32 {
177+
return "", "", "", fmt.Errorf("decimal string has %d fractional digits, exceeds maximum %d", len(fracPart), math.MaxInt32)
178+
}
179+
171180
return sign, intPart, fracPart, nil
172181
}
173182

0 commit comments

Comments
 (0)