Skip to content

psbt: fix nil pointer panic in TaprootLeafScript serialization - #2498

Closed
olegnazarov23 wants to merge 1 commit into
btcsuite:masterfrom
olegnazarov23:fix/psbt-nil-taproot-leaf-panic
Closed

olegnazarov23 wants to merge 1 commit into
btcsuite:masterfrom
olegnazarov23:fix/psbt-nil-taproot-leaf-panic

Conversation

@olegnazarov23

Copy link
Copy Markdown

Summary

Fix a nil pointer dereference panic when calling Packet.B64Encode() (or Serialize) on a PSBT that contains a nil entry in PInput.TaprootLeafScript.

Problem

If TaprootLeafScript contains a nil *TaprootTapLeafScript entry:

packet.Inputs[0].TaprootLeafScript = []*TaprootTapLeafScript{nil}

Calling B64Encode() panics during the sort.Slice call or when accessing leafScript.Script, instead of returning an error.

Fix

  • partial_input.go: Add nil check before sorting and serializing TaprootLeafScript entries. Returns ErrInvalidPsbtFormat if a nil entry is found.
  • utils.go: Add nil guard in FindLeafScript to skip nil entries instead of panicking.
  • psbt_test.go: Add regression test TestSerializeNilTaprootLeafScript.

Fixes #2495

When a PInput contains a nil entry in its TaprootLeafScript slice,
calling Serialize (via B64Encode) panics with a nil pointer dereference
during the sort or field access.

Add a nil check before sorting and serializing TaprootLeafScript
entries, returning ErrInvalidPsbtFormat instead of panicking. Also
add a nil guard in FindLeafScript to skip nil entries.

Fixes btcsuite#2495
Comment thread btcutil/psbt/psbt_test.go
packet, err := NewFromUnsignedTx(tx)
require.NoError(t, err)

packet.Inputs[0].TaprootLeafScript = []*TaprootTapLeafScript{nil}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would we reach this state without manually adding a nil entry to this slice of *TaprootTapLeafScript? Or else, why would we add a nil pointer into this slice?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't reachable through parsing; the deserializer only appends non-nil entries, so it takes a caller setting the exported field by hand. Either way #2539 (merged in July) covers this and more, so this PR can be closed.

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

Successfully merging this pull request may close these issues.

[bug]: pstb.Packet.B64Encode panics when given a nil TaprootScriptLeaf entry

2 participants