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

got panic in GetArray() for $.. in Eval #81

Closed
OTCMKTS opened this issue Nov 17, 2024 · 3 comments
Closed

got panic in GetArray() for $.. in Eval #81

OTCMKTS opened this issue Nov 17, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@OTCMKTS
Copy link

OTCMKTS commented Nov 17, 2024

Code to repro:

func test2() {
	jb := []byte(`[{"a": 1, "b": 2, "c": 3}]`)
	root, err := ajson.Unmarshal(jb)
	if err != nil {
		fmt.Println("Got error1:", err)
		return
	}
	result, err := ajson.Eval(root, "$...")
	if err != nil {
		fmt.Println("Got error2:", err)
		return
	}

	arrs, err := result.GetArray()
	if err != nil {
		fmt.Println("Got error3:", err)
		return
	} else {
		fmt.Println("Try:", arrs)
		for _, arr := range arrs {
			if arr.Type() == ajson.Array {
				arr, err := arr.GetArray()
				if err != nil {
					fmt.Println("Got error4:", err)
					return
				} else {
					fmt.Println("Try:", arr)
				}
			}
		}
	}
}

Console output:

Try: [[{"a": 1, "b": 2, "c": 3}] {"a": 1, "b": 2, "c": 3} {"a": 1, "b": 2, "c": 3}]
panic: runtime error: index out of range [2] with length 1

goroutine 1 [running]:
github.com/spyzhov/ajson.(*Node).getValue(0x14000128720)
	github.com/spyzhov/ajson@v0.9.5/node.go:391 +0x66c
github.com/spyzhov/ajson.(*Node).GetArray(0x10224bad8?)
	github.com/spyzhov/ajson@v0.9.5/node.go:483 +0x30
main.test2()
	bug-ajson/main.go:39 +0x25c
main.main()
	bug-ajson/main.go:15 +0x20
make: *** [run] Error 2
@spyzhov
Copy link
Owner

spyzhov commented Nov 25, 2024

Hello,
Thanks for the issue, I will fix it asap.

@spyzhov
Copy link
Owner

spyzhov commented Nov 25, 2024

Thanks once again for the issue, it looks much deeper than just a small fix, it was caused because all Nodes referred to the same downstream objects, so the index was also replaced.

I will try my best to fix it as soon as possible.

spyzhov added a commit that referenced this issue Nov 25, 2024
spyzhov added a commit that referenced this issue Nov 25, 2024
spyzhov added a commit that referenced this issue Nov 25, 2024
* #81 fix index corruption
@spyzhov
Copy link
Owner

spyzhov commented Nov 25, 2024

Fixed in v0.9.6

@spyzhov spyzhov closed this as completed Nov 25, 2024
@spyzhov spyzhov added the bug Something isn't working label Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants