gf icon indicating copy to clipboard operation
gf copied to clipboard

解析NaN为int,会出现一个很大的数,是故意为之,还是BUG

Open sanrentai opened this issue 3 years ago • 0 comments

1. What version of Go and system type/arch are you using?

go version go1.17.1 windows/amd64

2. What version of GoFrame are you using?

v2.1.1

3. Can this issue be re-produced with the latest release?

yes

4. What did you do?


// 浏览器访问 http://localhost:8199/?page=NaN
func main() {
	s := g.Server()
	s.BindHandler("/", func(r *ghttp.Request) {
		r.Response.Write(fmt.Sprintf("Page:%v", r.Get("page").Int()))
	})
	s.SetPort(8199)
	s.Run()
}

5. What did you expect to see?

Page:0

6. What did you see instead?

Page:-9223372036854775808

sanrentai avatar Aug 09 '22 07:08 sanrentai

https://pkg.go.dev/strconv#ParseFloat

NaN / Inf / Infinity 是 Golang 的一些特殊值,在解析为 float64 的时候他们不会发生异常

	v, _ := strconv.ParseFloat("NaN", 64)
	fmt.Printf("type = %T, raw = %v , int64 =  %v\n",v, v,int32(v))

	v,_ = strconv.ParseFloat("Infinity",64)
	fmt.Printf("type = %T, raw = %v , int64 =  %v\n",v, v,int64(v))

	v,_ = strconv.ParseFloat("-Inf",64)
	fmt.Printf("type = %T, raw = %v , int64 =  %v\n",v, v,int64(v))

当中 -inf 表现是正常的,跟预期一致。但是在 Windows 某些低版本 或 Mac 1.16 以下的版本中,将float64类型的 NaN 转换到 int / uint 的时候他们会变成一个边界值(即目标类型的最大或最小值).

type = float64, raw = NaN , int64 =  -2147483648
type = float64, raw = +Inf , int64 =  -9223372036854775808
type = float64, raw = -Inf , int64 =  -9223372036854775808

我们会在讨论过后决定是否对其进行修复,感谢您的反馈

DGuang21 avatar Aug 12 '22 08:08 DGuang21

https://github.com/gogf/gf/pull/2064

DGuang21 avatar Aug 12 '22 08:08 DGuang21