Only define GetDevURandom() if it is going to be used.
Silence by planting a dummy reference to the GetDevURandom symbol
in the places where we don't call the function.
Only define GetDevURandom() if it is going to be used.
Silence by planting a dummy reference to the GetDevURandom symbol
in the places where we don't call the function.
What warning are you seeing, compiling for which OS?
247 | @@ -248,7 +248,9 @@ static void Strengthen(const unsigned char (&seed)[32], int microseconds, CSHA51 248 | memory_cleanse(buffer, sizeof(buffer)); 249 | } 250 | 251 | -#ifndef WIN32 252 | +#if !defined(WIN32) && \ 253 | + !(defined(HAVE_GETENTROPY) && defined(__OpenBSD__)) && \
Concept ACK, but I think there's some redundancy in this experession: at the least, WIN32 will never be set if HAVE_GETENTROPY or HAVE_SYSCTL_ARND.
Edit: oh, that doesn't really help. I still think this is too complex an expression, would be good to have a NEED_DEVURANDOM_FALLBACK or such.
random.cpp:255:13: error: unused function 'GetDevURandom' [-Werror,-Wunused-function]
static void GetDevURandom(unsigned char *ent32)
^
1 error generated.
FreeBSD 12.1. @laanwj I agree that the "forest" of ifdef is totally unreadable.
I changed the approach to mark the function as possibly unused. After all the compilers provide a way to do that exactly for cases like this. Once we are on C++17, we can use [[maybe_unused]] directly in the code. Before that some magic in src/attributes.h, similarly to NODISCARD.
Marking that unused would be a great way to hid an actual bug in the future. Would it be possible to #define a USEGETDEVURANDOM as part of the conditional compilation that makes it unused and ifndef the function out if it won't be used?
Marking that unused would be a great way to hid an actual bug in the future.
Like what?
Would it be possible to #define a USEGETDEVURANDOM as part of the conditional compilation that makes it unused and ifndef the function out if it won't be used?
I don't think so. We have:
static GetDevURandom() { ... }
...
GetOSRand() {
#ifdef FOO
... don't use GetDevURandom() ...
#elif BAR
... use GetDevURandom() ...
#elif BAZ
... don't use GetDevURandom() ...
#else whatnot
... use GetDevURandom() ...
#endif
}
GetDevURandom() must be declared before GetOSRand(). If we want to
then we have to use the forest of ifdefs from the first iteration of this PR - replicate the same conditions as inside GetOSRand() before it.
There are also other ways to silence this:
GetDevURandom() (works for GCC and Clang, Visual Studio does not produce warning for this anyway):#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#endif
static void GetDevURandom() { ... }
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
Remove the static qualifier. Possibly add inline hoping that it will not be defined as a global symbol. static inline does not produce a warning with GCC, but still does for Clang.
I am not sure what @laanwj means with NEED_DEVURANDOM_FALLBACK, can you elaborate?
PS this seems to be causing a noise inversely proportional to the complexity of the change.
Just for reference, the warning appears on freebsd 12: https://cirrus-ci.com/task/4881774827536384?command=make#L423
I have no opinion on whether it should be fixed or how.
3rd attempt (https://github.com/bitcoin/bitcoin/commit/8a16b9d448b90aa5a234dc326ebda45b99b363b7): no marking as unused (but @gmaxwell what bug could that hide in the future, I am just curious?), no complex negated ifdef conditions that span 3 lines.
Looks good to me now. I don't think the current construction can hide any bug:
NEED_GET_DEV_URANDOM is set unnecessarily, the compiler warning comes backNEED_GET_DEV_URANDOM is left out in error, the compiler will throw an error because the function is used but missingACK 8a16b9d448b90aa5a234dc326ebda45b99b363b7
Rebased to get appveyor green, its failure was not due to this PR.
<!--e57a25ab6845829454e8d69fc972939a-->
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
<!--174a7506f384e20aa4161008e828411d-->
No conflicts as of last run.
```
random.cpp:255:13: error: unused function 'GetDevURandom' [-Werror,-Wunused-function]
```
Clang 9.0.0, FreeBSD 12.1
Silence by planting a dummy reference to the `GetDevURandom` symbol
in the places where we don't call the function.
248 | @@ -249,7 +249,22 @@ static void Strengthen(const unsigned char (&seed)[32], int microseconds, CSHA51 249 | memory_cleanse(buffer, sizeof(buffer)); 250 | } 251 | 252 | -#ifndef WIN32 253 | +#if defined(WIN32)
I don't know if I like effectively duplicating the which-random-source-to-use logic in macro statements. It feels like this too risks overlooking things.
One possibility is adding a (void)GetDevURandom; line in the exact places where we know that no /dev/urandom is needed (but only there, so that it doesn't silence an unintended lack of invocation). Such a statement will also silence the warning, and since the method is static, if it's unused, it still won't be included in the object code.
Ok, another ingenious way to silence this warning. The previous one did not gain traction for a few months, so lets see if this one flies. Updated the patch.
I confirm that the warning is gone and that the symbol is not included in the object code with -O1 or higher. It is included with -O0 or in the absence of any -O... option but this is fine.
I started working on a similar change to remove the warning on macOS, then remembered we had this PR. Will test shortly; if this is the way everyone is happy to solve the issue, let get this in.
This can be silenced in a few different ways. I am fine with either one, as long as it gets fixed. @sipa, what do you think about the last incarnation of this patch? I changed it according to your suggestion at #17563 (review)
ACK ca2e47437277ef6851a739f247b44e73a53f21a1, I have not tested the code, but I have reviewed it and it looks OK, I agree it can be merged.
re-ACK ca2e47437277ef6851a739f247b44e73a53f21a1, tested on macOS 10.15.6 + llvm clang 10.0.0
random.cpp:257:13: warning: unused function 'GetDevURandom' [-Wunused-function]
static void GetDevURandom(unsigned char *ent32)
^
1 warning generated.
ACK ca2e47437277ef6851a739f247b44e73a53f21a1 -- increased signal to noise in compiler diagnostics is good
utACK ca2e47437277ef6851a739f247b44e73a53f21a1
@fanquake Anything left to do here since your last comment?
just wanna say thanks for this! that warning was annoying.