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

[Bug] - Nil pointer exception #290

Closed
sujit-baniya opened this issue Sep 20, 2021 · 6 comments · Fixed by #291
Closed

[Bug] - Nil pointer exception #290

sujit-baniya opened this issue Sep 20, 2021 · 6 comments · Fixed by #291

Comments

@sujit-baniya
Copy link

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

goroutine 187 [running]:
github.com/goccy/go-json/internal/encoder/vm.Run(0xc002f276c0, {0xc001d27c00, 0xc001070180, 0x0}, 0x0)
        /Users/sujit/go/pkg/mod/github.com/goccy/go-json@v0.7.8/internal/encoder/vm/vm.go:197 +0x2f5a3
github.com/goccy/go-json.encodeRunCode(0x4de31c0, {0xc001d27c00, 0x6806d90, 0xc0009bf580}, 0xc00106e000)
        /Users/sujit/go/pkg/mod/github.com/goccy/go-json@v0.7.8/encode.go:307 +0x68
github.com/goccy/go-json.encode(0xc002f276c0, {0x4de31c0, 0xc0011cb7d0})
        /Users/sujit/go/pkg/mod/github.com/goccy/go-json@v0.7.8/encode.go:232 +0x21c
github.com/goccy/go-json.marshal({0x4de31c0, 0xc0011cb7d0}, {0x0, 0x0, 0x40169aa})
        /Users/sujit/go/pkg/mod/github.com/goccy/go-json@v0.7.8/encode.go:147 +0xba
github.com/goccy/go-json.MarshalWithOption(...)
        /Users/sujit/go/pkg/mod/github.com/goccy/go-json@v0.7.8/json.go:186
github.com/goccy/go-json.Marshal({0x4de31c0, 0xc0011cb7d0})
        /Users/sujit/go/pkg/mod/github.com/goccy/go-json@v0.7.8/json.go:171 +0x2a
@goccy
Copy link
Owner

goccy commented Sep 21, 2021

I cannot investigate from this information. Could you share me the reproducible code ?

@sujit-baniya
Copy link
Author

sujit-baniya commented Sep 21, 2021

The error started to occur after latest upgrade of this library.
The application is huge... don't know where the error is coming from. Thus I can't create reproducible code. Replacing with standard json package, it worked

@zJeremiah
Copy link

zJeremiah commented Sep 21, 2021

Here is an example of the nil pointer error.

package main

import (
	"fmt"
	"net/http"
	"time"

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

func main() {
	c := http.DefaultClient
	req, _ := http.NewRequest("GET", "http://localhost:9999/bad/request", nil)
	_, err := c.Do(req)

	s, err := json.Marshal(struct {
		Time string      `json:"time"`
		Data interface{} `json:"data"`
	}{
		Time: time.Now().Format(time.RFC3339),
		Data: err,
	})
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(s)
}
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x1261783]

goroutine 1 [running]:
github.com/goccy/go-json/internal/encoder/vm.Run(0xc000244000, {0xc000242000, 0x0, 0x0}, 0x0)
	/Users/jeremiah.zink/go/pkg/mod/github.com/goccy/go-json@v0.7.8/internal/encoder/vm/vm.go:197 +0x2f5a3
github.com/goccy/go-json.encodeRunCode(0x12b4a60, {0xc000242000, 0x15660e8, 0xc000228040}, 0xc000242400)
	/Users/jeremiah.zink/go/pkg/mod/github.com/goccy/go-json@v0.7.8/encode.go:307 +0x68
github.com/goccy/go-json.encode(0xc000244000, {0x12b4a60, 0xc000220040})
	/Users/jeremiah.zink/go/pkg/mod/github.com/goccy/go-json@v0.7.8/encode.go:232 +0x21c
github.com/goccy/go-json.marshal({0x12b4a60, 0xc000220040}, {0x0, 0x0, 0xc00021ff50})
	/Users/jeremiah.zink/go/pkg/mod/github.com/goccy/go-json@v0.7.8/encode.go:147 +0xba
github.com/goccy/go-json.MarshalWithOption(...)
	/Users/jeremiah.zink/go/pkg/mod/github.com/goccy/go-json@v0.7.8/json.go:186
github.com/goccy/go-json.Marshal({0x12b4a60, 0xc000220040})
	/Users/jeremiah.zink/go/pkg/mod/github.com/goccy/go-json@v0.7.8/json.go:171 +0x2a
main.main()

@orisano
Copy link
Contributor

orisano commented Sep 21, 2021

This is the reproducible test code.

func TestIssue290(t *testing.T) {
	type Issue290 interface {
		A()
	}
	var a struct {
		A Issue290
	}
	if b, err := stdjson.Marshal(a); err == nil {
		t.Log(string(b))
                 // {"A":null}
	}
	if b, err := json.Marshal(a); err == nil { // panicked
		t.Log(string(b))
	}
}

Maybe, the cause is untyped nil.

@sujit-baniya
Copy link
Author

Any expected update @goccy

@zimmski
Copy link

zimmski commented Sep 25, 2021

We just ran into this. Here is my reproducer which looks similar to what @orisano reported. What i find interesting is that an empty interface declaration does not trigger the problem: You need an interface with a method.

package main

import (
	"encoding/json"
	"fmt"

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

func main() {
	type Empty interface{}
	type NonEmpty interface {
		A()
	}

	type SomeStruct struct {
		Empty    Empty
		NonEmpty NonEmpty // Comment this line and you will receive no panic.
	}

	originalEncoded, originalError := json.Marshal(SomeStruct{}) // Works
	fmt.Printf("Original: %s\n-> %s\n", originalEncoded, originalError)

	goccyEncoded, goccyError := goccy.Marshal(SomeStruct{}) // Panics
	fmt.Printf("Original: %s\n-> %s\n", goccyEncoded, goccyError)
}
Original: {"Empty":null,"NonEmpty":null}
-> %!s(<nil>)
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x5104e3]

goroutine 1 [running]:
github.com/goccy/go-json/internal/encoder/vm.Run(0xc000102dd0, {0xc000028400, 0x0, 0x400}, 0xc0000320c0)
        src/github.com/goccy/go-json/internal/encoder/vm/vm.go:197 +0x2f5a3
github.com/goccy/go-json.encodeRunCode(0x524c20, {0xc000028400, 0x65fda8, 0xc000032080}, 0xc000028800)
        src/github.com/goccy/go-json/encode.go:307 +0x68
github.com/goccy/go-json.encode(0xc000102dd0, {0x524c20, 0xc00006a060})
        src/github.com/goccy/go-json/encode.go:232 +0x21c
github.com/goccy/go-json.marshal({0x524c20, 0xc00006a060}, {0x0, 0x0, 0xc00005df50})
        src/github.com/goccy/go-json/encode.go:147 +0xba
github.com/goccy/go-json.MarshalWithOption(...)
        src/github.com/goccy/go-json/json.go:186
github.com/goccy/go-json.Marshal({0x524c20, 0xc00006a060})
        src/github.com/goccy/go-json/json.go:171 +0x2a
main.main()
        tt.go:24 +0x10a
exit status 2

@goccy hope that helps!

orisano added a commit to orisano/go-json that referenced this issue Sep 27, 2021
goccy added a commit that referenced this issue Sep 27, 2021
Fix encoding of nil value about interface type that has method
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

Successfully merging a pull request may close this issue.

5 participants