It appears that there is a bug in certain GCC releases (in the version 9 and 10 series) where an optimization step breaks correctness of memcmp
when at least one of the arguments is a compile-time constant array that contains at least one zero byte.
GCC bug is here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95189. It was stumbled upon by @roconnor-blockstream in https://github.com/bitcoin-core/secp256k1/pull/822. It is being tracked for libsecp256k1 in https://github.com/bitcoin-core/secp256k1/issues/823.
I have verified that in some instances it also affects C++, and may even affect std::lexicographical_compare
.
This may be relevant in some of our code (in particular, the CNetAddr
IP range checking does comparisons with constants that contain zeroes, but perhaps more).
Solutions:
- Build with
-fno-builtin-memcmp
, but we should measure performance impact. - Very carefully inspect the codebase for potential cases, and use a custom memcmp for those.
TODO:
- Verify if compiler-generated memcmp calls may be affected as well