Commit 0d559f7b authored by Adrian Nos's avatar Adrian Nos Committed by Russ Cox

net/rpc/jsonrpc: nil pointer deference on invalid reply.

Fixes #5006.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/7691045
parent 276c04a9
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net" "net"
"net/rpc" "net/rpc"
"testing" "testing"
...@@ -185,6 +186,22 @@ func TestMalformedInput(t *testing.T) { ...@@ -185,6 +186,22 @@ func TestMalformedInput(t *testing.T) {
ServeConn(srv) // must return, not loop 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) { func TestUnexpectedError(t *testing.T) {
cli, srv := myPipe() cli, srv := myPipe()
go cli.PipeWriter.CloseWithError(errors.New("unexpected error!")) // reader will get this error go cli.PipeWriter.CloseWithError(errors.New("unexpected error!")) // reader will get this error
......
...@@ -83,7 +83,7 @@ func (c *clientCodec) ReadResponseHeader(r *rpc.Response) error { ...@@ -83,7 +83,7 @@ func (c *clientCodec) ReadResponseHeader(r *rpc.Response) error {
r.Error = "" r.Error = ""
r.Seq = c.resp.Id 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) x, ok := c.resp.Error.(string)
if !ok { if !ok {
return fmt.Errorf("invalid error %v", c.resp.Error) return fmt.Errorf("invalid error %v", c.resp.Error)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment