-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid nullptr dereference when constructing Blockchain
and tx_memory_pool
#8033
Conversation
Blockchain
and tx_memory_pool
Blockchain
and tx_memory_pool
struct BlockchainAndPool { | ||
cryptonote::Blockchain blockchain; | ||
cryptonote::tx_memory_pool tx_pool; | ||
|
||
BlockchainAndPool() : blockchain(tx_pool), tx_pool(blockchain) {} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems strange to initialize an object with an uninitialized other object. Would this pattern be more intuitive?
struct BlockchainAndPool {
cryptonote::Blockchain blockchain;
cryptonote::tx_memory_pool tx_pool;
BlockchainAndPool() : blockchain(), tx_pool(blockchain) {
blockchain.set_tx_pool(tx_pool);
}
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was following how cryptonode::core
does it here:
https://github.com/monero-project/monero/blob/528cd729687fcc73e2c082225af203d5f67b3eb6/src/cryptonote_core/cryptonote_core.cpp#L228
@moneromooo-monero has proposed this pattern on IRC/Matrix
Maybe a problem with using a setter would be that if the setter is not called the object would be in an invalid state.
Another reason is that Blockchain
and tx_memory_pool
hold a reference to each other and those references need to be initialized in the constructor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. The original design is dubious but I guess yours is the best solution without more intrusive changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please undo the unrelated indentation changes :)
Please squash. |
ff0a4bd
to
bfd78d9
Compare
done |
bfd78d9
to
3f0b5eb
Compare
This PR does not fix all cases of the problematic pattern.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for handling this. It was bothersome.
As @UkoeHB noted, in case the PR is supposed to be merged as it is now, let's not forget to address later:
|
Continuing in #8924 |
Until now
Blockchain
andtx_memory_pool
is constructed like the following:This contains a nullptr dereference.
This PR fixes this by introducing a new struct
BlockchainAndPool
which is used to construct
Blockchain
andtx_memory_pool
to avoid the nullptr dereferencation.