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

panic unmarshaling map with omitempty present in nested embedded struct #402

Closed
aexvir opened this issue Sep 14, 2022 · 1 comment
Closed

Comments

@aexvir
Copy link

aexvir commented Sep 14, 2022

kinda similar to #400 but not really the same... at least I can't get to reproduce it with other pointers, just with map

  • it doesn't happen if I remove one embed; e.g. var ord Product
  • it doesn't happen if the omitempty is on a different type; e.g. Discounts *float64 json:"discounts,omitempty"`
  • it doesn't happen if I remove the omitempty
  • it happens regardless of the map value type; e.g. map[string]bool still panics

I tried more variations but right now I can't really remember all of them 😅
when using encoding/json it works just fine

code reproducing the issue

package main

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

type Order struct {
	Product
}

type Product struct {
	Pricing
}

type Pricing struct {
	Retail    float64            `json:"price"`
	Discounts map[string]float64 `json:"discounts,omitempty"`
}

const data = `{"price": 1.6}`

func main() {
	var ord Order

	err := json.Unmarshal([]byte(data), &ord)
	if err != nil {
		fmt.Printf("error unmarshaling data: %s", err.Error())
	}

	_, _ = json.Marshal(ord)
}

when running this the output is a panic on

var code *encoder.Opcode
if (ctx.Option.Flag & encoder.HTMLEscapeOption) != 0 {
code = codeSet.EscapeKeyCode
} else {
code = codeSet.NoescapeKeyCode
}
for {
switch code.Op {

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x1042067ec]

goroutine 1 [running]:
github.com/goccy/go-json/internal/encoder/vm.Run(0x14000118c30, {0x1400015e000?, 0x0?, 0x400?}, 0x1400014c0e0?)
	/Users/aexvir/go/pkg/mod/github.com/goccy/go-json@v0.9.11/internal/encoder/vm/vm.go:26 +0x6c
github.com/goccy/go-json.encodeRunCode(0x14000157df8?, {0x1400015e000?, 0x104388a78?, 0x14000157dc0?}, 0x140001220c0?)
	/Users/aexvir/go/pkg/mod/github.com/goccy/go-json@v0.9.11/encode.go:310 +0x64
github.com/goccy/go-json.encode(0x14000118c30, {0x10427e940, 0x14000110400})
	/Users/aexvir/go/pkg/mod/github.com/goccy/go-json@v0.9.11/encode.go:235 +0x23c
github.com/goccy/go-json.marshal({0x10427e940, 0x14000110400}, {0x0, 0x0, 0x14000110400?})
	/Users/aexvir/go/pkg/mod/github.com/goccy/go-json@v0.9.11/encode.go:150 +0xb8
github.com/goccy/go-json.MarshalWithOption(...)
	/Users/aexvir/go/pkg/mod/github.com/goccy/go-json@v0.9.11/json.go:186
github.com/goccy/go-json.Marshal({0x10427e940?, 0x14000110400?})
	/Users/aexvir/go/pkg/mod/github.com/goccy/go-json@v0.9.11/json.go:171 +0x30
main.main()
	/tmp/jsondebug/main.go:31 +0x108
exit status 2
@aexvir
Copy link
Author

aexvir commented Sep 14, 2022

oh, nevermind, I just found #391 🤦🏻 sorry for the duplicate

@aexvir aexvir closed this as completed Sep 14, 2022
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