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

issue cculianu openend this issue on August 3, 2021
  1. cculianu commented at 0:01 am on August 3, 2021: none

    Expected behavior

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

    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.

  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: 2025-11-27 21:13 UTC

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