solana-go icon indicating copy to clipboard operation
solana-go copied to clipboard

NPE in getAccountInfo() if result is nil

Open riptl opened this issue 2 years ago • 5 comments

If a non-compliant RPC server returns result: null for getAccountInfo(), the client will panic with null pointer deref (line 127 on out.Value because out is nil).

https://github.com/gagliardetto/solana-go/blob/dd0af958252d8cdd28db04a39fc67103223946cc/rpc/getAccountInfo.go#L123-L127

Ideally the RPC client would never panic under any circumstances

riptl avatar Mar 06 '22 05:03 riptl

How about

if out == nil || out.Value == nil { 

but in that case, can we be sure that out == nil means not found ?

gagliardetto avatar Mar 06 '22 14:03 gagliardetto

imo, out == nil violates the spec, so it's best to error here instead of assuming a valid result. Maybe we could wrap CallForInto to return an error if out == nil after unmarshaling.

riptl avatar Mar 08 '22 06:03 riptl

In some cases, result == nil follows the spec (e.g. getBlock: https://github.com/solana-labs/solana/blob/master/docs/src/developing/clients/jsonrpc-api.md#getblock )

gagliardetto avatar Mar 08 '22 15:03 gagliardetto

if out == nil {
   return nil, errors.New("expected a value, got null response")
}

gagliardetto avatar Mar 08 '22 15:03 gagliardetto

https://github.com/gagliardetto/solana-go/commit/e6a58061889f44feecf7e5e6305cc851d06fb3a5

gagliardetto avatar Mar 22 '22 14:03 gagliardetto