interfaces: fix findAncestorByHash false positive #36473

Closed
Romansage6 wants to merge 1 commit from 2026-10-fix-findancestorbyhash into master AGit
Romansage6 commented 2026-10-08 13:04:55 +00:00 (Migrated from github.com)

findAncestorByHash looks up both block_hash and ancestor_hash, then was
only supposed to clear the ancestor pointer when the lookups succeeded
but the ancestor relationship didn't hold:

if (block && ancestor && block->GetAncestor(ancestor->nHeight) != ancestor) ancestor = nullptr;

Because of the && short circuit, an unknown block_hash makes the
whole condition false without ever evaluating the relationship check,
so a successful ancestor_hash lookup is passed through unmodified
and the function reports true for a descendant that doesn't exist.

Before:

chain->findAncestorByHash(uint256{}, active[0]->GetBlockHash()) // returns true

After this change it correctly returns false when either hash is
unknown, in addition to the existing relationship check.

Tested with:

cmake --build build --target test_bitcoin -j8
./build/bin/test_bitcoin --run_test=interfaces_tests

Full suite (./build/bin/test_bitcoin, 877 cases) also passes with no
regressions. Added two new cases to interfaces_tests/findAncestorByHash
covering unknown block_hash and unknown ancestor_hash, which fail
on master and pass with this fix.

Fixes #36472

findAncestorByHash looks up both block_hash and ancestor_hash, then was only supposed to clear the ancestor pointer when the lookups succeeded but the ancestor relationship didn't hold: ```cpp if (block && ancestor && block->GetAncestor(ancestor->nHeight) != ancestor) ancestor = nullptr; ``` Because of the `&&` short circuit, an unknown `block_hash` makes the whole condition false without ever evaluating the relationship check, so a successful `ancestor_hash` lookup is passed through unmodified and the function reports `true` for a descendant that doesn't exist. Before: ```cpp chain->findAncestorByHash(uint256{}, active[0]->GetBlockHash()) // returns true ``` After this change it correctly returns `false` when either hash is unknown, in addition to the existing relationship check. Tested with: ``` cmake --build build --target test_bitcoin -j8 ./build/bin/test_bitcoin --run_test=interfaces_tests ``` Full suite (`./build/bin/test_bitcoin`, 877 cases) also passes with no regressions. Added two new cases to `interfaces_tests/findAncestorByHash` covering unknown `block_hash` and unknown `ancestor_hash`, which fail on master and pass with this fix. Fixes #36472
DrahtBot commented 2026-10-08 13:05:05 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.
A summary of reviews will appear here.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36473 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36473.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36473#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. A summary of reviews will appear here. <!--5faf32d7da4f0f540f40219e4f7537a3-->
sedited commented 2026-10-08 13:10:06 +00:00 (Migrated from github.com)

This seems to be a drive-by LLM contribution from an issue that was created not even an hour ago. Closing again.

This seems to be a drive-by LLM contribution from an issue that was created not even an hour ago. Closing again.
First-time contributor

Base: 282a05b8d10e317d2ce4b42b96ee8b13345804ea
Head: ccea7c93f5d8a54516a4bfe74d36486eac6c9e11

I found no actionable issues in this static review.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> Base: `282a05b8d10e317d2ce4b42b96ee8b13345804ea` Head: `ccea7c93f5d8a54516a4bfe74d36486eac6c9e11` I found no actionable issues in this static review. [Full review report](<https://review.fish.foo/traces/36473-ccea7c93f5d8a54516a4bfe74d36486eac6c9e11-585-1.html>)
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36473
No description provided.