Skip to content

Commit

Permalink
net/rpc/jsonrpc: nil pointer deference on invalid reply.
Browse files Browse the repository at this point in the history
Fixes #5006.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/7691045
  • Loading branch information
Adrian Nos authored and rsc committed Mar 12, 2013
1 parent 276c04a commit 0d559f7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/pkg/net/rpc/jsonrpc/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/rpc"
"testing"
Expand Down Expand Up @@ -185,6 +186,22 @@ func TestMalformedInput(t *testing.T) {
ServeConn(srv) // must return, not loop
}

func TestMalformedOutput(t *testing.T) {
cli, srv := net.Pipe()
go srv.Write([]byte(`{"id":0,"result":null,"error":null}`))
go ioutil.ReadAll(srv)

client := NewClient(cli)
defer client.Close()

args := &Args{7, 8}
reply := new(Reply)
err := client.Call("Arith.Add", args, reply)
if err == nil {
t.Error("expected error")
}
}

func TestUnexpectedError(t *testing.T) {
cli, srv := myPipe()
go cli.PipeWriter.CloseWithError(errors.New("unexpected error!")) // reader will get this error
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/net/rpc/jsonrpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (c *clientCodec) ReadResponseHeader(r *rpc.Response) error {

r.Error = ""
r.Seq = c.resp.Id
if c.resp.Error != nil {
if c.resp.Error != nil || c.resp.Result == nil {
x, ok := c.resp.Error.(string)
if !ok {
return fmt.Errorf("invalid error %v", c.resp.Error)
Expand Down

0 comments on commit 0d559f7

Please sign in to comment.