Skip to content

Commit

Permalink
Merge pull request Unidata#2050 from e4t/strict-aliasing
Browse files Browse the repository at this point in the history
Fix Compiler Strict Aliasing Rule Violations
  • Loading branch information
WardF authored Mar 10, 2022
2 parents 3bf18fb + 329eaaa commit f121e0b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
6 changes: 3 additions & 3 deletions libdap4/d4dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ NCD4_dumpbytes(size_t size, const void* data0, int swap)
if(swap) {
swapinline16(v.u16);
swapinline32(v.u32);
swapinline32(v.u64);
swapinline64(v.u64);
swapinline16(v.i16);
swapinline32(v.i32);
swapinline32(v.i64);
swapinline64(v.i64);
swapinline32(v.f32);
swapinline32(v.f64);
swapinline64(v.f64);
}
if(v.s[0] == '\r') strcpy(v.s,"\\r");
else if(v.s[0] == '\n') strcpy(v.s,"\\n");
Expand Down
40 changes: 20 additions & 20 deletions libdap4/d4util.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,39 @@ typedef struct D4blob {d4size_t size; void* memory;} D4blob;
/* signature: void swapinline16(void* ip) */
#define swapinline16(ip) \
{ \
union {char b[2]; unsigned short i;} u; \
char b[2]; \
char* src = (char*)(ip); \
u.b[0] = src[1]; \
u.b[1] = src[0]; \
*((unsigned short*)ip) = u.i; \
b[0] = src[1]; \
b[1] = src[0]; \
memcpy(ip, b, 2); \
}

/* signature: void swapinline32(void* ip) */
#define swapinline32(ip) \
{ \
union {char b[4]; unsigned int i;} u; \
char b[4]; \
char* src = (char*)(ip); \
u.b[0] = src[3]; \
u.b[1] = src[2]; \
u.b[2] = src[1]; \
u.b[3] = src[0]; \
*((unsigned int*)ip) = u.i; \
b[0] = src[3]; \
b[1] = src[2]; \
b[2] = src[1]; \
b[3] = src[0]; \
memcpy(ip, b, 4); \
}

/* signature: void swapinline64(void* ip) */
#define swapinline64(ip) \
{ \
union {char b[8]; unsigned long long i;} u; \
char b[8]; \
char* src = (char*)(ip); \
u.b[0] = src[7]; \
u.b[1] = src[6]; \
u.b[2] = src[5]; \
u.b[3] = src[4]; \
u.b[4] = src[3]; \
u.b[5] = src[2]; \
u.b[6] = src[1]; \
u.b[7] = src[0]; \
*((unsigned long long*)ip) = u.i; \
b[0] = src[7]; \
b[1] = src[6]; \
b[2] = src[5]; \
b[3] = src[4]; \
b[4] = src[3]; \
b[5] = src[2]; \
b[6] = src[1]; \
b[7] = src[0]; \
memcpy(ip, b, 8); \
}

/***************************************************/
Expand Down
3 changes: 2 additions & 1 deletion libdispatch/daux.c
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,8 @@ filterspec_cvt(const char* txt, size_t* nparamsp, unsigned int* params)
sstat = sscanf(p,"%lf",&vald);
if(sstat != 1) {stat = NC_EINVAL; goto done;}
valf = (float)vald;
params[nparams++] = *(unsigned int*)&valf;
/* avoid type punning */
memcpy(&params[nparams++], &valf, sizeof(unsigned int));
break;
/* The following are 8-byte values, so we must swap pieces if this
is a little endian machine */
Expand Down
8 changes: 6 additions & 2 deletions libsrc/ncx.m4
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,9 @@ inline static void
swap4b(void *dst, const void *src)
{
/* copy over, make the below swap in-place */
uint32_t tmp = *(uint32_t*)src;
uint32_t tmp;
/* use memcpy to avoid type punning */
memcpy(&tmp, src, sizeof(tmp));
tmp = SWAP4(tmp);
memcpy(dst, &tmp, 4);

Expand Down Expand Up @@ -464,7 +466,9 @@ swap8b(void *dst, const void *src)
op = (uint32_t*)((char*)dst+4);
*op = SWAP4(*op);
#else
uint64_t tmp = *(uint64_t*)src;
uint64_t tmp;
/* use memcpy to avoid type punning */
memcpy(&tmp, src, sizeof(tmp));
tmp = SWAP8(tmp);
memcpy(dst, &tmp, 8);

Expand Down
3 changes: 2 additions & 1 deletion oc2/xxdr.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,8 @@ xxdrntohdouble(char* c8, double* dp)
ii[0] = ii[1];
ii[1] = tmp;
}
if(dp) *dp = *(double*)ii;
/* use memcpy avoid type punning */
if(dp) memcpy(dp, ii, sizeof(double));
}

void
Expand Down

0 comments on commit f121e0b

Please sign in to comment.