go-json icon indicating copy to clipboard operation
go-json copied to clipboard

Panic: invalid memory address or nil pointer dereference

Open NateDogg-MM opened this issue 1 year ago • 3 comments

I created a gist for it gist but the take away is it crashes when both structs have pointers, so

type botRet struct {
	Id            *string  `json:"id"`
	Name      string   `json:"name"`
	
}

var ret struct {
	Bot *botRet `json:"bot"`
}

but if at least one doesn't have the pointer, it works

type botRet struct {
	Id            string `json:"id"`
	Name      string `json:"name"`
	
}

or

var ret struct {
	Bot botRet `json:"bot"`
}

Here's the output of the error

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x24 pc=0x4cb674]

goroutine 1 [running]:
github.com/goccy/go-json/internal/encoder.appendNormalizedHTMLString({0xc0000d8400?, 0x0?, 0x0?}, {0x24, 0x52afb4})
        /root/go/pkg/mod/github.com/goccy/[email protected]/internal/encoder/string.go:73 +0xe34
github.com/goccy/go-json/internal/encoder.AppendString(0x0?, {0xc0000d8400?, 0x0?, 0x0?}, {0x24?, 0x0?})
        /root/go/pkg/mod/github.com/goccy/[email protected]/internal/encoder/string.go:52 +0x45
github.com/goccy/go-json/internal/encoder/vm.Run(0xc0000a2680, {0xc0000d8400?, 0x0?, 0x400?}, 0xc0000ac150?)
        /root/go/pkg/mod/github.com/goccy/[email protected]/internal/encoder/vm/vm.go:4541 +0x17cc3
github.com/goccy/go-json.encodeRunCode(0xc0000a2680?, {0xc0000d8400?, 0xc0000d1db0?, 0xc00009c200?}, 0xc0000d8800?)
        /root/go/pkg/mod/github.com/goccy/[email protected]/encode.go:310 +0x56
github.com/goccy/go-json.encode(0xc0000a2680, {0x53b8a0, 0xc0000b0060})
        /root/go/pkg/mod/github.com/goccy/[email protected]/encode.go:235 +0x205
github.com/goccy/go-json.marshal({0x53b8a0, 0xc0000b0060}, {0x0, 0x0, 0xc0000ae000?})
        /root/go/pkg/mod/github.com/goccy/[email protected]/encode.go:150 +0xb9
github.com/goccy/go-json.MarshalWithOption(...)
        /root/go/pkg/mod/github.com/goccy/[email protected]/json.go:185
github.com/goccy/go-json.Marshal({0x53b8a0?, 0xc0000b0060?})
        /root/go/pkg/mod/github.com/goccy/[email protected]/json.go:170 +0x25
main.main()
        /var/www/go/json/json.go:29 +0x68
exit status 2

NateDogg-MM avatar Mar 18 '25 13:03 NateDogg-MM

@NateDogg-MM hi, have you solved this problem?

zxmantou avatar Jun 30 '25 12:06 zxmantou

@zxmantou sort of. i started using this package instead github.com/json-iterator/go

NateDogg-MM avatar Jun 30 '25 15:06 NateDogg-MM

@zxmantou sort of. i started using this package instead github.com/json-iterator/go

@NateDogg-MM got it, thanks for reply

zxmantou avatar Jul 01 '25 09:07 zxmantou