Panic on Go 1.27: nil pointer dereference when passing a type to json.Marshal
The following program sample.go triggers an unexpected result
package main
import (
"encoding/json"
"fmt"
)
type MyJsonMarshaler struct{ n int }
func (m MyJsonMarshaler) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`{"num": %d}`, m.n)), nil
}
func main() {
ch := make(chan json.Marshaler, 1)
ch <- MyJsonMarshaler{2}
m, err := json.Marshal(<-ch)
fmt.Println(string(m), err)
}This is the existing test file _test/issue-1010.go.
Expected result
$ go run ./sample.go
{"num":2} <nil>Got
$ yaegi ./sample.go
run: runtime error: invalid memory address or nil pointer dereference
goroutine 1 [running]:
...
github.com/traefik/yaegi/interp.genInterfaceWrapper(...)
.../interp/run.go:1082 +0x144
github.com/traefik/yaegi/interp.genDestValue(...)
.../interp/value.go:164 +0x11aYaegi Version
fcb76d1e (master)
Reproduce via the test suite
Built with Go 1.27.1:
$ go test -run 'TestFile/issue-1010' ./interp/
--- FAIL: TestFile (0.01s)
--- FAIL: TestFile/issue-1010.go (0.00s)
interp_file_test.go:50: runtime error: invalid memory address or nil pointer dereference
FAILThe same test passes when built with Go 1.26:
$ /usr/lib/go-1.26/bin/go test -run 'TestFile/issue-1010' ./interp/
ok github.com/traefik/yaegi/interp 0.015sTestInterpConsistencyBuild/issue-1010.go fails the same way.
Additional Notes
Go 1.27 enables encoding/json/v2, so json.Marshaler's package path is now
encoding/json/v2 rather than encoding/json. The wrapper lookup in
getWrapper (interp/use.go) searches the interpreter's binary package table
by that path and returns nil when the entry is absent; genInterfaceWrapper
(interp/run.go:1082) then dereferences it without checking.
This is causing a FTBFS for the Debian package golang-github-traefik-yaegi
(https://bugs.debian.org/1146215) ahead of the golang-defaults transition to
Go 1.27, so an upstream fix would be appreciated.
Source: traefik/yaegi