Skip to content

Commit 6898198

Browse files
committed
NewScriptPubkey: fix panic on empty scriptPubKey
If scriptPubKey is empty, we can't get &scriptPubKey[0] from it.
1 parent 680aa68 commit 6898198

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

kernel/script_pubkey.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ func newScriptPubkey(ptr *C.btck_ScriptPubkey, fromOwned bool) *ScriptPubkey {
3535
// Parameters:
3636
// - rawScriptPubkey: Serialized script pubkey data
3737
func NewScriptPubkey(rawScriptPubkey []byte) *ScriptPubkey {
38-
ptr := C.btck_script_pubkey_create(unsafe.Pointer(&rawScriptPubkey[0]), C.size_t(len(rawScriptPubkey)))
38+
var buf unsafe.Pointer
39+
if len(rawScriptPubkey) > 0 {
40+
buf = unsafe.Pointer(&rawScriptPubkey[0])
41+
}
42+
ptr := C.btck_script_pubkey_create(buf, C.size_t(len(rawScriptPubkey)))
3943
return newScriptPubkey(check(ptr), true)
4044
}
4145

kernel/script_pubkey_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,16 @@ func TestInvalidScripts(t *testing.T) {
173173
inputIndex: 0,
174174
description: "a random segwit transaction from the blockchain using native segwit - WITH WRONG SEGWIT",
175175
},
176+
{
177+
name: "empty_scriptpubkey",
178+
scriptPubkeyHex: "",
179+
amount: 0,
180+
// Minimal coinbase-style transaction with a single empty scriptSig and zero-value output;
181+
// used to trigger verification paths.
182+
txToHex: "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff00ffffffff0100000000000000000000000000",
183+
inputIndex: 0,
184+
description: "empty scriptPubkey should fail verification",
185+
},
176186
}
177187

178188
for _, tt := range tests {

0 commit comments

Comments
 (0)