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_cast
ed 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.