random: Initialize variables in hardware RNG functions #31863
pull eval-exec wants to merge 1 commits into bitcoin:master from eval-exec:exec/initialize-variables-in-random changing 1 files +5 −5-
eval-exec commented at 9:08 am on February 14, 2025: contributorSee: #31826 (review) , So this PR want to prevent potential uninitialized value issues and improve code clarity.
-
DrahtBot commented at 9:09 am on February 14, 2025: contributor
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
Code Coverage & Benchmarks
For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/31863.
Reviews
See 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.
-
in src/random.cpp:219 in ffac2733c9 outdated
214@@ -215,8 +215,8 @@ void ReportHardwareRand() 215 */ 216 uint64_t GetRNDR() noexcept 217 { 218- uint8_t ok; 219- uint64_t r1; 220+ uint8_t ok = 0; 221+ uint64_t r1 = 0;
maflcko commented at 11:07 am on February 14, 2025:Setting the sanity check value to a default of
0
is fine, because if it was never set, it will run in an infinite loop, just as-if the rng failed.However, setting the random value that is returned to a default value is problematic and controversial, because:
- It silences sanitizers such as msan and valgrind that detect uninitialized reads
- If we wanted to zero-init everything, we could just do #18892 (or a project-wide source change)
- The initialization above was added in commit 71f183a49b714a28622277fa668d8f9f3dac0aae, however, it is not needed here, so adding it here as well seems odd. At least, I’d expect a rationale for it.
eval-exec commented at 11:24 am on February 14, 2025:Agreed, setingok = 0
is enough.
random: Initialize variables in hardware RNG functions 99755e04ffeval-exec force-pushed on Feb 14, 2025luke-jr commented at 7:57 pm on February 14, 2025: memberimprove code clarity.
Initializing variables that do not need initializing is worse for code clarity.
If you want to silence buggy static analysis, I don’t mind (~0), but it’s not a code clarity fix…
sipa commented at 8:01 pm on February 14, 2025: memberEagerly awaiting C++26’s explicit[[indeterminate]]
initialization.theuni approvedtheuni commented at 8:44 pm on February 14, 2025: memberutACk 99755e04ffadd5daad53c686f4f0feee2232eeb2sipa commented at 8:51 pm on February 14, 2025: memberutACK 99755e04ffadd5daad53c686f4f0feee2232eeb2achow101 commented at 10:43 pm on February 14, 2025: memberACK 99755e04ffadd5daad53c686f4f0feee2232eeb2achow101 merged this on Feb 14, 2025achow101 closed this on Feb 14, 2025
maflcko commented at 8:41 am on February 17, 2025: memberEagerly awaiting C++26’s explicit
[[indeterminate]]
initialization.Maybe I am missing something, but I don’t think
[[indeterminate]]
should be used here either. My understanding is that[[indeterminate]]
in C++26 is simply a way to restore pre-C++-26 UB. However, if there really is UB in one of those functions, we’d want the C++26 erroneous behavior so that the implementation is encouraged to issue a diagnostic (and possibly abort) to notify about the incorrect program code.Though, given that no implementation exists for
[[indeterminate]]
(nor C++26 erroneous values), it is hard to say what the overhead would be for one over the other and in what cases the compiler can optimize and rule out the erroneous handling.
This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2025-02-22 06:12 UTC
More mirrored repositories can be found on mirror.b10c.me