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

[BUG] runtime error: slice bounds out of range [23:21] while escaping #507

Open
alexey-slivkin opened this issue May 8, 2024 · 0 comments

Comments

@alexey-slivkin
Copy link

Problem: panic while escaping.

Example:

package main

import (
	"encoding/json"
	"fmt"
	goccy "github.com/goccy/go-json"
	"log"
)

func main() {

	rawData := []byte(`{"steps": [{"config":{"message": "Fail 
<"}}]}`)

	type config struct {
		Steps []struct {
			Config json.RawMessage `json:"config"`
		}
	}

	v1 := config{}

	if err := json.Unmarshal(rawData, &v1); err != nil {
		log.Fatal(err)
	}

	dataFromSTD, err := json.Marshal(v1) // {"Steps":[{"config":{"message":"Fail \u2028\u003c"}}]}
	if err != nil {
		log.Fatal(err)
	}

	v2 := config{}
	if err := goccy.Unmarshal(rawData, &v2); err != nil {
		log.Fatal(err)
	}

	// https://github.com/goccy/go-json/blob/master/internal/encoder/compact.go#L208
	dataFromGOCCY, err := goccy.Marshal(v1) // panic: runtime error: slice bounds out of range [23:21]
	if err != nil {
		log.Fatal(err)
	}

	if string(dataFromSTD) == string(dataFromGOCCY) {
		fmt.Println("OK")
		return
	}

	log.Fatal("diff")
}

Original:
main.txt

Result:

panic: runtime error: slice bounds out of range [23:21]

goroutine 1 [running]:
github.com/goccy/go-json/internal/encoder.compactString({0x140000e6000?, 0x0?, 0x220000000000?}, {0x140000da060, 0x19, 0x30}, 0x400?, 0x1)
        /Users/test/projects/goccy-test/vendor/github.com/goccy/go-json/internal/encoder/compact.go:208 +0x6a8

Expected result:

OK

It seems this problem is being solved in https://github.com/goccy/go-json/pull/479/files

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

No branches or pull requests

1 participant