Merge "Switch to memcpy for accessing misaligned data."

This commit is contained in:
Stephen Hines 2016-12-14 02:51:34 +00:00 committed by Gerrit Code Review
commit 2bdb37190c
2 changed files with 8 additions and 14 deletions

View file

@ -20,25 +20,19 @@
namespace android { namespace android {
namespace base { namespace base {
// Use packed structures for access to unaligned data on targets with alignment // Use memcpy for access to unaligned data on targets with alignment
// restrictions. The compiler will generate appropriate code to access these // restrictions. The compiler will generate appropriate code to access these
// structures without generating alignment exceptions. // structures without generating alignment exceptions.
template <typename T> template <typename T>
static inline T get_unaligned(const T* address) { static inline T get_unaligned(const void* address) {
struct unaligned { T result;
T v; memcpy(&result, address, sizeof(T));
} __attribute__((packed)); return result;
const unaligned* p = reinterpret_cast<const unaligned*>(address);
return p->v;
} }
template <typename T> template <typename T>
static inline void put_unaligned(T* address, T v) { static inline void put_unaligned(void* address, T v) {
struct unaligned { memcpy(address, &v, sizeof(T));
T v;
} __attribute__((packed));
unaligned* p = reinterpret_cast<unaligned*>(address);
p->v = v;
} }
} // namespace base } // namespace base

View file

@ -152,7 +152,7 @@ public:
} }
bool DoVerify(const password_handle_t *expected_handle, const SizedBuffer &password) { bool DoVerify(const password_handle_t *expected_handle, const SizedBuffer &password) {
uint64_t user_id = android::base::get_unaligned(&expected_handle->user_id); uint64_t user_id = android::base::get_unaligned<secure_id_t>(&expected_handle->user_id);
FastHashMap::const_iterator it = fast_hash_map_.find(user_id); FastHashMap::const_iterator it = fast_hash_map_.find(user_id);
if (it != fast_hash_map_.end() && VerifyFast(it->second, password)) { if (it != fast_hash_map_.end() && VerifyFast(it->second, password)) {
return true; return true;