fix(decode): overflow-check uint and float32 destinations
Assisted-by: GLM 5.3
This commit is contained in:
@@ -223,6 +223,33 @@ func TestUnmarshalIntToUint64FitsMaxInt64(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalFloat32Overflow(t *testing.T) {
|
||||
// A finite float64 beyond the float32 range must not decode silently as
|
||||
// an infinity.
|
||||
type C struct {
|
||||
X float32 `toml:"x"`
|
||||
}
|
||||
var c C
|
||||
err := Unmarshal([]byte("x = 1e300\n"), &c)
|
||||
if err == nil {
|
||||
t.Fatal("expected overflow error for float32")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "overflow") {
|
||||
t.Errorf("err = %v, want substring 'overflow'", err.Error())
|
||||
}
|
||||
// Infinities themselves pass through, and in-range values are untouched.
|
||||
var ok C
|
||||
if err := Unmarshal([]byte("x = inf\n"), &ok); err != nil {
|
||||
t.Fatalf("inf should decode into float32, got %v", err)
|
||||
}
|
||||
if !math.IsInf(float64(ok.X), 1) {
|
||||
t.Errorf("X = %v, want +Inf", ok.X)
|
||||
}
|
||||
if err := Unmarshal([]byte("x = 1.5\n"), &ok); err != nil || ok.X != 1.5 {
|
||||
t.Fatalf("1.5 should decode into float32, got %v (X=%v)", err, ok.X)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalNegativeIntToUint(t *testing.T) {
|
||||
type C struct {
|
||||
X uint8 `toml:"x"`
|
||||
|
||||
Reference in New Issue
Block a user