I am working on fixing the below problem in C-program on Linux (4.19.21-linux-gnu-gcc),
//Structure definition :
struct data_from_u64 {
uint32_t u32_value;
uint16_t u16_value;
uint8_t u8_value1;
uint8_t u8_value2;
}
//Expectation : Convert a incoming uint64_t variable to the above structure using the below macro,
#define U64_TO_STRUCT(u64_value) ( * (data_from_u64 *) ((void*) &(u64_value)) )
//Usage
data_from_u64 data = U64_TO_STRUCT(u64_value);
The project is huge that it is very widely used, but the new compilation procedure is enforcing "-O2" and "-Werror" flag while compiling. Can you help resolve the error/warning mentioned below. change should be minimal as this macro is used in more than 1000 places.
Error/Warning:
error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing] #define U64_TO_STRUCT(u64_value) ( * (data_from_u64 ) ((void) &(u64_value)) )
What have i tried :
I have tried doing memcpy, it works. But it requires to be updated in all the 1000places of usage[it requires parameter of the macro to be changed to include the destination].