aboutsummaryrefslogtreecommitdiffhomepage
path: root/objectivec
diff options
context:
space:
mode:
authorGravatar Thomas Van Lenten <thomasvl@google.com>2018-01-31 15:25:13 -0500
committerGravatar Thomas Van Lenten <thomasvl@google.com>2018-01-31 15:40:22 -0500
commitd570d48648503e57f3abfcd9b9516583768fec48 (patch)
tree9ac715336e45fc6b9aa071b7e2e144f220f6b439 /objectivec
parentd83837ded558bc2fd2b61a5f51ed52e4606345aa (diff)
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.
Diffstat (limited to 'objectivec')
-rw-r--r--objectivec/GPBDescriptor.m6
1 files changed, 4 insertions, 2 deletions
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);