fix(psbt): filter nil entries from taproot fields before sort/serialize#2511
fix(psbt): filter nil entries from taproot fields before sort/serialize#2511ThomsenDrake wants to merge 2 commits intobtcsuite:masterfrom
Conversation
…ze (fixes btcsuite#2495) B64Encode panics with nil pointer dereference when TaprootLeafScript, TaprootScriptSpendSig, or TaprootBip32Derivation slices contain nil entries. Filter nil elements before sorting and serialization to prevent the panic.
|
Thanks for fixing this crash issue! The defensive filtering approach makes sense and matches PSBT's pattern of silently handling edge cases. One suggestion for cleaner code: The filtering logic is repeated 3 times with just type differences. Consider extracting to a helper function: func filterNil[T any](slice []*T) []*T { Then simplify to: |
|
Thanks for the review, @TechLateef! Great suggestion on extracting a helper function. I'll refactor the repeated filtering logic into a generic helper. I also noticed the same nil-filtering pattern likely applies to the |
Addresses review feedback from @TechLateef (btcsuite#2511). The repeated nil-filtering logic (3 blocks) is now consolidated into a single generic helper function filterNil[T any], reducing duplication and improving readability. No behavioral change — the filtering logic is identical.
|
Pushed the refactoring as discussed. Extracted a I also audited the deserialize path — nil entries can only originate from external code that manually constructs |
Fixes #2495
Problem
panics with a nil pointer dereference when , , or slices contain nil entries. The sort comparison functions and serialization loops dereference slice elements without nil checks.
Fix
Filter nil elements from all three taproot-specific slice fields before sorting and serialization. This is a defensive approach: nil entries are silently removed rather than rejected with an error, matching the behavior of silently ignoring zero-value fields that PSBT already uses elsewhere.
Changes
btcutil/psbt/partial_input.go: Added nil-filtering blocks before sort/serialize forTaprootScriptSpendSig,TaprootLeafScript, andTaprootBip32Derivation.Testing
All existing
btcutil/psbttests pass (go test ./btcutil/psbt/...).