You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Set panics when the document is a pointer to a nil map #163
Pointer.Set returns an error for a nil map passed by value, but panics when the same map is passed by pointer. SetForToken also panics for the pointer form. This can occur with a zero-value map or one decoded from JSON null.
Steps to Reproduce
Run this program against master at ba302318e77c6929f102563a80883c9f565ffcb5:
package main
import (
"fmt""github.com/go-openapi/jsonpointer"
)
funcmain() {
p, err:=jsonpointer.New("/key")
iferr!=nil {
panic(err)
}
vardocumentmap[string]any_, err=p.Set(document, 1)
fmt.Println(err) // returns an error_, err=p.Set(&document, 1)
fmt.Println(err) // panics before reaching this line
}
Replacing the second call with jsonpointer.SetForToken(&document, "key", 1) reproduces the same panic.
Expected behavior
Return an error for the nil map, consistently with p.Set(document, 1), without panicking.
Actual behavior
cannot set field "key" on nil value: JSON pointer error
panic: assignment to entry in nil map
Passing a pointer to an initialized map[string]any{} succeeds and sets key to 1.
go env GOVERSION GOOS GOARCH: go1.27.4, darwin, arm64
Additional context
In setSingleImpl, isNil(node) checks the non-nil pointer before reflect.Indirect dereferences it. The map branch then calls SetMapIndex on the nil map. The nil-value handling added in #158 is already present in the tested commit; this case concerns a nil destination map behind a pointer.
Reported with OpenAI Codex on behalf of @FFspiky. The reproducer and the initialized-map control were run locally; no human code review is claimed.
Activity
Fix: Set should return error instead of panicking
fredbi commentedon Nov 17, 2026
@FFspiky thanks. Good catch indeed. Fixing this edge case right away.