forked from mirrors/gecko-dev
Bug 1465981 - Use memcpy instead of union-arm-punning (which has implementation-defined, desired behavior with gcc and presumably clang, and is not known to have problems on MSVC, but potentially could with other compilers) in BitwiseCast. r=froydnj
--HG-- extra : rebase_source : 686363576c84710ae0181afc32b05dee8b40a59b
This commit is contained in:
parent
6efbfd2a22
commit
7bbef1fafc
1 changed files with 15 additions and 7 deletions
|
|
@ -12,7 +12,9 @@
|
||||||
#include "mozilla/Assertions.h"
|
#include "mozilla/Assertions.h"
|
||||||
#include "mozilla/TypeTraits.h"
|
#include "mozilla/TypeTraits.h"
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
namespace mozilla {
|
namespace mozilla {
|
||||||
|
|
||||||
|
|
@ -43,13 +45,19 @@ BitwiseCast(const From aFrom, To* aResult)
|
||||||
{
|
{
|
||||||
static_assert(sizeof(From) == sizeof(To),
|
static_assert(sizeof(From) == sizeof(To),
|
||||||
"To and From must have the same size");
|
"To and From must have the same size");
|
||||||
union
|
|
||||||
{
|
// We could maybe downgrade these to std::is_trivially_copyable, but the
|
||||||
From mFrom;
|
// various STLs we use don't all provide it.
|
||||||
To mTo;
|
static_assert(std::is_trivial<From>::value,
|
||||||
} u;
|
"shouldn't bitwise-copy a type having non-trivial "
|
||||||
u.mFrom = aFrom;
|
"initialization");
|
||||||
*aResult = u.mTo;
|
static_assert(std::is_trivial<To>::value,
|
||||||
|
"shouldn't bitwise-copy a type having non-trivial "
|
||||||
|
"initialization");
|
||||||
|
|
||||||
|
std::memcpy(static_cast<void*>(aResult),
|
||||||
|
static_cast<const void*>(&aFrom),
|
||||||
|
sizeof(From));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename To, typename From>
|
template<typename To, typename From>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue