Skip to content

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

@cculianu

Description

@cculianu

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:

struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions