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

[float string]different result vs origin after call Marshal & Unmarshal #510

Closed
hanxiatu-fc opened this issue Nov 4, 2020 · 2 comments · Fixed by #513
Closed

[float string]different result vs origin after call Marshal & Unmarshal #510

hanxiatu-fc opened this issue Nov 4, 2020 · 2 comments · Fixed by #513
Assignees
Labels

Comments

@hanxiatu-fc
Copy link

hanxiatu-fc commented Nov 4, 2020

testcase:
{"":[9020000000039351e-5]}

testcode:

func test(data []byte) {
	create := func() interface{} { m := map[string]interface{}{}; return &m }
	v := create()
	v1 := create()

	_ = json.Unmarshal(data, v)
	data1, _ := json.Marshal(v)
	_ = json.Unmarshal(data1, v1)

	fmt.Printf("v0: %#v, v1: %#v\n", v, v1)
}

when call test with "encoding/json" , get result:

v0: &map[string]interface {}{"":[]interface {}{9.020000000039351e+10}}, v1: &map[string]interface {}{"":[]interface {}{9.020000000039351e+10}}

when call test with json-iterator ,get result :

v0: &map[string]interface {}{"":[]interface {}{9.020000000039351e+10}}, v1: &map[string]interface {}{"":[]interface {}{9.020000000039352e+10}}

9.020000000039351e+10 vs 9.020000000039352e+10

How did I find the problem?

I refer to the test cases and test corpus for std json in the go-fuzz project to test json-iterator.

see : https://github.com/dvyukov/go-fuzz-corpus/tree/master/json

@AllenX2018
Copy link
Collaborator

AllenX2018 commented Nov 16, 2020

It's the nature of double precision floating number(IEEE 754).
90200000000.39351 can't be present as it is since it's overflowed the max fraction part of float64 (1<<53-1)

9020000000039351
9007199254740992

https://en.wikipedia.org/wiki/Double-precision_floating-point_format

You can try

	f, err := strconv.ParseFloat("9020000000039351e-5", 64)
	if err != nil {
		return
	}
	fmt.Printf("%0.16f\n", f)
	fp := float64(9020000000039351)/float64(100000)
	fmt.Printf("%0.16f\n", fp )
	fmt.Println(fp == f)

// 90200000000.3935089111328125
// 90200000000.3935241699218750
// false

Although we can fix it by running the slow path if value is over 1<<53-1

@catenacyber
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants