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

rpc: fix double reference in CallContext unmarshal #26701

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,10 @@ func (c *Client) CallContext(ctx context.Context, result interface{}, method str
case len(resp.Result) == 0:
return ErrNoResult
default:
return json.Unmarshal(resp.Result, &result)
if result == nil {
return nil
}
return json.Unmarshal(resp.Result, result)
}
}

Expand Down
34 changes: 34 additions & 0 deletions rpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,40 @@ import (
"github.com/ethereum/go-ethereum/log"
)

type nullTestService struct{}

func (s *nullTestService) ReturnNull() json.RawMessage {
// An example where null results are returned is calling eth_getTransactionReceipt on a non-existent
// transaction. The result is null, but the call is not an error.
return json.RawMessage("null")
}

func TestNullResponse(t *testing.T) {
server := newTestServer()
defer server.Stop()
err := server.RegisterName("test", new(nullTestService))
if err != nil {
t.Fatal(err)
}

client := DialInProc(server)
defer client.Close()
result := &jsonrpcMessage{}

err = client.Call(&result.Result, "test_returnNull")
if err != nil {
t.Fatal(err)
}

if result.Result == nil {
t.Fatal("Expected non-nil result")
}

if !reflect.DeepEqual(result.Result, json.RawMessage("null")) {
t.Errorf("Expected null, got %s", result.Result)
}
}

func TestClientRequest(t *testing.T) {
server := newTestServer()
defer server.Stop()
Expand Down