Skip to content

memcmp with constants that contain zero bytes are broken in GCC #20005

Closed
@sipa

Description

@sipa

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 bitcoin-core/secp256k1#822. It is being tracked for libsecp256k1 in bitcoin-core/secp256k1#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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions