Commit 7f8ce3d9 authored by jwd@chromium.org's avatar jwd@chromium.org

Fixing issues with alignment, undefined behaviour and endianness in the...

Fixing issues with alignment, undefined behaviour and endianness in the FieldTrial::HashName function. I also added new functions to base that convert from host order to little endian.

BUG=123230FieldTrialTest.HashName
TEST=FieldTrialTest.HashName


Review URL: http://codereview.chromium.org/10088001

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@132404 0039d316-1c4b-4281-b951-d872f2087c98
parent ab675b68
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/sha1.h" #include "base/sha1.h"
#include "base/stringprintf.h" #include "base/stringprintf.h"
#include "base/string_util.h" #include "base/string_util.h"
#include "base/sys_byteorder.h"
#include "base/utf_string_conversions.h" #include "base/utf_string_conversions.h"
namespace base { namespace base {
...@@ -212,13 +213,14 @@ uint32 FieldTrial::HashName(const std::string& name) { ...@@ -212,13 +213,14 @@ uint32 FieldTrial::HashName(const std::string& name) {
sha1_hash); sha1_hash);
COMPILE_ASSERT(sizeof(uint32) < sizeof(sha1_hash), need_more_data); COMPILE_ASSERT(sizeof(uint32) < sizeof(sha1_hash), need_more_data);
uint32* bits = reinterpret_cast<uint32*>(&sha1_hash[0]); uint32 bits;
memcpy(&bits, sha1_hash, sizeof(bits));
// We only DCHECK, since this should not happen because the registration // We only DCHECK, since this should not happen because the registration
// of the experiment on the server should have already warn the developer. // of the experiment on the server should have already warn the developer.
// If this ever happen, we'll ignore this experiment/group when reporting. // If this ever happen, we'll ignore this experiment/group when reporting.
DCHECK(*bits != kReservedHashValue); DCHECK(bits != kReservedHashValue);
return *bits; return base::ByteSwapToLE32(bits);
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
......
...@@ -69,6 +69,30 @@ inline uint64 ByteSwap(uint64 x) { ...@@ -69,6 +69,30 @@ inline uint64 ByteSwap(uint64 x) {
#endif #endif
} }
// Converts the bytes in |x| from host order (endianness) to little endian, and
// returns the result.
inline uint16 ByteSwapToLE16(uint16 x) {
#if defined(ARCH_CPU_LITTLE_ENDIAN)
return x;
#else
return ByteSwap(x);
#endif
}
inline uint32 ByteSwapToLE32(uint32 x) {
#if defined(ARCH_CPU_LITTLE_ENDIAN)
return x;
#else
return ByteSwap(x);
#endif
}
inline uint64 ByteSwapToLE64(uint64 x) {
#if defined(ARCH_CPU_LITTLE_ENDIAN)
return x;
#else
return ByteSwap(x);
#endif
}
// Converts the bytes in |x| from network to host order (endianness), and // Converts the bytes in |x| from network to host order (endianness), and
// returns the result. // returns the result.
inline uint16 NetToHost16(uint16 x) { inline uint16 NetToHost16(uint16 x) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment