Net code contains some UB -- violations of strict aliasing rules in C++ #22613

issue cculianu opened this issue on August 3, 2021
  1. cculianu commented at 12:01 AM on August 3, 2021: none

    <!-- This issue tracker is only for technical issues related to Bitcoin Core. General bitcoin questions and/or support requests are best directed to the Bitcoin StackExchange at https://bitcoin.stackexchange.com. For reporting security issues, please read instructions at https://bitcoincore.org/en/contact/. If the node is "stuck" during sync or giving "block checksum mismatch" errors, please ensure your hardware is stable by running memtest and observe CPU temperature with a load-test tool such as linpack before creating an issue! -->

    <!-- Describe the issue -->

    Expected behavior

    No undefined behavior in bitcoin's C++ code.

    <!--- What behavior did you expect? -->

    However, the Bitcoin code does contain UB in the net code in particular. All of the code that works with struct sockaddr needs to be inspected and fixed. For example here: https://github.com/bitcoin/bitcoin/blob/b620b2d58a55a88ad21da70cb2000863ef17b651/src/netaddress.cpp#L988

    It is illegal/UB in C++ to do this type of aliasing and dereference a pointer that is reinterpre_casted in this way. C++ has stricter aliasing rules than C. For reference, see this: https://en.cppreference.com/w/cpp/language/reinterpret_cast#Type_aliasing

    Suggested Fix

    Use std::memcpy to copy the structs around and dereference them after copying them.

    <!--- What was the actual behavior (provide screenshots if the issue is GUI-related)? -->

    <!--- How reliably can you reproduce the issue, what are the steps to do so? -->

    <!-- What version of Bitcoin Core are you using, where did you get it (website, self-compiled, etc)? -->

    <!-- What type of machine are you observing the error on (OS/CPU and disk type)? -->

    <!-- GUI-related issue? What is your operating system and its version? If Linux, what is your desktop environment and graphical shell? -->

    <!-- Any extra information that might be useful in the debugging process. -->

    <!--- This is normally the contents of a `debug.log` or `config.log` file. Raw text or a link to a pastebin type site are preferred. -->

  2. cculianu added the label Bug on Aug 3, 2021
  3. cculianu renamed this:
    Net code contains some UB -- violations of strict type aliasing rules in C++
    Net code contains some UB -- violations of strict aliasing rules in C++
    on Aug 3, 2021
  4. maflcko added the label P2P on Aug 3, 2021
  5. Adlai-Holler referenced this in commit 997de28dc8 on Apr 11, 2022
  6. Adlai-Holler referenced this in commit f488d2e3da on Apr 11, 2022
  7. Adlai-Holler referenced this in commit 77f0208cf9 on Apr 11, 2022
  8. Adlai-Holler referenced this in commit bc21a2b0df on Apr 15, 2022
  9. Adlai-Holler referenced this in commit d6466e889f on May 3, 2022
  10. Adlai-Holler referenced this in commit 0b3ad72146 on May 3, 2022
  11. willcl-ark commented at 2:12 PM on October 21, 2025: member

    This issue hasn’t attracted much interest from other contributors in quite some time.

    Given that, it doesn’t seem important enough to keep open indefinitely. I’m going to close it for now due to lack of activity, but pull requests or renewed discussion are always welcome.

    Comment here if you think this shoudl be re-opened.

  12. willcl-ark closed this on Oct 21, 2025


github-metadata-mirror

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: 2026-04-15 00:14 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me