From d570d48648503e57f3abfcd9b9516583768fec48 Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Wed, 31 Jan 2018 15:25:13 -0500 Subject: Don't assume c-strings are 4 byte aligned. The Undefined Behavior sanitizer flags one part of the unittests for this. For default values for `bytes` we write a length on the front of a c-string in the static data, apparently the compiler/linker doesn't always make this 4 byte aligned, so it get flagged for undefined/degraded performance. Avoid this by using memcpy instead. --- objectivec/GPBDescriptor.m | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'objectivec') diff --git a/objectivec/GPBDescriptor.m b/objectivec/GPBDescriptor.m index 3c3844da..615d2234 100644 --- a/objectivec/GPBDescriptor.m +++ b/objectivec/GPBDescriptor.m @@ -548,7 +548,8 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) { // descriptor structure. const uint8_t *bytes = (const uint8_t *)defaultValue_.valueData; if (bytes) { - uint32_t length = *((uint32_t *)bytes); + uint32_t length; + memcpy(&length, bytes, sizeof(length)); length = ntohl(length); bytes += sizeof(length); defaultValue_.valueData = @@ -963,7 +964,8 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) { const uint8_t *bytes = (const uint8_t *)description->defaultValue.valueData; if (bytes) { - uint32_t length = *((uint32_t *)bytes); + uint32_t length; + memcpy(&length, bytes, sizeof(length)); // The length is stored in network byte order. length = ntohl(length); bytes += sizeof(length); -- cgit v1.2.3