Skip to content

Bug Found in bitcoin/block.c #9002

@Schnema1

Description

@Schnema1

Hi,

During investigation of #8973 Claude discovered an error introduced with commit #40dd780 Link to commit

The switch from pull_bitcoin_tx to pull_bitcoin_tx_only removed an implicit null check, causing a segfault if pull_bitcoin_tx_only returns NULL:
/lightning/bitcoin/block.c

c
// line 212-213 - missing null check
b->tx[i] = pull_bitcoin_tx_only(b->tx, &p, &len);
b->tx[i]->chainparams = chainparams;  // segfaults if above returns NULL

Fix:

cb->tx[i] = pull_bitcoin_tx_only(b->tx, &p, &len);
if (!b->tx[i])
        return tal_free(b);
b->tx[i]->chainparams = chainparams;

This null check is consistent with the existing pattern already used elsewhere in the same file and in pull_bitcoin_tx itself. Without this fix, a corrupted block response from bitcoind causes an immediate segfault rather than a graceful error. This patch is recommended regardless of whether you have a corrupted block file, as it converts a segfault into a handled error condition.

Diagnostic Steps for Others
If you see this crash, check in this order:

  1. Verify bitcoind is healthy and fully synced:
    bashbitcoin-cli getblockchaininfo

  2. Find which block CLN is failing on from the logs:
    bashgrep -B 50 "FATAL SIGNAL" ~/.lightning/cln.log | grep "Adding block" | tail -5

  3. Test that specific block at different verbosity levels:

bashbitcoin-cli getblock <blockhash> 0  # should return hex
bitcoin-cli getblock <blockhash> 1  # will fail if corrupted
bitcoin-cli getblockstats <height>  # will fail if corrupted

Check bitcoind logs for corruption:
bashtail -100 ~/.bitcoin/debug.log | grep -E "ERROR|error|corrupt"
Environment

CLN version: v25.12.1
Bitcoin Core: v29 / v30.2
Downgrade to v25.09 not possible due to database migration introduced in v25.12

FYI @cdecker as you marked the pull request #40dd780 in Jan. 2024 as unverified

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions