From 5642664b6159b0fe3940a1276f23998dcfce5481 Mon Sep 17 00:00:00 2001 From: niftynei Date: Fri, 29 May 2020 16:41:51 -0500 Subject: [PATCH] psbt: fix broken script finder in wally_finalize_psbt As written, this was looking at the output script on the global_tx for an input on the same, which caused valgrind problems because A) there's no guarantee that an output at that input index exists and also B) that's not even the right script. What you *want* to be doing is using the non_witness_utxo to look up the script iff it exists. If there's no out_script available we totally bail on finalizing this input at all. --- src/psbt.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/psbt.c b/src/psbt.c index bc9448758..d89937e5b 100644 --- a/src/psbt.c +++ b/src/psbt.c @@ -2697,7 +2697,7 @@ int wally_finalize_psbt(struct wally_psbt *psbt) for (i = 0; i < psbt->num_inputs; ++i) { struct wally_psbt_input *input = &psbt->inputs[i]; struct wally_tx_input *txin = &psbt->tx->inputs[i]; - unsigned char *out_script; /* Script that determines how we should finalize this input, typically output script */ + unsigned char *out_script = NULL; /* Script that determines how we should finalize this input, typically output script */ size_t out_script_len, type; bool witness = false, p2sh = false;; @@ -2710,9 +2710,10 @@ int wally_finalize_psbt(struct wally_psbt *psbt) out_script = input->redeem_script; out_script_len = input->redeem_script_len; p2sh = true; - } else { - out_script = psbt->tx->outputs[txin->index].script; - out_script_len = psbt->tx->outputs[txin->index].script_len; + } else if (input->non_witness_utxo && input->non_witness_utxo->num_outputs > txin->index) { + struct wally_tx_output out = input->non_witness_utxo->outputs[txin->index]; + out_script = out.script; + out_script_len = out.script_len; } if (input->witness_script) { out_script = input->witness_script; @@ -2720,6 +2721,11 @@ int wally_finalize_psbt(struct wally_psbt *psbt) witness = true; } + /* We need an outscript to do anything */ + if (!out_script) { + continue; + } + if ((ret = wally_scriptpubkey_get_type(out_script, out_script_len, &type)) != WALLY_OK) { return ret; }