From d5a67ec87a29ce59bb3c0f5130a322793aa390f8 Mon Sep 17 00:00:00 2001 From: ZmnSCPxj Date: Thu, 26 Apr 2018 05:21:45 +0000 Subject: [PATCH] chaintopology: Protect against underflow when computing first_blocknum. Fixes: #1423 (Hopefully) Reported-by: @NicolasDorier --- lightningd/chaintopology.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index 532c34cc6..41af56038 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -525,10 +525,20 @@ static void get_init_blockhash(struct bitcoind *bitcoind, u32 blockcount, if (blockcount < topo->first_blocknum) { if (bitcoind->ld->config.rescan < 0) { /* Absolute blockheight, but bitcoind's blockheight isn't there yet */ - topo->first_blocknum = blockcount - 1; + /* Protect against underflow in subtraction. + * Possible in regtest mode. */ + if (blockcount < 1) + topo->first_blocknum = 0; + else + topo->first_blocknum = blockcount - 1; } else if (topo->first_blocknum == UINT32_MAX) { /* Relative rescan, but we didn't know the blockheight */ - topo->first_blocknum = blockcount - bitcoind->ld->config.rescan; + /* Protect against underflow in subtraction. + * Possible in regtest mode. */ + if (blockcount < bitcoind->ld->config.rescan) + topo->first_blocknum = 0; + else + topo->first_blocknum = blockcount - bitcoind->ld->config.rescan; } }