WaE: strict-aliasing issues

Change-Id: I394b61fac90e1c2c26b1a4f073b87a5d3ae3e666
Reviewed-on: https://gerrit.libreoffice.org/2105
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Tested-by: Stephan Bergmann <sbergman@redhat.com>
This commit is contained in:
Caolán McNamara
2013-02-11 15:29:57 +00:00
committed by Stephan Bergmann
parent 7e2cae964e
commit 07c3bc01cb
2 changed files with 55 additions and 42 deletions

View File

@@ -259,20 +259,23 @@ struct Memory32 {
} }
float getIso60599Binary32() const { float getIso60599Binary32() const {
// Create a copy in either case, for alingment: // Create a copy in either case, for alignment:
union {
unsigned char buf[4]; unsigned char buf[4];
float f;
} sa;
#if defined OSL_LITENDIAN #if defined OSL_LITENDIAN
buf[0] = byte[0]; sa.buf[0] = byte[0];
buf[1] = byte[1]; sa.buf[1] = byte[1];
buf[2] = byte[2]; sa.buf[2] = byte[2];
buf[3] = byte[3]; sa.buf[3] = byte[3];
#else #else
buf[0] = byte[3]; sa.buf[0] = byte[3];
buf[1] = byte[2]; sa.buf[1] = byte[2];
buf[2] = byte[1]; sa.buf[2] = byte[1];
buf[3] = byte[0]; sa.buf[3] = byte[0];
#endif #endif
return *reinterpret_cast< float * >(buf); return sa.f;
// assuming float is ISO 60599 binary32 // assuming float is ISO 60599 binary32
} }
}; };
@@ -293,28 +296,31 @@ struct Memory64 {
} }
double getIso60599Binary64() const { double getIso60599Binary64() const {
// Create a copy in either case, for alingment: // Create a copy in either case, for alignment:
union {
unsigned char buf[8]; unsigned char buf[8];
double d;
} sa;
#if defined OSL_LITENDIAN #if defined OSL_LITENDIAN
buf[0] = byte[0]; sa.buf[0] = byte[0];
buf[1] = byte[1]; sa.buf[1] = byte[1];
buf[2] = byte[2]; sa.buf[2] = byte[2];
buf[3] = byte[3]; sa.buf[3] = byte[3];
buf[4] = byte[4]; sa.buf[4] = byte[4];
buf[5] = byte[5]; sa.buf[5] = byte[5];
buf[6] = byte[6]; sa.buf[6] = byte[6];
buf[7] = byte[7]; sa.buf[7] = byte[7];
#else #else
buf[0] = byte[7]; sa.buf[0] = byte[7];
buf[1] = byte[6]; sa.buf[1] = byte[6];
buf[2] = byte[5]; sa.buf[2] = byte[5];
buf[3] = byte[4]; sa.buf[3] = byte[4];
buf[4] = byte[3]; sa.buf[4] = byte[3];
buf[5] = byte[2]; sa.buf[5] = byte[2];
buf[6] = byte[1]; sa.buf[6] = byte[1];
buf[7] = byte[0]; sa.buf[7] = byte[0];
#endif #endif
return *reinterpret_cast< double * >(buf); return sa.d;
// assuming double is ISO 60599 binary64 // assuming double is ISO 60599 binary64
} }
}; };

View File

@@ -956,27 +956,34 @@ void write64(osl::File & file, sal_uInt64 value) {
} }
void writeIso60599Binary32(osl::File & file, float value) { void writeIso60599Binary32(osl::File & file, float value) {
union {
unsigned char buf[4]; unsigned char buf[4];
*reinterpret_cast< float * >(buf) = value; float f;
} sa;
sa.f = value;
// assuming float is ISO 60599 binary32 // assuming float is ISO 60599 binary32
#if defined OSL_BIGENDIAN #if defined OSL_BIGENDIAN
std::swap(buf[0], buf[3]); std::swap(sa.buf[0], sa.buf[3]);
std::swap(buf[1], buf[2]); std::swap(sa.buf[1], sa.buf[2]);
#endif #endif
write(file, buf, SAL_N_ELEMENTS(buf)); write(file, sa.buf, SAL_N_ELEMENTS(sa.buf));
} }
void writeIso60599Binary64(osl::File & file, double value) { void writeIso60599Binary64(osl::File & file, double value) {
union
{
unsigned char buf[8]; unsigned char buf[8];
*reinterpret_cast< double * >(buf) = value; float d;
} sa;
sa.d = value;
// assuming double is ISO 60599 binary64 // assuming double is ISO 60599 binary64
#if defined OSL_BIGENDIAN #if defined OSL_BIGENDIAN
std::swap(buf[0], buf[7]); std::swap(sa.buf[0], sa.buf[7]);
std::swap(buf[1], buf[6]); std::swap(sa.buf[1], sa.buf[6]);
std::swap(buf[2], buf[5]); std::swap(sa.buf[2], sa.buf[5]);
std::swap(buf[3], buf[4]); std::swap(sa.buf[3], sa.buf[4]);
#endif #endif
write(file, buf, SAL_N_ELEMENTS(buf)); write(file, sa.buf, SAL_N_ELEMENTS(sa.buf));
} }
rtl::OString toAscii(rtl::OUString const & name) { rtl::OString toAscii(rtl::OUString const & name) {