Issue Details
- Number
- 22613
- Title
- Net code contains some UB -- violations of strict aliasing rules in C++
- Description
- <!-- 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_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.
<!--- 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. -->
- URL
-
https://github.com/bitcoin/bitcoin/issue/22613
- Closed by
-
Back to List