Skip to content

Commit aba798d

Browse files
author
Chandra Pratap
committed
common/bolt12: fix tlv_span() behaviour with empty tlvstream
Changelog-Fixed: When `tlv_span()` receives an empty `tlvsteam`, it sets the value of `startp` to `start` - `tlvstream` = `NULL` - `tlvstream` where `tlvstream` is a pointer. Similarly, `end` can also become `NULL` when `fromwire_bigsize()` reads to the end of `cursor` and then sets `cursor` = `NULL` resulting in the same error. Since this is undefined behaviour, add a fix for it by correcting the initial value of `end` and guarding against `cursor` = `NULL`.
1 parent 2b8b709 commit aba798d

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

common/bolt12.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,16 +613,26 @@ size_t tlv_span(const u8 *tlvstream, u64 minfield, u64 maxfield,
613613
size_t tlvlen = tal_bytelen(tlvstream);
614614
const u8 *start, *end;
615615

616-
start = end = NULL;
616+
start = NULL;
617+
end = tlvstream;
617618
while (tlvlen) {
618619
const u8 *before = cursor;
619620
bigsize_t type = fromwire_bigsize(&cursor, &tlvlen);
621+
if (!cursor)
622+
break;
623+
620624
bigsize_t len = fromwire_bigsize(&cursor, &tlvlen);
625+
if (!cursor)
626+
break;
627+
621628
if (type >= minfield && start == NULL)
622629
start = before;
623630
if (type > maxfield)
624631
break;
625632
fromwire_pad(&cursor, &tlvlen, len);
633+
if (!cursor)
634+
break;
635+
626636
end = cursor;
627637
}
628638
if (!start)

0 commit comments

Comments
 (0)