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

nil Pointer Dereference in GetBinary Method Causes Panic #264

Open
sina-haseli opened this issue Dec 7, 2024 · 0 comments
Open

nil Pointer Dereference in GetBinary Method Causes Panic #264

sina-haseli opened this issue Dec 7, 2024 · 0 comments

Comments

@sina-haseli
Copy link

sina-haseli commented Dec 7, 2024

I encountered a runtime panic caused by a nil pointer dereference in the GetBinary method of the DataBytesOrJSON struct. The issue occurs when calling the GetTransaction() method on a TransactionWithMeta instance.

Steps to Reproduce

  1. Use TransactionWithMeta.GetTransaction() in a context where the underlying asDecodedBinary field in DataBytesOrJSON is nil.
  2. The GetBinary() method attempts to access asDecodedBinary.Content without checking if asDecodedBinary is nil.
  3. This leads to a runtime panic:

Relevant Code

The panic occurs in this part of the GetBinary method:

func (dt *DataBytesOrJSON) GetBinary() []byte {
 return dt.asDecodedBinary.Content
}

Add a nil check for asDecodedBinary in the GetBinary method to handle uninitialized or invalid states gracefully:

func (dt *DataBytesOrJSON) GetBinary() []byte {
    if dt == nil {
        return nil // or log an error to indicate invalid state
    }
    return dt.asDecodedBinary.Content
}

Environment

solana-go version: v1.12.0
Go version: (go1.21)
Operating system: (Ubuntu 20.04)

Additional Context

Here’s an example of how this issue surfaces in code using solana-go:

func processBalanceChanges(
    tx rpc.TransactionWithMeta,
    uniqueAddresses map[string]struct{},
) {
    transaction, err := tx.GetTransaction()
    if err != nil {
        log.Error("Error getting transaction: ", "err", err)
        return
    }

    // Panic occurs if tx.Transaction.GetBinary() dereferences a nil pointer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant