summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Greg Kaiser <gkaiser@google.com> 2020-01-29 13:48:09 -0800
committer Greg Kaiser <gkaiser@google.com> 2020-01-29 15:28:55 -0800
commit19a88669c37b67d698bb6acbe87bc2f016112aef (patch)
treeb7a5bcb125312764cb6047dd8e3fc5626c8c6426
parente9437d2ee36f2b657e9d8e533c84634101bcd6ea (diff)
LogEvent: Avoid unaligned assignment of value
Since we can have 'T' be things like int64_t, and we're arbitrarily incrementing 'mBuf', we're not always assured 'mBuf' will be properly aligned for an assignment. We do a check and use memcpy() if it's going to be unsafe. Test: TreeHugger Bug: 148549384 Change-Id: I76fd12a3241a997acf4625c9ce1176ae76c5bff2
-rw-r--r--cmds/statsd/src/logd/LogEvent.h10
1 files changed, 9 insertions, 1 deletions
diff --git a/cmds/statsd/src/logd/LogEvent.h b/cmds/statsd/src/logd/LogEvent.h
index 0f33c56be42a..463a1b68f885 100644
--- a/cmds/statsd/src/logd/LogEvent.h
+++ b/cmds/statsd/src/logd/LogEvent.h
@@ -255,7 +255,15 @@ private:
mValid = false;
value = 0; // all primitive types can successfully cast 0
} else {
- value = *((T*)mBuf);
+ // When alignof(T) == 1, hopefully the compiler can optimize away
+ // this conditional as always true.
+ if ((reinterpret_cast<uintptr_t>(mBuf) % alignof(T)) == 0) {
+ // We're properly aligned, and can safely make this assignment.
+ value = *((T*)mBuf);
+ } else {
+ // We need to use memcpy. It's slower, but safe.
+ memcpy(&value, mBuf, sizeof(T));
+ }
mBuf += sizeof(T);
mRemainingLen -= sizeof(T);
}