]> Cypherpunks repositories - gostls13.git/commitdiff
net/rpc/jsonrpc: nil pointer deference on invalid reply.
authorAdrian Nos <nos.adrian@gmail.com>
Tue, 12 Mar 2013 15:45:56 +0000 (11:45 -0400)
committerRuss Cox <rsc@golang.org>
Tue, 12 Mar 2013 15:45:56 +0000 (11:45 -0400)
Fixes #5006.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/7691045

src/pkg/net/rpc/jsonrpc/all_test.go
src/pkg/net/rpc/jsonrpc/client.go

index 3c7c4d48fa66f6860dfee2e30a78596bdf7b7c28..40d4b82d7f2b4e4a38af1d0ba99364a7b4739161 100644 (file)
@@ -9,6 +9,7 @@ import (
        "errors"
        "fmt"
        "io"
+       "io/ioutil"
        "net"
        "net/rpc"
        "testing"
@@ -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
index 3fa8cbf08a37bfb53308d6deaa6fd10f7e8a88d6..2194f212574908a1f747b0591447d887f557b8f5 100644 (file)
@@ -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)