# HG changeset patch # User Adrian Nos # Date 1362748556 -3600 # Node ID e6c6714075723f7a12d42f9eba6a2a45bd03d13d # Parent 451d31faf61c391125faa29e7311ee769003c3b2 Fix null pointer deference on partially invalid input diff -r 451d31faf61c -r e6c671407572 src/pkg/net/rpc/jsonrpc/all_test.go --- a/src/pkg/net/rpc/jsonrpc/all_test.go Thu Mar 07 21:23:59 2013 -0800 +++ b/src/pkg/net/rpc/jsonrpc/all_test.go Fri Mar 08 14:15:56 2013 +0100 @@ -9,6 +9,7 @@ "errors" "fmt" "io" + "io/ioutil" "net" "net/rpc" "testing" @@ -185,6 +186,22 @@ 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 diff -r 451d31faf61c -r e6c671407572 src/pkg/net/rpc/jsonrpc/client.go --- a/src/pkg/net/rpc/jsonrpc/client.go Thu Mar 07 21:23:59 2013 -0800 +++ b/src/pkg/net/rpc/jsonrpc/client.go Fri Mar 08 14:15:56 2013 +0100 @@ -83,7 +83,7 @@ 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)