Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strconv: ParseFloat returns out of range error on invalid suffix #46628

Closed
itchyny opened this issue Jun 7, 2021 · 2 comments
Closed

strconv: ParseFloat returns out of range error on invalid suffix #46628

itchyny opened this issue Jun 7, 2021 · 2 comments
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@itchyny
Copy link
Contributor

itchyny commented Jun 7, 2021

What version of Go are you using (go version)?

$ go version
go version go1.16.4 darwin/arm64

Does this issue reproduce with the latest release?

Yes, behavior inconsistency against the document.
This is an undocumented breaking change between 1.14 and 1.15.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/itchyny/Library/Caches/go-build"
GOENV="/Users/itchyny/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/itchyny/.share/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/itchyny/.share/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/opt/homebrew/Cellar/go/1.16.4/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/homebrew/Cellar/go/1.16.4/libexec/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.16.4"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/ph/slcnxxv955n3zk1fzk50rmvr0000gn/T/go-build4154388589=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Execute the following code.

package main

import (
	"fmt"
	"strconv"
)

func main() {
	fmt.Println(strconv.ParseFloat("1e100xxx", 64))
	fmt.Println(strconv.ParseFloat("1e1000xxx", 64))
}

What did you expect to see?

Just as Go <= 1.14,

0 strconv.ParseFloat: parsing "1e100xxx": invalid syntax
0 strconv.ParseFloat: parsing "1e1000xxx": invalid syntax

The strconv.ParseFloat document says

If s is not syntactically well-formed, ParseFloat returns err.Err = ErrSyntax.

If s is syntactically well-formed but is more than 1/2 ULP away from the largest floating point number of the given size, ParseFloat returns f = ±Inf, err.Err = ErrRange.

The input 1e1000xxx here satisfies the first if statement just like 1e100xxx does, so the err.Err should be ErrSyntax, not ErrRange. They are not syntactically well-formed.

The change seems to be the side effect of 1d31f9b, which changed the error on unconsumed characters.

What did you see instead?

0 strconv.ParseFloat: parsing "1e100xxx": invalid syntax
+Inf strconv.ParseFloat: parsing "1e1000xxx": value out of range
@cherrymui cherrymui added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Jun 7, 2021
@cherrymui cherrymui added this to the Backlog milestone Jun 7, 2021
@cherrymui
Copy link
Member

cc @griesemer @ianlancetaylor

@gopherbot
Copy link

Change https://golang.org/cl/325750 mentions this issue: strconv: ParseFloat: always return ErrSyntax for bad syntax

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

3 participants