Skip to content

Make sure all callers of LookupBlockIndex(...) check for nullptr before dereferencing (CBlockIndex*)#13969

Closed
practicalswift wants to merge 1 commit intobitcoin:masterfrom
practicalswift:LookupBlockIndex
Closed

Make sure all callers of LookupBlockIndex(...) check for nullptr before dereferencing (CBlockIndex*)#13969
practicalswift wants to merge 1 commit intobitcoin:masterfrom
practicalswift:LookupBlockIndex

Conversation

@practicalswift
Copy link
Copy Markdown
Contributor

Make sure all callers of LookupBlockIndex(...) check for nullptr before dereferencing (CBlockIndex*).

@DrahtBot
Copy link
Copy Markdown
Contributor

DrahtBot commented Aug 14, 2018

Reviewers, 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.

Copy link
Copy Markdown
Contributor

@domob1812 domob1812 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nits, but otherwise utACK 5983a8d9eda7a3e53c4d9ec715f802d434ba7cf8.

Comment thread src/rpc/blockchain.cpp Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: You could make this const CBlockIndex*.

Comment thread src/validation.cpp Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you are here, you could make this const.

Comment thread src/net_processing.cpp Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a matter of taste, but I think that the more explicit pBestIndex != nullptr is more readable as to the intent. I'm not sure if there's a style rule for how to test pointers against null which goes against that, though.

Comment thread src/wallet/rpcwallet.cpp Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can also be const.

@practicalswift practicalswift force-pushed the LookupBlockIndex branch 2 times, most recently from 076ab14 to 1c02054 Compare August 15, 2018 12:48
@practicalswift
Copy link
Copy Markdown
Contributor Author

@domob1812 Thanks for reviewing. Feedback addressed. Please re-review :-)

Copy link
Copy Markdown
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a matter of taste, but I think that the more explicit pBestIndex != nullptr is more readable as to the intent

Not sure but I think the most frequent is assert(pointer) so I would go that way. If you feel otherwise then submit a PR to update developer notes first?

Comment thread src/rpc/blockchain.cpp Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this even possible? If so then it's new behaviour, should have a test, otherwise should be an assertion.

Comment thread src/net_processing.cpp Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR. Above you see that if vHeaders.push is called then pBestIndex = pindex was called. IMO this should be removed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't mean that the assertion should be removed? How do you mean "unrelated to this PR" in this context? :-)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not an assertion of a LookupBlockIndex result, that's done in L3400.

Comment thread src/rpc/blockchain.cpp Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this even possible? If not, should be an assertion.

Comment thread src/wallet/rpcwallet.cpp Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be an assertion? How can it be nullptr since confirms > 0?

@practicalswift practicalswift force-pushed the LookupBlockIndex branch 3 times, most recently from a392e32 to 8dd6609 Compare August 16, 2018 10:24
@practicalswift
Copy link
Copy Markdown
Contributor Author

@promag Thanks for reviewing! Feedback addressed. Please re-review :-)

Copy link
Copy Markdown
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR only changes how the process terminates in case there is some bug. An early assertion makes sense when there are side effects until the dereferencing, which I believe is not the case here.

Also, see #13969 (review) above.

Comment thread src/wallet/rpcwallet.cpp Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this assertion?

Copy link
Copy Markdown
Contributor Author

@practicalswift practicalswift Aug 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@promag stopBlock is dereferenced a few lines down:

response.pushKV("stop_height", stopBlock->nHeight);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And that's fine, if by any chance stopBlock is null then the process will terminate. In this simple code the assertion is pointless IMO.

@practicalswift
Copy link
Copy Markdown
Contributor Author

practicalswift commented Aug 16, 2018

@promag I'm not sure I follow. The assumption underlying your reasoning seems to be that the only difference between …

assert(foo);
foo->bar();

… and …

// assert(foo);
foo->bar();

... at runtime would be the line number at which the process terminates (given foo == nullptr). That seems like a very risky assumption to make? :-)

@promag
Copy link
Copy Markdown
Contributor

promag commented Aug 16, 2018

@practicalswift I said

which I believe is not the case here.

@practicalswift
Copy link
Copy Markdown
Contributor Author

@promag Ah, I think I misunderstood what you wrote. You're simply suggesting that the assertions should be made right before dereferencing and not earlier (assuming no side effects before dereferencing)?

I misunderstood and thought that you meant that the only potential problem caused by null pointer dereferencing would be process termination :-)

@promag
Copy link
Copy Markdown
Contributor

promag commented Aug 16, 2018

You're simply suggesting that the assertions should be made right before dereferencing and not earlier

I mean that an assertion makes sense to prevent side effects before dereferencing.

I also think that these particular assertions are not that relevant or useful.

IMHO of course, and I'd wait for more reviews.

Copy link
Copy Markdown
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it would be a good idea to make this change without adding a style guideline about when it is actually recommended to assert that a pointer is not null before dereferencing.

Personally, I don't like these asserts because I think they are distracting and make the code verbose and awkward to read, but I guess the benefit of adding them would be to have error messages that make it easier to determine the causes of crashes when core dumps aren't available. Another benefit might be that if there was a guideline requiring asserts before pointer dereferences, it might encourage more uses of c++ references instead of pointers, which I think would be good.

Will add my code utACK 75f360c if other people want this change, but personally would like it better if the change was dropped or accompanied by a developer guideline.

@promag
Copy link
Copy Markdown
Contributor

promag commented Sep 13, 2018

it might encourage more uses of c++ references instead of pointers

@ryanofsky +1

@practicalswift practicalswift deleted the LookupBlockIndex branch April 10, 2021 19:35
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants