]> Cypherpunks repositories - gostls13.git/commitdiff
strconv: fix for parseFloatPrefix
authorRobert Griesemer <gri@golang.org>
Thu, 30 Apr 2020 20:24:05 +0000 (13:24 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 30 Apr 2020 21:34:51 +0000 (21:34 +0000)
parseFloatPrefix accepts a string if it has a valid floating-point
number as prefix. Make sure that "infi", "infin", ... etc. are
accepted as valid numbers "inf" with suffix "i", "in", etc. This
is important for parsing complex numbers such as "0+infi".

This change does not affect the correctness of ParseFloat because
ParseFloat rejects strings that contain a suffix after a valid
floating-point number.

Updates #36771.

Change-Id: Ie1693a8ca2f8edf07b57688e0b35751b7100d39d
Reviewed-on: https://go-review.googlesource.com/c/go/+/231237
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/strconv/atof.go
src/strconv/atof_test.go

index c1e9907e09a96351451ab4e46a3a67ba1dcbbd7e..28ad094080196799c7228ecb026aaa0b52654bf1 100644 (file)
@@ -55,7 +55,11 @@ func special(s string) (f float64, n int, ok bool) {
                fallthrough
        case 'i', 'I':
                n := commonPrefixLenIgnoreCase(s, "infinity")
-               // both "inf" and "infinity" are ok
+               // Anything longer than "inf" is ok, but if we
+               // don't have "infinity", only consume "inf".
+               if 3 < n && n < 8 {
+                       n = 3
+               }
                if n == 3 || n == 8 {
                        return math.Inf(sign), nsign + n, true
                }
index 8201e75af66c332a4881293961085dfeae44a295..c30cb2e0feb902ce7432bae6c26c1f29be73c40f 100644 (file)
@@ -486,8 +486,9 @@ func TestParseFloatPrefix(t *testing.T) {
                        continue
                }
                // Adding characters that do not extend a number should not invalidate it.
-               // Test a few.
-               for _, suffix := range []string{" ", "q", "+", "-", "<", "=", ">", "(", ")"} {
+               // Test a few. The "i" and "init" cases test that we accept "infi", "infinit"
+               // correctly as "inf" with suffix.
+               for _, suffix := range []string{" ", "q", "+", "-", "<", "=", ">", "(", ")", "i", "init"} {
                        in := test.in + suffix
                        _, n, err := ParseFloatPrefix(in, 64)
                        if err != nil {