// away from the largest floating point number of the given component's size,
// ParseComplex returns err.Err = ErrRange and c = ±Inf for the respective component.
func ParseComplex(s string, bitSize int) (complex128, error) {
- if bitSize != 64 && bitSize != 128 {
- return 0, bitSizeError(fnParseComplex, s, bitSize)
+ size := 64
+ if bitSize == 64 {
+ size = 32 // complex64 uses float32 parts
}
- size := bitSize >> 1
orig := s
}
}
-func TestParseComplexInvalidBitSize(t *testing.T) {
- _, err := ParseComplex("1+2i", 100)
- const want = `strconv.ParseComplex: parsing "1+2i": invalid bit size 100`
- if err == nil {
- t.Fatalf("got nil error, want %q", want)
- }
- if err.Error() != want {
- t.Fatalf("got error %q, want %q", err, want)
+// Issue 42297: allow ParseComplex(s, not_32_or_64) for legacy reasons
+func TestParseComplexIncorrectBitSize(t *testing.T) {
+ const s = "1.5e308+1.0e307i"
+ const want = 1.5e308 + 1.0e307i
+
+ for _, bitSize := range []int{0, 10, 100, 256} {
+ c, err := ParseComplex(s, bitSize)
+ if err != nil {
+ t.Fatalf("ParseComplex(%q, %d) gave error %s", s, bitSize, err)
+ }
+ if c != want {
+ t.Fatalf("ParseComplex(%q, %d) = %g (expected %g)", s, bitSize, c, want)
+ }
}
}
// ParseFloat recognizes the strings "NaN", and the (possibly signed) strings "Inf" and "Infinity"
// as their respective special floating point values. It ignores case when matching.
func ParseFloat(s string, bitSize int) (float64, error) {
- if bitSize != 32 && bitSize != 64 {
- return 0, bitSizeError(fnParseFloat, s, bitSize)
- }
f, n, err := parseFloatPrefix(s, bitSize)
if err == nil && n != len(s) {
return 0, syntaxError(fnParseFloat, s)
t.Logf("tested %d float32's", count)
}
-func TestParseFloatInvalidBitSize(t *testing.T) {
- _, err := ParseFloat("3.14", 100)
- const want = `strconv.ParseFloat: parsing "3.14": invalid bit size 100`
- if err == nil {
- t.Fatalf("got nil error, want %q", want)
- }
- if err.Error() != want {
- t.Fatalf("got error %q, want %q", err, want)
+// Issue 42297: a lot of code in the wild accidentally calls ParseFloat(s, 10)
+// or ParseFloat(s, 0), so allow bitSize values other than 32 and 64.
+func TestParseFloatIncorrectBitSize(t *testing.T) {
+ const s = "1.5e308"
+ const want = 1.5e308
+
+ for _, bitSize := range []int{0, 10, 100, 128} {
+ f, err := ParseFloat(s, bitSize)
+ if err != nil {
+ t.Fatalf("ParseFloat(%q, %d) gave error %s", s, bitSize, err)
+ }
+ if f != want {
+ t.Fatalf("ParseFloat(%q, %d) = %g (expected %g)", s, bitSize, f, want)
+ }
}
}