Repository navigation
psbt: fix nil pointer panic in TaprootLeafScript serialization - #2498
Closed
olegnazarov23 wants to merge 1 commit into
Closed
olegnazarov23 wants to merge 1 commit into
olegnazarov23 wants to merge 1 commit into
Conversation
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
allocz
reviewed
Oct 13, 2026
| packet, err := NewFromUnsignedTx(tx) | ||
| require.NoError(t, err) | ||
|
|
||
| packet.Inputs[0].TaprootLeafScript = []*TaprootTapLeafScript{nil} |
Contributor
There was a problem hiding this comment.
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?
Author
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix a nil pointer dereference panic when calling
Packet.B64Encode()(orSerialize) on a PSBT that contains a nil entry inPInput.TaprootLeafScript.Problem
If
TaprootLeafScriptcontains a nil*TaprootTapLeafScriptentry:Calling
B64Encode()panics during thesort.Slicecall or when accessingleafScript.Script, instead of returning an error.Fix
partial_input.go: Add nil check before sorting and serializingTaprootLeafScriptentries. ReturnsErrInvalidPsbtFormatif a nil entry is found.utils.go: Add nil guard inFindLeafScriptto skip nil entries instead of panicking.psbt_test.go: Add regression testTestSerializeNilTaprootLeafScript.Fixes #2495