From: Wayne Davison Date: Wed, 14 Oct 2009 04:07:46 +0000 (-0700) Subject: Avoid type-punned compiler warnings for the byteorder.h macros X-Git-Url: https://mattmccutchen.net/rsync/rsync.git/commitdiff_plain/df6350a8b83a9e669f5e5c822bf2dc929526a128 Avoid type-punned compiler warnings for the byteorder.h macros by using inline functions for the 4-char <-> uint32 conversions. --- diff --git a/byteorder.h b/byteorder.h index 6dd9b459..146b861f 100644 --- a/byteorder.h +++ b/byteorder.h @@ -19,6 +19,7 @@ */ #undef CAREFUL_ALIGNMENT +#undef AVOID_BYTEORDER_INLINE /* We know that the x86 can handle misalignment and has the same * byte order (LSB-first) as the 32-bit numbers we transmit. */ @@ -32,21 +33,68 @@ #define CVAL(buf,pos) (((unsigned char *)(buf))[pos]) #define UVAL(buf,pos) ((uint32)CVAL(buf,pos)) -#define SCVAL(buf,pos,val) (CVAL(buf,pos) = (val)) #if CAREFUL_ALIGNMENT + #define PVAL(buf,pos) (UVAL(buf,pos)|UVAL(buf,(pos)+1)<<8) #define IVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+2)<<16) #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8) #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16)) #define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val))) -#else -/* this handles things for architectures like the 386 that can handle - alignment errors */ -/* - WARNING: This section is dependent on the length of int32 - being correct. set CAREFUL_ALIGNMENT if it is not. -*/ + +#define IVALu(buf,pos) IVAL(buf,pos) +#define SIVALu(buf,pos,val) SIVAL(buf,pos,val) + +#else /* !CAREFUL_ALIGNMENT */ + +/* This handles things for architectures like the 386 that can handle alignment errors. + * WARNING: This section is dependent on the length of an int32 (and thus a uint32) + * being correct (4 bytes)! Set CAREFUL_ALIGNMENT if it is not. */ + +# ifdef AVOID_BYTEORDER_INLINE + #define IVAL(buf,pos) (*(uint32 *)((char *)(buf) + (pos))) #define SIVAL(buf,pos,val) IVAL(buf,pos)=((uint32)(val)) -#endif + +#define IVALu(buf,pos) IVAL(buf,pos) +#define SIVALu(buf,pos,val) SIVAL(buf,pos,val) + +# else /* !AVOID_BYTEORDER_INLINE */ + +static inline uint32 +IVALu(const uchar *buf, int pos) +{ + union { + const uchar *b; + const uint32 *num; + } u; + u.b = buf + pos; + return *u.num; +} + +static inline void +SIVALu(uchar *buf, int pos, uint32 val) +{ + union { + uchar *b; + uint32 *num; + } u; + u.b = buf + pos; + *u.num = val; +} + +static inline uint32 +IVAL(const char *buf, int pos) +{ + return IVALu((uchar*)buf, pos); +} + +static inline void +SIVAL(char *buf, int pos, uint32 val) +{ + SIVALu((uchar*)buf, pos, val); +} + +# endif /* !AVOID_BYTEORDER_INLINE */ + +#endif /* !CAREFUL_ALIGNMENT */ diff --git a/checksum.c b/checksum.c index 811b5b60..90d6ee3a 100644 --- a/checksum.c +++ b/checksum.c @@ -56,7 +56,7 @@ void get_checksum2(char *buf, int32 len, char *sum) md5_begin(&m); md5_update(&m, (uchar *)buf, len); if (checksum_seed) { - SIVAL(seedbuf, 0, checksum_seed); + SIVALu(seedbuf, 0, checksum_seed); md5_update(&m, seedbuf, 4); } md5_result(&m, (uchar *)sum); diff --git a/hashtable.c b/hashtable.c index d5f1d110..219210de 100644 --- a/hashtable.c +++ b/hashtable.c @@ -106,7 +106,7 @@ void *hashtable_find(struct hashtable *tbl, int64 key, int allocate_if_missing) uchar buf[4], *keyp = buf; int i; - SIVAL(buf, 0, key); + SIVALu(buf, 0, key); for (ndx = 0, i = 0; i < 4; i++) { ndx += keyp[i]; ndx += (ndx << 10); diff --git a/lib/md5.c b/lib/md5.c index 2c5b9014..4baa9638 100644 --- a/lib/md5.c +++ b/lib/md5.c @@ -38,22 +38,22 @@ static void md5_process(md_context *ctx, const uchar data[CSUM_CHUNK]) C = ctx->C; D = ctx->D; - X[0] = IVAL(data, 0); - X[1] = IVAL(data, 4); - X[2] = IVAL(data, 8); - X[3] = IVAL(data, 12); - X[4] = IVAL(data, 16); - X[5] = IVAL(data, 20); - X[6] = IVAL(data, 24); - X[7] = IVAL(data, 28); - X[8] = IVAL(data, 32); - X[9] = IVAL(data, 36); - X[10] = IVAL(data, 40); - X[11] = IVAL(data, 44); - X[12] = IVAL(data, 48); - X[13] = IVAL(data, 52); - X[14] = IVAL(data, 56); - X[15] = IVAL(data, 60); + X[0] = IVALu(data, 0); + X[1] = IVALu(data, 4); + X[2] = IVALu(data, 8); + X[3] = IVALu(data, 12); + X[4] = IVALu(data, 16); + X[5] = IVALu(data, 20); + X[6] = IVALu(data, 24); + X[7] = IVALu(data, 28); + X[8] = IVALu(data, 32); + X[9] = IVALu(data, 36); + X[10] = IVALu(data, 40); + X[11] = IVALu(data, 44); + X[12] = IVALu(data, 48); + X[13] = IVALu(data, 52); + X[14] = IVALu(data, 56); + X[15] = IVALu(data, 60); #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) @@ -192,8 +192,8 @@ void md5_result(md_context *ctx, uchar digest[MD5_DIGEST_LEN]) | (ctx->totalN2 << 3); low = (ctx->totalN << 3); - SIVAL(msglen, 0, low); - SIVAL(msglen, 4, high); + SIVALu(msglen, 0, low); + SIVALu(msglen, 4, high); last = ctx->totalN & 0x3F; padn = last < 56 ? 56 - last : 120 - last; @@ -201,10 +201,10 @@ void md5_result(md_context *ctx, uchar digest[MD5_DIGEST_LEN]) md5_update(ctx, md5_padding, padn); md5_update(ctx, msglen, 8); - SIVAL(digest, 0, ctx->A); - SIVAL(digest, 4, ctx->B); - SIVAL(digest, 8, ctx->C); - SIVAL(digest, 12, ctx->D); + SIVALu(digest, 0, ctx->A); + SIVALu(digest, 4, ctx->B); + SIVALu(digest, 8, ctx->C); + SIVALu(digest, 12, ctx->D); } void get_md5(uchar *out, const uchar *input, int n)