From 07466260e2dcdb18a7ff8b666c78c710747f28ae Mon Sep 17 00:00:00 2001 From: "jimblandy@gmail.com" Date: Tue, 15 Dec 2009 17:11:54 +0000 Subject: [PATCH] Issue 42001: Breakpad Linux Dumper: remove compilation warnings in guid_creator.cc. Building on Ubuntu 9.10 with the distributed compiler (GCC 4.4.1), we get warnings like the following: guid_creator.cc:56: warning: dereferencing type-punned pointer will break strict-aliasing rules It doesn't matter in this case, but there's no crying need to use reinterpret casts in an endian-dependent way when there are plenty of well-defined ways to get the same effect. a=jimblandy, r=nealsid git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@447 4c0a9323-5329-0410-9bdc-e9ce6186880e --- src/common/linux/guid_creator.cc | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/common/linux/guid_creator.cc b/src/common/linux/guid_creator.cc index d133c6b0..7611cc3e 100644 --- a/src/common/linux/guid_creator.cc +++ b/src/common/linux/guid_creator.cc @@ -49,12 +49,26 @@ class GUIDGenerator { srandom(time(NULL)); } + static u_int32_t BytesToUInt32(const u_int8_t bytes[]) { + return ((u_int32_t) bytes[0] + | ((u_int32_t) bytes[1] << 8) + | ((u_int32_t) bytes[2] << 16) + | ((u_int32_t) bytes[3] << 24)); + } + + static void UInt32ToBytes(u_int8_t bytes[], u_int32_t n) { + bytes[0] = n & 0xff; + bytes[1] = (n >> 8) & 0xff; + bytes[2] = (n >> 16) & 0xff; + bytes[3] = (n >> 24) & 0xff; + } + bool CreateGUID(GUID *guid) const { guid->data1 = random(); guid->data2 = (u_int16_t)(random()); guid->data3 = (u_int16_t)(random()); - *reinterpret_cast(&guid->data4[0]) = random(); - *reinterpret_cast(&guid->data4[4]) = random(); + UInt32ToBytes(&guid->data4[0], random()); + UInt32ToBytes(&guid->data4[4], random()); return true; } }; @@ -72,8 +86,8 @@ bool GUIDToString(const GUID *guid, char *buf, int buf_len) { assert(buf_len > kGUIDStringLength); int num = snprintf(buf, buf_len, kGUIDFormatString, guid->data1, guid->data2, guid->data3, - *reinterpret_cast(&(guid->data4[0])), - *reinterpret_cast(&(guid->data4[4]))); + GUIDGenerator::BytesToUInt32(&(guid->data4[0])), + GUIDGenerator::BytesToUInt32(&(guid->data4[4]))); if (num != kGUIDStringLength) return false;