less than 1 minute read

Categories:

Tags:

There are very few ways to use reinterpret_cast without invoking undefined behavior

#include <cstring>
struct S
{
    int i;
    int j;
};

// we are trying to access an object whose life time never began
void update_data(char* blob, int new_value)
{
    reinterpret_cast<S*>(blob)[2].j = new_value; //mov   dword ptr [rdi + 20], esi
}

void update_date_not_undefined(char* blob, int new_value)
{
    S obj[3];
    memcpy(obj, blob, sizeof(obj));
    obj[2].j = new_value;
    memcpy(blob, obj, sizeof(obj));

    //std::bitcast<> : cpp20 
}

Leave a comment