This allows us to convert all the double negative calls such as !ContainsNoNUL()
-> ContainsNUL()
.
There are no usages which aren’t double negatives. This makes the code more understandable
Also, use str.find instead of manually looping
This allows us to convert all the double negative calls such as !ContainsNoNUL()
-> ContainsNUL()
.
There are no usages which aren’t double negatives. This makes the code more understandable
Also, use str.find instead of manually looping
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/31301.
See the guideline for information on the review process. A summary of reviews will appear here.
🚧 At least one of the CI tasks failed. Debug: https://github.com/bitcoin/bitcoin/runs/33094507644
Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example:
Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch.
A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test.
An intermittent issue.
Leave a comment here, if you need help tracking down a confusing failure.
214@@ -214,12 +215,9 @@ inline std::string MakeUnorderedList(const std::vector<std::string>& items)
215 /**
216 * Check if a string does not contain any embedded NUL (\0) characters
220 {
221- for (auto c : str) {
222- if (c == 0) return false;
223- }
224- return true;
225+ return std::ranges::any_of(str, [](char c) { return c == 0; });
We should be able to simplify further:
0 return str.find('\0') != std::string_view::npos;
214@@ -214,12 +215,9 @@ inline std::string MakeUnorderedList(const std::vector<std::string>& items)
215 /**
216 * Check if a string does not contain any embedded NUL (\0) characters
217 */
218-[[nodiscard]] inline bool ContainsNoNUL(std::string_view str) noexcept
219+[[nodiscard]] inline bool ContainsNUL(std::string_view str) noexcept
[[nodiscard]]
doesn’t make a lot of sense here, since the method doesn’t have any side-effect
Calling this without the return value would just be removed as dead code.
Yes, it almost certainly would; but with [[nodiscard]] it simply won’t compile. This is better as having some code such that calls ContainsNUL but doesn’t use it, must be a bug, as there is no purpose to call it, and not use it.
While refactor-only PRs are often frowned upon (since the benefit is small, but the risk is big), I think this is small enough for us to consider.
You could simplify further by changing the occurrences via https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#scripted-diffs (making sure all commit compile)
Note that the utxo_snapshot.cpp(45,1): error C1001: Internal compiler error
seems unrelated to this change.
(nit: typo in description)
220 {
221- for (auto c : str) {
222- if (c == 0) return false;
223- }
224- return true;
225+ return str.find('\0') != std::string_view::npos;;
str.contains('\0')
directly everywhere instead of ContainsNUL(str)
? If so, a simple comment to say so may be sufficient to avoid having to touch the same lines twice?
This allows us to convert all the double negative calls such as `!ContainsNoNUL()` -> `ContainsNUL()`. There are no usages which aren't double negatives.
Also, use std::any_of instead of manually looping
211@@ -212,14 +212,12 @@ inline std::string MakeUnorderedList(const std::vector<std::string>& items)
212 }
213
214 /**
215- * Check if a string does not contain any embedded NUL (\0) characters
216+ * Check if a string contains any embedded NUL (\0) characters
Based on #31301 (comment), to set your expectations, this likely won’t be merged (we can just postpone the fix until C++23, we’re not in a rush)
I would argue against this mentality :) I can’t tell for sure, but my guess is requiring c++23 is at least a year away from being merged? Makes more sense imo, to merge this in, and then once c++23 is available, update this code. (Also, I try to do PRs like this to move towards contributing upstream to bitcoin core more, and having PRs like this stall makes it hard to be motivated to try larger tasks)