You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by so...@apache.org on 2013/08/17 21:59:15 UTC

[2/5] git commit: TS-302: Fix INK_MD5 to not break strict aliasing. Additional refactoring.

TS-302: Fix INK_MD5 to not break strict aliasing. Additional refactoring.


Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/0f1c78b9
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/0f1c78b9
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/0f1c78b9

Branch: refs/heads/master
Commit: 0f1c78b9e6a32234924c7eafb61d5adab2a988b3
Parents: 9b55369
Author: Phil Sorber <so...@apache.org>
Authored: Fri Aug 16 13:39:14 2013 -0600
Committer: Phil Sorber <so...@apache.org>
Committed: Sat Aug 17 11:07:11 2013 -0600

----------------------------------------------------------------------
 lib/ts/INK_MD5.h | 78 ++++++++++++++++++++++++---------------------------
 1 file changed, 37 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/0f1c78b9/lib/ts/INK_MD5.h
----------------------------------------------------------------------
diff --git a/lib/ts/INK_MD5.h b/lib/ts/INK_MD5.h
index fe26f6a..b046fa6 100644
--- a/lib/ts/INK_MD5.h
+++ b/lib/ts/INK_MD5.h
@@ -29,37 +29,41 @@
 
 struct INK_MD5
 {
-  uint64_t b[2];
+
+  // This union is needed to fix strict-aliasing warnings.
+  // It is anonymous so that the 'b' member can remain in
+  // the top level scope of the struct. This is because
+  // other code touches that directly. Once that code is
+  // also fixed we can make this an Alias64 type from
+  // ink_align.h
+  union
+  {
+    uint64_t b[2];   // Legacy placeholder
+    uint64_t u64[2];
+    uint32_t u32[4];
+    uint8_t  u8[16];
+  };
+
   const INK_MD5 & operator =(const INK_MD5 & md5)
   {
-    b[0] = md5.b[0];
-    b[1] = md5.b[1];
+    u64[0] = md5.u64[0];
+    u64[1] = md5.u64[1];
     return md5;
   }
   uint32_t word(int i)
   {
-    uint32_t *p = (uint32_t *) & b[0];
-    return p[i];
+    return u32[i];
   }
   unsigned char byte(int i)
   {
-    unsigned char *p = (unsigned char *) &b[0];
-    return p[i];
+    return u8[i];
   }
   INK_MD5 & loadFromBuffer(char *md5_buf) {
-    int i;
-    char *s, *d;
-
-    for (i = 0, s = md5_buf, d = (char *) (&(b[0])); i < 8; i++, *d++ = *s++);
-    for (i = 0, d = (char *) (&(b[1])); i < 8; i++, *d++ = *s++);
+    memcpy((void *) u8, (void *) md5_buf, 16);
     return (*this);
   }
   INK_MD5 & storeToBuffer(char *md5_buf) {
-    int i;
-    char *s, *d;
-
-    for (i = 0, d = md5_buf, s = (char *) (&(b[0])); i < 8; i++, *d++ = *s++);
-    for (i = 0, s = (char *) (&(b[1])); i < 8; i++, *d++ = *s++);
+    memcpy((void *) md5_buf, (void *) u8, 16);
     return (*this);
   }
   INK_MD5 & operator =(char *md5) {
@@ -71,18 +75,11 @@ struct INK_MD5
 
   char *toStr(char *md5_str)
   {
-    int i;
-    char *s, *d;
-
-    for (i = 0, d = md5_str, s = (char *) (&(b[0])); i < 8; i++, *d++ = *s++);
-    for (i = 0, s = (char *) (&(b[1])); i < 8; i++, *d++ = *s++);
-    return (md5_str);
+    return (char *) memcpy((void *) md5_str, (void *) u8, 16);
   }
   void encodeBuffer(unsigned char *buffer, int len)
   {
-    unsigned char md5[16];
-    ink_code_md5(buffer, len, md5);
-    *this = md5;
+    ink_code_md5(buffer, len, u8);
   }
   void encodeBuffer(const char *buffer, int len)
   {
@@ -90,7 +87,7 @@ struct INK_MD5
   }
   char *str()
   {
-    return ((char *) b);
+    return ((char *) u8);
   }
   char *toHexStr(char hex_md5[33])
   {
@@ -98,11 +95,11 @@ struct INK_MD5
   }
   void set(INK_MD5 & md5)
   {
-    loadFromBuffer((char *) &md5);
+    loadFromBuffer((char *) md5.u8);
   }
   void set(INK_MD5 * md5)
   {
-    loadFromBuffer((char *) md5);
+    loadFromBuffer((char *) md5->u8);
   }
   void set(char *p)
   {
@@ -110,18 +107,17 @@ struct INK_MD5
   }
   void set(uint64_t a1, uint64_t a2)
   {
-    b[0] = a1;
-    b[1] = a2;
+    u64[0] = a1;
+    u64[1] = a2;
   }
   char *string(char buf[33])
   {
     char hex_digits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
     int i, j;
-    unsigned char *u = (unsigned char *) &b[0];
 
     for (i = 0, j = 0; i < 16; i += 1, j += 2) {
-      buf[j + 0] = hex_digits[u[i] >> 4];
-      buf[j + 1] = hex_digits[u[i] & 0xF];
+      buf[j + 0] = hex_digits[u8[i] >> 4];
+      buf[j + 1] = hex_digits[u8[i] & 0xF];
     }
     buf[32] = '\0';
     return buf;
@@ -129,24 +125,24 @@ struct INK_MD5
 
   uint64_t fold() const
   {
-    return (b[0] ^ b[1]);
+    return (u64[0] ^ u64[1]);
   }
 
   uint64_t operator[] (int i) const
   {
-    return b[i];
+    return u64[i];
   }
   bool operator==(INK_MD5 const& md5)
   {
-    return b[0] == md5.b[0] && b[1] == md5.b[1];
+    return u64[0] == md5.u64[0] && u64[1] == md5.u64[1];
   }
   INK_MD5() {
-    b[0] = 0;
-    b[1] = 0;
+    u64[0] = 0;
+    u64[1] = 0;
   }
   INK_MD5(uint64_t a1, uint64_t a2) {
-    b[0] = a1;
-    b[1] = a2;
+    u64[0] = a1;
+    u64[1] = a2;
   }
 };