Skip to content

Commit c7a27e4

Browse files
committed
bitcoin: avoid adding to NULL
Detected by UBSan: $ UBSAN_OPTIONS=print_stacktrace=1 ./wallet/test/run-psbt_fixup bitcoin/psbt.c:733:2: runtime error: applying zero offset to null pointer #0 0x53c829 in psbt_from_bytes lightning/bitcoin/psbt.c:733:2 #1 0x5adcb0 in main lightning/wallet/test/run-psbt_fixup.c:174:10 SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior bitcoin/psbt.c:733:2
1 parent 6d76642 commit c7a27e4

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

bitcoin/psbt.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -656,12 +656,14 @@ struct wally_tx *psbt_final_tx(const tal_t *ctx, const struct wally_psbt *psbt)
656656
return NULL;
657657

658658
tal_wally_start();
659-
if (wally_psbt_extract(psbt, /* flags */ 0, &wtx) == WALLY_OK)
659+
if (wally_psbt_extract(psbt, /* flags */ 0, &wtx) == WALLY_OK) {
660660
tal_add_destructor(wtx, wally_tx_destroy);
661-
else
661+
tal_wally_end_onto(ctx, wtx, struct wally_tx);
662+
} else {
662663
wtx = NULL;
664+
tal_wally_end(ctx);
665+
}
663666

664-
tal_wally_end_onto(ctx, wtx, struct wally_tx);
665667
return wtx;
666668
}
667669

@@ -673,11 +675,13 @@ struct wally_psbt *psbt_from_b64(const tal_t *ctx,
673675
char *str = tal_strndup(tmpctx, b64, b64len);
674676

675677
tal_wally_start();
676-
if (wally_psbt_from_base64(str, /* flags */ 0, &psbt) == WALLY_OK)
678+
if (wally_psbt_from_base64(str, /* flags */ 0, &psbt) == WALLY_OK) {
677679
tal_add_destructor(psbt, psbt_destroy);
678-
else
680+
tal_wally_end_onto(ctx, psbt, struct wally_psbt);
681+
} else {
679682
psbt = NULL;
680-
tal_wally_end_onto(ctx, psbt, struct wally_psbt);
683+
tal_wally_end(ctx);
684+
}
681685

682686
return psbt;
683687
}
@@ -726,11 +730,13 @@ struct wally_psbt *psbt_from_bytes(const tal_t *ctx, const u8 *bytes,
726730
struct wally_psbt *psbt;
727731

728732
tal_wally_start();
729-
if (wally_psbt_from_bytes(bytes, byte_len, /* flags */ 0, &psbt) == WALLY_OK)
733+
if (wally_psbt_from_bytes(bytes, byte_len, /* flags */ 0, &psbt) == WALLY_OK) {
730734
tal_add_destructor(psbt, psbt_destroy);
731-
else
735+
tal_wally_end_onto(ctx, psbt, struct wally_psbt);
736+
} else {
732737
psbt = NULL;
733-
tal_wally_end_onto(ctx, psbt, struct wally_psbt);
738+
tal_wally_end(ctx);
739+
}
734740

735741
return psbt;
736742
}

0 commit comments

Comments
 (0)