// avoid performance bugs
panic("validate called but debugFloat is not set")
}
+ if msg := x.validate0(); msg != "" {
+ panic(msg)
+ }
+}
+
+func (x *Float) validate0() string {
if x.form != finite {
- return
+ return ""
}
m := len(x.mant)
if m == 0 {
- panic("nonzero finite number with empty mantissa")
+ return "nonzero finite number with empty mantissa"
}
const msb = 1 << (_W - 1)
if x.mant[m-1]&msb == 0 {
- panic(fmt.Sprintf("msb not set in last word %#x of %s", x.mant[m-1], x.Text('p', 0)))
+ return fmt.Sprintf("msb not set in last word %#x of %s", x.mant[m-1], x.Text('p', 0))
}
if x.prec == 0 {
- panic("zero precision finite number")
+ return "zero precision finite number"
}
+ return ""
}
// round rounds z according to z.mode to z.prec bits and sets z.acc accordingly.
"encoding/gob"
"encoding/json"
"io"
+ "strings"
"testing"
)
}
}
}
+
+func TestFloatGobDecodeInvalid(t *testing.T) {
+ for _, tc := range []struct {
+ buf []byte
+ msg string
+ }{
+ {
+ []byte{0x1, 0x2a, 0x20, 0x20, 0x20, 0x20, 0x0, 0x20, 0x20, 0x20, 0x0, 0x20, 0x20, 0x20, 0x20, 0x0, 0x0, 0x0, 0x0, 0xc},
+ "Float.GobDecode: msb not set in last word",
+ },
+ {
+ []byte{1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0},
+ "Float.GobDecode: nonzero finite number with empty mantissa",
+ },
+ } {
+ err := NewFloat(0).GobDecode(tc.buf)
+ if err == nil || !strings.HasPrefix(err.Error(), tc.msg) {
+ t.Errorf("expected GobDecode error prefix: %s, got: %v", tc.msg, err)
+ }
+ }
+}