Fix Lightning invoice parsing and fetching
Three issues were causing invoices to not render or fetch: 1. bech32.c: Hardcoded MAX_PREFIX limited HRP length, but BOLT11 HRPs can be arbitrarily long depending on amount. Now derives max HRP length dynamically from input length (len-6 to match bolt11.c buffer). 2. content_parser.c: bolt11_decode_minimal was passed a pointer into the content buffer without null-termination. When a note contained multiple invoices, the decoder would read past the first invoice into newlines and the second invoice, causing checksum failure. Fixed by creating a null-terminated copy using strndup. 3. bolt11.c: bech32_decode_alloc allocated buffers using strlen(str)-6 and strlen(str)-8 without checking minimum length first. For inputs shorter than 8 chars, this caused size_t underflow leading to huge allocations and potential crash. Added early length guard. IMPORTANT: bech32_decode callers must allocate hrp buffer of at least strlen(input) - 6 bytes. This matches existing bolt11.c usage. Changelog-Fixed: Fixed Lightning invoice parsing and fetching for all amounts Closes: https://github.com/damus-io/damus/issues/3456 Closes: https://github.com/damus-io/damus/issues/3151 Signed-off-by: alltheseas <alltheseas@noreply.github.com> Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
committed by
Daniel D’Aquino
parent
c88d881801
commit
845089bed1
@@ -180,7 +180,15 @@ static int push_invoice_str(struct ndb_content_parser *p, struct ndb_str_block *
|
||||
struct bolt11 *bolt11;
|
||||
char *fail;
|
||||
|
||||
if (!(bolt11 = bolt11_decode_minimal(NULL, str->str, &fail)))
|
||||
// Create null-terminated copy since str->str points into content buffer
|
||||
char *invoice_str = strndup(str->str, str->len);
|
||||
if (!invoice_str)
|
||||
return 0;
|
||||
|
||||
bolt11 = bolt11_decode_minimal(NULL, invoice_str, &fail);
|
||||
free(invoice_str);
|
||||
|
||||
if (!bolt11)
|
||||
return 0;
|
||||
|
||||
start = p->buffer.p;
|
||||
|
||||
Reference in New Issue
Block a user