-
Notifications
You must be signed in to change notification settings - Fork 38.7k
refactor: Drop unsafe AsBytePtr function #27978
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
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. |
|
Updated 3fca8c3 -> 2109a14 ( |
|
Needs rebase lgtm ACK 2109a14 🐭 Show signatureSignature: |
|
Rebased 2109a14 -> 650ca0d ( |
jonatack
left a comment
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 650ca0d
src/wallet/bdb.h
Outdated
| { | ||
| return {reinterpret_cast<const std::byte*>(dbt.get_data()), dbt.get_size()}; | ||
| } | ||
|
|
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.
Would it make sense for SpanFromDbt() to be a static method in src/wallet/bdb.cpp instead, which contains its only callers?
Replace calls to AsBytePtr with direct calls to AsBytes or reinterpret_cast. AsBytePtr is just a wrapper around reinterpret_cast. It accepts any type of pointer as an argument and uses reinterpret_cast to cast the argument to a std::byte pointer. Despite taking any type of pointer as an argument, it is not useful to call AsBytePtr on most types of pointers, because byte representations of most types will be implmentation-specific. Also, because it is named similarly to the AsBytes function, AsBytePtr looks safer than it actually is. Both AsBytes and AsBytePtr call reinterpret_cast internally and may be unsafe to use with certain types, but AsBytes at least has some type checking and can only be called on Span objects, while AsBytePtr can be called on any pointer argument. Co-authored-by: Pieter Wuille <[email protected]>
|
Updated 650ca0d -> 7c85361 ( |
|
re-ACK 7c85361 The OP and commit message might need to be updated a bit. |
|
ACK 7c85361 |
7c85361 refactor: Drop unsafe AsBytePtr function (Ryan Ofsky) Pull request description: Replace calls to `AsBytePtr` with calls to `AsBytes` or `reinterpret_cast`. `AsBytePtr` is just a wrapper around `reinterpret_cast`. It accepts any type of pointer as an argument and uses `reinterpret_cast` to cast the argument to a `std::byte` pointer. Despite taking any type of pointer as an argument, it is not useful to call `AsBytePtr` on most types of pointers, because byte representations of most types will be platform specific or undefined. Also, because it is named similarly to the `AsBytes` function, `AsBytePtr` looks safer than it actually is. Both `AsBytes` and `AsBytePtr` call reinterpret_cast internally and may be unsafe to use with certain types, but AsBytes at least has some type checking and can only be called on `Span` objects, while `AsBytePtr` can be called on any pointer argument. The change was motivated by discussion on bitcoin#27973 and bitcoin#27927 and is compatible with those PRs ACKs for top commit: jonatack: re-ACK 7c85361 sipa: utACK 7c85361 achow101: ACK 7c85361 Tree-SHA512: 200d858b1d4d579f081a7f9a14d488a99713b4918b4564ac3dd5c18578d927dbd6426e62e02f49f04a3fa73ca02ff7109c495cb0b92bec43c27d9b74e2f95757
Replace calls to
AsBytePtrwith calls toAsBytesorreinterpret_cast.AsBytePtris just a wrapper aroundreinterpret_cast. It accepts any type of pointer as an argument and usesreinterpret_castto cast the argument to astd::bytepointer.Despite taking any type of pointer as an argument, it is not useful to call
AsBytePtron most types of pointers, because byte representations of most types will be platform specific or undefined. Also, because it is named similarly to theAsBytesfunction,AsBytePtrlooks safer than it actually is. BothAsBytesandAsBytePtrcall reinterpret_cast internally and may be unsafe to use with certain types, but AsBytes at least has some type checking and can only be called onSpanobjects, whileAsBytePtrcan be called on any pointer argument.The change was motivated by discussion on #27973 and #27927 and is compatible with those PRs