Skip to content

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

Description

@chris-ricketts

Background

If for some reason you have:

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

Calling packet.B64Encode will panic with a nil pointer dereference instead of returning an error.

--- FAIL: TestB64Encode_PanicsWithNilTaprootLeafScriptEntry (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x18 pc=0x102ddca48]

goroutine 47 [running]:
testing.tRunner.func1.2({0x102f8e980, 0x10318f460})
        /nix/store/jp3z5jp9gaxsw7fdzbqn29aabmrxq62j-go-1.24.1/share/go/src/testing/testing.go:1734 +0x1ac
testing.tRunner.func1()
        /nix/store/jp3z5jp9gaxsw7fdzbqn29aabmrxq62j-go-1.24.1/share/go/src/testing/testing.go:1737 +0x334
panic({0x102f8e980?, 0x10318f460?})
        /nix/store/jp3z5jp9gaxsw7fdzbqn29aabmrxq62j-go-1.24.1/share/go/src/runtime/panic.go:792 +0x124
github.com/btcsuite/btcd/btcutil/psbt.(*PInput).serialize(0x14000098b08, {0x102fd9a80, 0x1400063eb40})
        /Users/chris/code/bitcoin/btcd/btcutil/psbt/partial_input.go:524 +0xb58
github.com/btcsuite/btcd/btcutil/psbt.(*Packet).Serialize(0x140000e5ea0, {0x102fd9a80, 0x1400063eb40})
        /Users/chris/code/bitcoin/btcd/btcutil/psbt/psbt.go:389 +0x44c
github.com/btcsuite/btcd/btcutil/psbt.(*Packet).B64Encode(0x14000098ea0)
        /Users/chris/code/bitcoin/btcd/btcutil/psbt/psbt.go:417 +0x40
github.com/btcsuite/btcd/btcutil/psbt.TestB64Encode_PanicsWithNilTaprootLeafScriptEntry(0x14000102540)
        /Users/chris/code/bitcoin/btcd/btcutil/psbt/psbt_test.go:1709 +0x274
testing.tRunner(0x14000102540, 0x102fd7148)
        /nix/store/jp3z5jp9gaxsw7fdzbqn29aabmrxq62j-go-1.24.1/share/go/src/testing/testing.go:1792 +0xe4
created by testing.(*T).Run in goroutine 1
        /nix/store/jp3z5jp9gaxsw7fdzbqn29aabmrxq62j-go-1.24.1/share/go/src/testing/testing.go:1851 +0x374
FAIL    github.com/btcsuite/btcd/btcutil/psbt   0.365s
FAIL

Your environment

btcd @ 1c55c7c18179f9638f5a481151692516c9db45d6

Steps to reproduce

func TestB64Encode_PanicsWithNilTaprootLeafScriptEntry(t *testing.T) {
	tx := wire.NewMsgTx(2)
	tx.AddTxIn(&wire.TxIn{
		PreviousOutPoint: wire.OutPoint{}, // minimal dummy prevout
	})
	tx.AddTxOut(&wire.TxOut{
		Value:    0,
		PkScript: []byte{txscript.OP_TRUE},
	})

	p, err := NewFromUnsignedTx(tx)
	require.NoError(t, err)

	// This nil element triggers a panic inside psbt serialization/B64Encode.
	p.Inputs[0].TaprootLeafScript = []*TaprootTapLeafScript{nil}
	_, err = p.B64Encode()
	require.Error(t, err)
}

Expected behaviour

An 'invalid pstb taproot leaf' kind of error or maybe ErrInvalidPsbtFormat to be returned from B64Encode.

Actual behaviour

SIGSEGV: segmentation violation

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions