Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 19a88669 authored by Greg Kaiser's avatar Greg Kaiser
Browse files

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
parent e9437d2e
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -255,7 +255,15 @@ private:
            mValid = false;
            value = 0; // all primitive types can successfully cast 0
        } else {
            // 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);
        }