Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/34416. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. LLM Linter (✨ experimental)Possible typos and grammar issues:
2026-03-25 05:21:17 |
| bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex* pindex) | ||
| { | ||
| AssertLockHeld(cs_main); | ||
| if (!pindex) return false; |
There was a problem hiding this comment.
i think this goes in the wrong direction. All call sites have checked against nullptr, and there should never be a reason to call this with nullptr, so this seems it should be a reference. Just like BlockRequested and FetchBlock take a reference.
There was a problem hiding this comment.
Following this feedback, I will reconsider the switch to pass-by-reference. As mentioned in the description, I started in that direction, but was concerned about consistency.
For consistency, it makes sense to also change the other methods (e.g. Next()), increasing the scope. Const input pointer parameters are relatively easy to change to pass-by-reference; but return values where nullptr is a valid option should be changed to std::option<&...>. These end up in a larger changeset (arguably not adding much value).
There was a problem hiding this comment.
Well, i haven't looked into the lower level chain util methods. For some methods, it could make sense to accept a pointer. My comment was mostly about BlockRequestAllowed, and other similar high level functions that only work on blocks that exist and whose presence has been previously confirmed
There was a problem hiding this comment.
bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex*) should be changed to bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex&)
There was a problem hiding this comment.
i don't understand why the some line of code should be changed, only to revert it in the next commit. Please submit 6539829321aa13b4afb8d898a62eb3be382607c0 as a separate pull request
There was a problem hiding this comment.
also, the name pindex should probably be changed to block_index (or so), like in the function immediately below
There was a problem hiding this comment.
i don't understand why the some line of code should be changed, only to revert it in the next commit. Please submit 6539829 as a separate pull request
I can do it in a separate PR, should be trivial.
The add/remove is a side-effect of independent commits: one commit changed the method called from here (Contains), so a null check had to be added before the call. Then when this method has also been change to take a reference input, the check is no longer needed. (The check moved outwards.)
There was a problem hiding this comment.
Note: BlockRequestAllowed change has been done in #34464.
|
I'm exploring the option to use references, but I've decided to keep this simple PR as first step. As suggested, I look into using references instead of pointers. Howerver, that is a bigger refactor. I think the minimal change (one extra check) and unit tests from this PR make sense in isolation, as the bigger change will be heavier. Draft PR #34440 opened. I will strip this one a bit more. |
7f27d10 to
0c2df01
Compare
|
Marked ready for review. |
| bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex* pindex) | ||
| { | ||
| AssertLockHeld(cs_main); | ||
| if (!pindex) return false; |
There was a problem hiding this comment.
bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex*) should be changed to bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex&)
|
test ACK 0c2df01 |
1f8f7d4 Change BlockRequestAllowed() to take ref (optout) Pull request description: As [suggested here](#34416 (comment)), a minor refactor of `PeerManagerImpl::BlockRequestAllowed()` to take reference parameter (instead of pointer). The motivation is to make the code safer, by minimizing the risk of null-dereference, and to be more consistent. The change is local to the `PeerManagerImpl::BlockRequestAllowed()` class. Related to #34440. ACKs for top commit: maflcko: review ACK 1f8f7d4 🎐 l0rinc: tested ACK 1f8f7d4 stickies-v: ACK 1f8f7d4 Tree-SHA512: 9c2de2d3e7d067e018db7ec54c79f512ccc1da54574d4fb362f6697ee6e235783779d7094cf20856cd34e08a1dbc74609d8351fe7b2287cd8ec0c836ef07be19
0c2df01 to
6c3a91f
Compare
|
Rebased, following merging of #34464 . |
|
ACK 6c3a91f9cefe28a9c7a59963a51c2d64d8c005a2 By the way, the CI test failed not because of the PR code. |
| BOOST_CHECK_EQUAL(chain_2.Next(&genesis), &bi1); | ||
| BOOST_CHECK_EQUAL(chain_2.Next(&bi1), nullptr); | ||
| } | ||
| } |
There was a problem hiding this comment.
This test seems overly verbose to me, and I don't think it is meaningfully documenting anything either. We have a test that exercises the block index below, maybe extend that instead?
There was a problem hiding this comment.
The existing chain_test() is a rather specific use case, and it doesn't use CChain at all, therefore I would not combine with that.
What I prefer to do is to remove some tests, and rearrange the calls by called function, and keep only one for each different case. I see some value even in basic tests, such as "what happens if Next() is called with nullptr?" or "What happens if indexer is called with a non-existing index?".
But if you prefer, I can remove some more, keep only the nullptr-input ones, or even none.
| @@ -410,7 +410,9 @@ class CChain | |||
| /** Efficiently check whether a block is present in this chain. */ | |||
| bool Contains(const CBlockIndex* pindex) const | |||
| { | |||
| return (*this)[pindex->nHeight] == pindex; | |||
| if (pindex) | |||
| return (*this)[pindex->nHeight] == pindex; | |||
There was a problem hiding this comment.
This should either be on the same line as the condition or wrapped in braces per the style guide.
There was a problem hiding this comment.
Done. (I was following the style of the surrounding code, but better to follow the style guide.)
Add basic unit tests to the `CChain` class, filling a gap.
The `CChain::Contains()` method dereferences its input without checking, potentially resulting in nullptr-dereference if invoked with `nullptr`. The `Next()` method can also trigger this. Avoid the memory access violation by adding an extra check for the `pindex` input. The result is `false` for `nullptr` input.
6c3a91f to
c8949c4
Compare
|
Minor rework following review comments:
|
| /** Efficiently check whether a block is present in this chain. */ | ||
| bool Contains(const CBlockIndex* pindex) const | ||
| { | ||
| if (!pindex) return false; |
There was a problem hiding this comment.
I do not agree with adding this presently redundant guard. Existing code of master never calls Contains() with nullptr, otherwise it would have crashed.
Much more in favor of #34440.
|
|
||
| } // namespace | ||
|
|
||
| BOOST_AUTO_TEST_CASE(cchain_basic_tests) |
There was a problem hiding this comment.
If you keep this PR over the other one, please consider the suggestions from #34440 (comment)
|
Update: #34440 is a more complete improvement, and there has been some opinions in favor of that. However, I still keep this PR open, as a more limited-scope change (easier to review). |
…tests 7c75244 Change pindexMostWork parameter of ActivateBestChainStep() to reference (optout) c5eb283 Change CChain::FindFork() to take ref (optout) 20b58e2 Change CChain::Next() to take reference (optout) fe2d6e2 Change CChain::Contains() to take reference (optout) db56bcd test: Add CChain::FindFork() tests (optout) 8333abd test: Add CChain basic tests (optout) Pull request description: Refactor `CChain` methods (`Contains()`, `Next()`, `FindFork()`) to use references instead of pointers, to minimize the risk of accidental `nullptr` dereference (memory access violation). Also add missing unit tests to the `CChain` class. The `CChain::Contains()` method (in `src/chain.h`) dereferences its input without checking. The `Next()` method also calls into this with a `nullptr` if invoked with `nullptr`. While most call sites have indirect guarantee that the input is not `nullptr`, it's not easy to establish this to all call sites with high confidence. These methods are publicly available. There is no known high-level use case to trigger this error, but the fix is easy, and makes the code safer. Changes: - Add basic unit tests for `CChain` class methods - Add unit tests for `CChain::FindFork()` - Change `CChain::Contains()` to take reference - Change `CChain::Next()` to take reference - Change `CChain::FindFork()` to take reference - Change `pindexMostWork` parameter of `ActivateBestChainStep()` to reference - Rename changed parameters (`* pindex` --> `& index`) Alternative. A simpler change is to stick with pointers, with extra checks where needed, see #34416 . This change is remotely related to and indirectly triggered by #32875 . Further ideas, not considered in this PR: - Change `InvalidateBlock()` and `PreciousBlock()` to take references. - Change `CChain` internals to store references instead of pointers - Change CChain to always have at least one element (genesis), that way there is always genesis and tip. - Check related methods to return reference (guaranteed non-null) -- `FindFork`, `FindEarliestAtLeast`, `FindForkInGlobalIndex`, `blockman.AddToBlockIndex`, etc. ACKs for top commit: l0rinc: reACK 7c75244 maflcko: re-review ACK 7c75244 🌅 achow101: ACK 7c75244 hodlinator: re-ACK 7c75244 Tree-SHA512: 122f40120058f7e1f0273b3afed9c54966c05f06b6f2fee45bc48430617f24a5e4320a9bb7bb0ac986f2accfa22fabae5cc941b949758ddca2e9fcd472b46c33
|
This has became unnecessary after #34440 landed -- tests are included there, |
|
🐙 This pull request conflicts with the target branch and needs rebase. |
… (minor refactor) e3a3963 Change BlockRequestAllowed() to take ref (optout) Pull request description: As [suggested here](bitcoin/bitcoin#34416 (comment)), a minor refactor of `PeerManagerImpl::BlockRequestAllowed()` to take reference parameter (instead of pointer). The motivation is to make the code safer, by minimizing the risk of null-dereference, and to be more consistent. The change is local to the `PeerManagerImpl::BlockRequestAllowed()` class. Related to #34440. ACKs for top commit: maflcko: review ACK e3a3963 🎐 l0rinc: tested ACK e3a3963 stickies-v: ACK e3a3963 Tree-SHA512: 9c2de2d3e7d067e018db7ec54c79f512ccc1da54574d4fb362f6697ee6e235783779d7094cf20856cd34e08a1dbc74609d8351fe7b2287cd8ec0c836ef07be19
… (minor refactor) 2e0df69 Change BlockRequestAllowed() to take ref (optout) Pull request description: As [suggested here](bitcoin/bitcoin#34416 (comment)), a minor refactor of `PeerManagerImpl::BlockRequestAllowed()` to take reference parameter (instead of pointer). The motivation is to make the code safer, by minimizing the risk of null-dereference, and to be more consistent. The change is local to the `PeerManagerImpl::BlockRequestAllowed()` class. Related to #34440. ACKs for top commit: maflcko: review ACK 2e0df69 🎐 l0rinc: tested ACK 2e0df69 stickies-v: ACK 2e0df69 Tree-SHA512: 9c2de2d3e7d067e018db7ec54c79f512ccc1da54574d4fb362f6697ee6e235783779d7094cf20856cd34e08a1dbc74609d8351fe7b2287cd8ec0c836ef07be19
… (minor refactor) 1f8f7d477ae0d33bd96f7936889c17bd40805fb9 Change BlockRequestAllowed() to take ref (optout) Pull request description: As [suggested here](bitcoin/bitcoin#34416 (comment)), a minor refactor of `PeerManagerImpl::BlockRequestAllowed()` to take reference parameter (instead of pointer). The motivation is to make the code safer, by minimizing the risk of null-dereference, and to be more consistent. The change is local to the `PeerManagerImpl::BlockRequestAllowed()` class. Related to #34440. ACKs for top commit: maflcko: review ACK 1f8f7d477ae0d33bd96f7936889c17bd40805fb9 🎐 l0rinc: tested ACK 1f8f7d477ae0d33bd96f7936889c17bd40805fb9 stickies-v: ACK 1f8f7d477ae0d33bd96f7936889c17bd40805fb9 Tree-SHA512: 9c2de2d3e7d067e018db7ec54c79f512ccc1da54574d4fb362f6697ee6e235783779d7094cf20856cd34e08a1dbc74609d8351fe7b2287cd8ec0c836ef07be19
Add unit tests to the
CChainclass, and add a nullptr-check toCChain::Contains()to avoid potential memory access violation bynullptrdereference.The
CChain::Contains()method (insrc/chain.h) dereferences its input without checking. TheNext()method also calls into this with anullptrif invoked withnullptr. While most call sites have indirect guarantee that the input is notnullptr, it's not easy to establish this to all call sites with high confidence. These methods are publicly available. There is no known high-level use case to trigger this error, but the fix is easy, and makes the code safer.Also, unit tests were added to the
CChainclass, filling a gap.Changes:
CChainclass methodsCChain::Contains()Alternative. I have considered changing
Contains()to take a reference instead of pointer, however, when applying that to various methods, it grew to a much bigger changeset -- see #34440. Having in mind the friction of larger PRs, I think the present minimal one-check solution (with the tests) makes sense on its own.This change is remotely related to and indirectly triggered by #32875 .