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

nil pointer deference in proto.Size #631

Closed
jpbetz opened this issue Jun 8, 2018 · 2 comments
Closed

nil pointer deference in proto.Size #631

jpbetz opened this issue Jun 8, 2018 · 2 comments

Comments

@jpbetz
Copy link

jpbetz commented Jun 8, 2018

When adding a proto.Size() call to code in etcd, we encountered this error:

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

github.com/coreos/etcd/etcdserver/etcdserverpb.(*PutResponse).Size(0x0, 0x4cac5b)
        /usr/local/google/home/jpbetz/Projects/etcd/src/github.com/coreos/etcd/etcdserver/etcdserverpb/rpc.pb.go:8148 +0x26
github.com/coreos/etcd/etcdserver/etcdserverpb.(*PutResponse).Marshal(0x0, 0x176af60, 0x0, 0x7fbfb4a53af0, 0x0, 0x1)
        /usr/local/google/home/jpbetz/Projects/etcd/src/github.com/coreos/etcd/etcdserver/etcdserverpb/rpc.pb.go:5161 +0x2f
github.com/coreos/etcd/vendor/github.com/golang/protobuf/proto.Size(0x176af60, 0x0, 0xc420117880)
        /usr/local/google/home/jpbetz/Projects/etcd/src/github.com/coreos/etcd/vendor/github.com/golang/protobuf/proto/table_marshal.go:2602 +0x100
github.com/coreos/etcd/etcdserver.warnOfExpensiveRequest(0xc428750060, 0xbebecd857a50c3e6, 0xdde501b3a, 0x17c4620, 0x17604e0, 0xc4203a88c0, 0x176af60, 0x0, 0x175f920, 0xc42014db10)

Here the nil pointer dereference appears to occur here:

https://github.com/coreos/etcd/blob/c6e18c712c7cc336a86404478297afa6063a72eb/etcdserver/etcdserverpb/rpc.pb.go#L8148

The nil was propagated here:

https://github.com/coreos/etcd/blob/c6e18c712c7cc336a86404478297afa6063a72eb/etcdserver/etcdserverpb/rpc.pb.go#L5161

And appears to have originated here:

b, _ := m.Marshal()

Suggesting that a nil check needs to be added to f m, ok := pb.(Marshaler); ok { ?

Versions:

github.com/golang/protobuf v1.1.0
github.com/gogo/protobuf v1.0.0

@gyuho
Copy link

gyuho commented Jun 8, 2018

To reproduce:

package main

import (
	"fmt"

	"github.com/coreos/etcd/etcdserver/etcdserverpb"
	"github.com/golang/protobuf/proto"
)

func main() {
	var msg *etcdserverpb.PutResponse
	m := proto.Message(msg)
	a, b := m.(proto.Marshaler)
	fmt.Println(a, b) // <nil> true

	// trigger panic
	a.Marshal()
	/*
	   panic: runtime error: invalid memory address or nil pointer dereference
	   [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7e2826]

	   goroutine 1 [running]:
	   github.com/coreos/etcd/etcdserver/etcdserverpb.(*PutResponse).Size(0x0, 0x9cbe00)
	   	/home/gyuho/go/src/github.com/coreos/etcd/etcdserver/etcdserverpb/rpc.pb.go:8148 +0x26
	   github.com/coreos/etcd/etcdserver/etcdserverpb.(*PutResponse).Marshal(0x0, 0x2, 0x2, 0xb, 0x0, 0x0)
	   	/home/gyuho/go/src/github.com/coreos/etcd/etcdserver/etcdserverpb/rpc.pb.go:5161 +0x2f
	   main.main()
	   	/home/gyuho/p.go:15 +0xce
	   exit status 2
	*/
}

@jpbetz
Copy link
Author

jpbetz commented Jun 9, 2018

Turns out that the input to proto.Size was a non-nil interface with a nil value pointer. By checking for both msg != nil && !reflect.ValueOf(msg).IsNil() before calling proto.Size we were able to resolve the issue.

@jpbetz jpbetz closed this as completed Jun 9, 2018
@golang golang locked and limited conversation to collaborators Jun 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants