Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/36030. ReviewsSee the guideline and AI policy for information on the review process. ConflictsReviewers, 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. LLM Linter (✨ experimental)Possible places where named args for integral literals may be used (e.g.
2026-08-20 05:17:33 |
`KeyPair` derives and serializes its x-only public key before computing a Taproot tweak. Both libsecp calls ran inside `assert()`, so disabling assertions left `pubkey_bytes` uninitialized. Reading those bytes when computing the tweak is undefined behavior. Supported builds reject `NDEBUG`, so released binaries are unaffected. Both calls now run before the tweak computation, allowing the existing cleanup path to handle either failure.
0743075 to
76ca754
Compare
| - key: bugprone-assert-side-effect.CheckFunctionCalls | ||
| value: true | ||
| - key: bugprone-assert-side-effect.IgnoredFunctions | ||
| value: '^MoneyRange$;^base_blob<.*>::size$;^std::basic_string<.*>::data$;^std::map<.*>::end$;^std::numeric_limits<.*>::(min|max)$;^std::this_thread::get_id$;^std::unordered_map<.*>::end$;^std::unordered_set<.*>::end$;^std::vector<.*>::(back|data)$' |
There was a problem hiding this comment.
Not sure about excluding stuff.
Either assert is overall fine to use, and this pull can just be closed.
Or, it is not, and then it should just be disallowed completely. Maybe with a simple git grep assert linter (like lint_rpc_assert)?
Anything in-between just seems inconsistent and pointless.
There was a problem hiding this comment.
Either assert is overall fine to use [...] Or, it is not
I'd argue it's more complicated because of possible side effects of the calls.
Pure calls should be safe to eliminate, so it doesn't matter if it's assert or Assert.
We can migrate the remaining assert calls all in a follow-up to get rid of this confusion.
But here I rather wanted to focus on the non-const ones (but it seems that's still too broad or not broad enough), so I've refocused on side-effectful calls here that we can never eliminate: remove the assert in KeyPair::KeyPair, and migrate assert to Assert for calls like ConnectBlock.
There was a problem hiding this comment.
Pure calls should be safe to eliminate, so it doesn't matter if it's
assertorAssert.
I don't think this is true. What about "pure" calls or calls without side-effects to assert that are there to prevent UB in the next line?
There was a problem hiding this comment.
I agree, I don't think that contradict what I said.
I'm fixing the worst offenders here, I'm all for fixing the rest in a follow-up PR (though we likely have to extract the assertion helpers to avoid circular dependencies)
There was a problem hiding this comment.
I think the build dep issue is already fixed in #28690, no?
There was a problem hiding this comment.
I haven't dug deeply into #28690 yet, but I see that it moves util/check.cpp into bitcoin_util_kernel, so kernel code should be fine to migrate to Assert() afterward.
A complete migration still seems to require either exempting the lowest-level libraries or splitting the assertion failure helper further.
Regardless, this PR isn't focused on that migration, it only makes sure required function calls are always evaluated.
There was a problem hiding this comment.
They already are evaluated, and compilation fails if they are not. This is not a fix, but a style cleanup.
fixing the worst offenders
None of them are real issues, so it seems odd to ask reviewers to classify them into severity. I remain, that this style cleanup should either fix all or none (#36030 (comment))
There was a problem hiding this comment.
assert has many pitfalls (some are documented in https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2829.htm#remedy), I assume it's why we introduced Assert. But currently we can't just switch to Assert everywhere because of the mentioned circular dependencies - but looking forward to when we will be able to do that.
As mentioned above and in the PR description, this isn't a style change, it's the smallest change that I found to make sure the assertions cannot remove critical code (e.g. if NDEBUG is enabled or the code forked and changed, etc).
There was a problem hiding this comment.
The paper you link to is about a small style cleanup of the assert macro to not require extra () around the condition in some case. I don't think there is any pitfall in that paper relevant to the code here.
(e.g. if
NDEBUGis enabled
This won't compile
or the code forked and changed, etc).
If someone removes check, it is on them, and they could equally remove any other check and there is nothing that can be done about that.
Also, if such a use-case was supported and the user cared about checks being retained, then UB checks should equally be preserved, because UB can also lead to critical code being removed (by the compiler or at runtime).
Also, since you are modifying benchmarks: If the assert was compiled out, the benchmark would be wrong, etc.
I am not against this change, and it is probably fine to do (even if the only goal is to avoid confusion), but either all places are fixed (and enforced), or none of them. (I've done it for all test code in #36074)
Cherry-picking single cases one-by-one and asking for review for every single one in a separate pull doesn't seem scalable.
`assert()` does not evaluate its expression when `NDEBUG` is defined. Keep only calls that write output used afterward, mutate state, perform I/O, or release resources; these operations are part of setup or the target under test rather than the invariant alone. Use `Assert()` so their evaluation does not depend on the standard assertion macro.
76ca754 to
246ca6c
Compare
Problem:
KeyPair::KeyPair()performed two requiredlibsecp256k1calls insideassert().Disabling assertions skips those calls, so the Taproot tweak is computed from uninitialized data.
Supported builds reject
NDEBUG, so this affects only unsupported configurations, but it motivated a broader audit of calls inside standard assertions.Fix: Evaluate the required key operations explicitly.
Audit the remaining call sites and use the always-evaluated
Assert()helper only when a call initializes output used afterward, mutates state, performs I/O, or releases resources.Leave pure predicates and calculations as standard assertions.
Reproducer: Compiler Explorer demonstrates
assert(keypair_xonly_pub(&pubkey))skipping the required key derivation under-DNDEBUG.