MantisBT - CMake
View Issue Details
0014314CMakeCMakepublic2013-07-25 12:172013-12-02 08:51
Petr Machata 
Brad King 
normalminoralways
closedfixed 
GCCAny
CMake 2.8.11.2 
CMake 2.8.12CMake 2.8.12 
0014314: GCC warns about strict aliasing in cm_sha2.c
When compiling cm_sha2.c, GCC gives the following warning:

/builddir/build/BUILD/cmake-2.8.11.2/Source/cm_sha2.c: In function 'cmSHA1_Final':
/builddir/build/BUILD/cmake-2.8.11.2/Source/cm_sha2.c:743:2: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
  *(sha_word64*)&context->s1.buffer[56] = context->s1.bitcount;
  ^

There are four cases total.
1) Compile with recent GCC (4.8?)
2) Inspect compile log
I propose fixing this by copying the necessary bytes through union. This should lead to the same binary, but will make it clear to GCC that we are only using the buffer as a bunch of characters. The test suite passes after applying the patch, that I attach.
No tags attached.
patch cmake-2.8.11-strict_aliasing.patch (1,947) 2013-07-25 12:17
https://public.kitware.com/Bug/file/4824/cmake-2.8.11-strict_aliasing.patch
patch cmake-strict_aliasing.patch (648) 2013-07-25 12:42
https://public.kitware.com/Bug/file/4825/cmake-strict_aliasing.patch
Issue History
2013-07-25 12:17Petr MachataNew Issue
2013-07-25 12:17Petr MachataFile Added: cmake-2.8.11-strict_aliasing.patch
2013-07-25 12:42Petr MachataNote Added: 0033610
2013-07-25 12:42Petr MachataFile Added: cmake-strict_aliasing.patch
2013-07-25 13:13Brad KingNote Added: 0033611
2013-07-25 14:06Petr MachataNote Added: 0033613
2013-07-25 14:35Brad KingNote Added: 0033614
2013-07-25 14:35Brad KingAssigned To => Brad King
2013-07-25 14:35Brad KingStatusnew => resolved
2013-07-25 14:35Brad KingResolutionopen => fixed
2013-07-25 14:35Brad KingFixed in Version => CMake 2.8.12
2013-07-25 14:35Brad KingTarget Version => CMake 2.8.12
2013-12-02 08:51Robert MaynardNote Added: 0034658
2013-12-02 08:51Robert MaynardStatusresolved => closed

Notes
(0033610)
Petr Machata   
2013-07-25 12:42   
Alternatively, you can pass -fno-strict-aliasing for that file when compiling with GCC.
(0033611)
Brad King   
2013-07-25 13:13   
Why not just use memcpy directly? It converts through pointer-to-void.

diff --git a/Source/cm_sha2.c b/Source/cm_sha2.c
index 12c39ed..24de2b2 100644
--- a/Source/cm_sha2.c
+++ b/Source/cm_sha2.c
@@ -740,7 +740,8 @@ void SHA1_Final(sha_byte digest[], SHA_CTX* context) {
        /* Convert FROM host byte order */
        REVERSE64(context->s1.bitcount,context->s1.bitcount);
 #endif
-       *(sha_word64*)&context->s1.buffer[56] = context->s1.bitcount;
+       MEMCPY_BCOPY(&context->s1.buffer[56], &context->s1.bitcount,
+                    sizeof(sha_word64));
 
        /* Final transform: */
        SHA1_Internal_Transform(context, (sha_word32*)context->s1.buffer);
@@ -1067,7 +1068,8 @@ void SHA256_Internal_Last(SHA_CTX* context) {
                *context->s256.buffer = 0x80;
        }
        /* Set the bit count: */
-       *(sha_word64*)&context->s256.buffer[56] = context->s256.bitcount;
+       MEMCPY_BCOPY(&context->s256.buffer[56], &context->s256.bitcount,
+                    sizeof(sha_word64));
 
        /* Final transform: */
        SHA256_Internal_Transform(context, (sha_word32*)context->s256.buffer);
@@ -1475,8 +1477,10 @@ void SHA512_Internal_Last(SHA_CTX* context) {
                *context->s512.buffer = 0x80;
        }
        /* Store the length of input data (in bits): */
-       *(sha_word64*)&context->s512.buffer[112] = context->s512.bitcount[1];
-       *(sha_word64*)&context->s512.buffer[120] = context->s512.bitcount[0];
+       MEMCPY_BCOPY(&context->s512.buffer[112], &context->s512.bitcount[1],
+                    sizeof(sha_word64));
+       MEMCPY_BCOPY(&context->s512.buffer[120], &context->s512.bitcount[0],
+                    sizeof(sha_word64));
 
        /* Final transform: */
        SHA512_Internal_Transform(context, (sha_word64*)context->s512.buffer);
(0033613)
Petr Machata   
2013-07-25 14:06   
Yes, I think you can. I don't see why it wouldn't work.
(0033614)
Brad King   
2013-07-25 14:35   
Thanks, patch applied:

 http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=6a365d09 [^]
(0034658)
Robert Maynard   
2013-12-02 08:51   
Closing resolved issues that have not been updated in more than 4 months.