Hi!

Compilation of md5.cpp fails because of a potential problem detected by gcc 4.7.0:

 "dereferencing type-punned pointer will break strict aliasing".

The attached patch is absolutely NOT tested. Is there any test suite for these classes?

I found the following post by a former colleague to be a good intro on the topic of type-punning and strict-aliasing:

 http://labs.qt.nokia.com/2011/06/10/type-punning-and-strict-aliasing/

Harri.
From fc550799a2d48e6f70db9ef0f323aa52a26abf94 Mon Sep 17 00:00:00 2001
From: Harri Porten <[email protected]>
Date: Sun, 29 Jul 2012 18:01:28 +0200
Subject: [PATCH] Avoid aliasing of type-punned pointers.

gcc warns about "dereferencing type-punned pointer will break
strict aliasing". Dereferencing a cast of a variable from one
type of pointer to a different type can violate the strict aliasing
rule.
---
 core/md5.cpp |    5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/core/md5.cpp b/core/md5.cpp
index ff0147b..ff7661c 100644
--- a/core/md5.cpp
+++ b/core/md5.cpp
@@ -5,7 +5,7 @@
 #include "estring.h"
 #include "buffer.h"
 
-// memmove, memset
+// memmove, memset, memcpy
 #include <string.h>
 
 
@@ -150,8 +150,7 @@ EString MD5::hash()
     swapBytes( in, 14 );
 
     /* Append length in bits and transform. */
-    ((uint32 *)in)[14] = bits[0];
-    ((uint32 *)in)[15] = bits[1];
+    memcpy( in + 14 * sizeof( uint32 ), bits, sizeof( bits ) );
     transform();
     swapBytes( (char *)buf, 4 );
 
-- 
1.7.10.4

Reply via email to