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
refactor: Remove C-style const-violating cast, Use reinterpret_cast #28127
Conversation
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update. |
Can |
What does 'UB' stand for? Something-Breakage? |
UB = Undefined behavior |
Concept ACK |
Yeah, you can use |
src/crypto/common.h
Outdated
|
||
#include <compat/endian.h> | ||
|
||
uint16_t static inline ReadLE16(const unsigned char* ptr) | ||
{ | ||
uint16_t x; | ||
memcpy((char*)&x, ptr, 2); | ||
memcpy(reinterpret_cast<char*>(&x), ptr, 2); |
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.
Why casting in the first place since memcpy
would reinterpret it as unsigned char *
anyways?
Concept ACK. |
fa752d8
to
fa474bc
Compare
UPD. Removed comment. nm |
No? |
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.
ACK fa474bc, I have reviewed the code and it looks OK.
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.
utACK fa474bc. This makes sense and looks good to me, though i'm far from a language lawyer.
nit: the first two commits share the same objective and title and could thus be squashed.
As a follow up, I'm pretty sure that all casts of |
Seems confusing and brittle to remove const and then add it back in the return type.
Also, wrap reinterpret_cast into a CharCast to ensure it is only called on byte pointers.
fa474bc
to
fa9108f
Compare
Thanks, addressed both comments by reviewers. |
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.
re-ACK fa9108f.
re-utACK fa9108f |
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.
ACK fa9108f
…se reinterpret_cast fa9108f refactor: Use reinterpret_cast where appropriate (MarcoFalke) 3333f95 refactor: Avoid casting away constness (MarcoFalke) fa6394d refactor: Remove unused C-style casts (MarcoFalke) Pull request description: Using a C-style cast to convert pointer types to a byte-like pointer type has many issues: * It may accidentally and silently throw away `const`. * It forces reviewers to check that it doesn't accidentally throw away `const`. For example, on current master a `const char*` is cast to `unsigned char*` (without `const`), see https://github.com/bitcoin/bitcoin/blob/d23fda05842ba4539b225bbab01b94df0060f697/src/span.h#L273 . This can lead to UB, and the only reason why it didn't lead to UB is because the return type added back the `const`. (Obviously this would break if the return type was deduced via `auto`) Fix all issues by adding back the `const` and using `reinterpret_cast` where appropriate. ACKs for top commit: darosior: re-utACK fa9108f hebasto: re-ACK fa9108f. john-moffett: ACK fa9108f Tree-SHA512: 87f6e4b574f9bd96d4e0f2a0631fd0a9dc6096e5d4f1b95042fe9f197afc2fe9a24e333aeb34fed11feefcdb184a238fe1ea5aff10d580bb18d76bfe48b76a10
Using a C-style cast to convert pointer types to a byte-like pointer type has many issues:
const
.const
.For example, on current master a
const char*
is cast tounsigned char*
(withoutconst
), seebitcoin/src/span.h
Line 273 in d23fda0
const
. (Obviously this would break if the return type was deduced viaauto
)Fix all issues by adding back the
const
and usingreinterpret_cast
where appropriate.