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

Marshal panic #441

Closed
piyongcai opened this issue Mar 16, 2023 · 1 comment · Fixed by #442
Closed

Marshal panic #441

piyongcai opened this issue Mar 16, 2023 · 1 comment · Fixed by #442

Comments

@piyongcai
Copy link

piyongcai commented Mar 16, 2023

Hi, every one!
The go-json is a great project but just unstable and painc in some cases.

This problem can be reproduced with the following code:

package main

import (
    "fmt"

    "github.com/goccy/go-json"
)

type UserTypeReq struct {
    UserType string `json:"user_type,omitempty"`
}

type Path struct {
    OrgID       *int `json:"org_id,omitempty"`  //
    UserTypeReq                                 // HERE IF CHAGE FIELD POSITION, MARSHAL IS WORKING OK.
    UserID      int  `json:"user_id,omitempty"` //
}

func main() {
    var route = Path{}

    var bytesJson, err = json.Marshal(route)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(string(bytesJson))
}

Panic information:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0xc0e19c]

goroutine 1 [running]:
github.com/goccy/go-json/internal/encoder/vm.Run(0xc000094340, {0xc0000f6000?, 0x0?, 0x0?}, 0x0?)
        D:/Work/GOPATH/pkg/mod/github.com/goccy/[email protected]/internal/encoder/vm/vm.go:26 +0x7c
github.com/goccy/go-json.encodeRunCode(0xc0000c9e38?, {0xc0000f6000?, 0xdc2160?, 0xc0000f8000?}, 0xc0000f6400?)
        D:/Work/GOPATH/pkg/mod/github.com/goccy/[email protected]/encode.go:310 +0x68
github.com/goccy/go-json.encode(0xc000094340, {0xc51840, 0xc0000a03e0})
        D:/Work/GOPATH/pkg/mod/github.com/goccy/[email protected]/encode.go:235 +0x21f
github.com/goccy/go-json.marshal({0xc51840, 0xc0000a03e0}, {0x0, 0x0, 0xc0000c9f50?})
        D:/Work/GOPATH/pkg/mod/github.com/goccy/[email protected]/encode.go:150 +0xb9
github.com/goccy/go-json.MarshalWithOption(...)
        D:/Work/GOPATH/pkg/mod/github.com/goccy/[email protected]/json.go:186     
github.com/goccy/go-json.Marshal({0xc51840?, 0xc0000a03e0?})
        D:/Work/GOPATH/pkg/mod/github.com/goccy/[email protected]/json.go:171 +0x2a
main.main()
        D:/Temp/go-json-err/main.go:22 +0x45
@piyongcai
Copy link
Author

piyongcai commented Mar 16, 2023

I got!!!

in file: /go-json/internal/encoder/vm/vm.go

// ...
// in some case:code.NextField  is nil
for {
    switch code.Op {  // HERE:  line 26,  when `code = code.NextField`,  -----> code == nil

     // ......

     if v == 0 {
        code = code.NextField   // HERE: line: 649 code.NextField == nil
     } else {
         // ......
     }
     // ......
}
//......

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant