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. MarcoFalke 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


cculianu

Labels
Bug P2P


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: 2024-11-23 06:12 UTC

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