From e8498b9264c2397ff1a101c9eef7676ca95d6c3c Mon Sep 17 00:00:00 2001 From: Tsuda kageyu Date: Sat, 20 Apr 2013 05:54:06 +0900 Subject: [PATCH] Fix -Wcast-align violation --- taglib/riff/aiff/aiffproperties.cpp | 4 +-- taglib/toolkit/tbytevector.cpp | 8 ++++-- taglib/toolkit/tstring.cpp | 39 ++++++++++++++++------------- taglib/toolkit/tstring.h | 1 - 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/taglib/riff/aiff/aiffproperties.cpp b/taglib/riff/aiff/aiffproperties.cpp index 1ed03d6b..18b973eb 100644 --- a/taglib/riff/aiff/aiffproperties.cpp +++ b/taglib/riff/aiff/aiffproperties.cpp @@ -39,7 +39,7 @@ #define UnsignedToFloat(u) (((double)((long)(u - 2147483647L - 1))) + 2147483648.0) -static double ConvertFromIeeeExtended(unsigned char *bytes) +static double ConvertFromIeeeExtended(const TagLib::uchar *bytes) { double f; int expon; @@ -153,7 +153,7 @@ void RIFF::AIFF::Properties::read(const ByteVector &data) d->channels = data.toShort(0U); d->sampleFrames = data.toUInt(2U); d->sampleWidth = data.toShort(6U); - double sampleRate = ConvertFromIeeeExtended(reinterpret_cast(data.mid(8, 10).data())); + double sampleRate = ConvertFromIeeeExtended(reinterpret_cast(data.data() + 8)); d->sampleRate = (int)sampleRate; d->bitrate = (int)((sampleRate * d->sampleWidth * d->channels) / 1000.0); d->length = d->sampleRate > 0 ? d->sampleFrames / d->sampleRate : 0; diff --git a/taglib/toolkit/tbytevector.cpp b/taglib/toolkit/tbytevector.cpp index e6e8cb85..f1ca4c78 100644 --- a/taglib/toolkit/tbytevector.cpp +++ b/taglib/toolkit/tbytevector.cpp @@ -209,10 +209,14 @@ T toNumber(const ByteVector &v, size_t offset, bool mostSignificantByteFirst) return 0; } + // Uses memcpy instead of reinterpret_cast to avoid an alignment exception. + T tmp; + ::memcpy(&tmp, v.data() + offset, sizeof(T)); + if(isLittleEndianSystem == mostSignificantByteFirst) - return byteSwap(*reinterpret_cast(v.data() + offset)); + return byteSwap(tmp); else - return *reinterpret_cast(v.data() + offset); + return tmp; } template diff --git a/taglib/toolkit/tstring.cpp b/taglib/toolkit/tstring.cpp index bb0b0b93..22590c89 100644 --- a/taglib/toolkit/tstring.cpp +++ b/taglib/toolkit/tstring.cpp @@ -789,16 +789,32 @@ void String::copyFromUTF16(const wchar_t *s, size_t length, Type t) } } -template -void String::internalCopyFromUTF16(const char *s, size_t length, Type t) +void String::copyFromUTF16(const char *s, size_t length, Type t) { - // Non specialized version. Used where sizeof(wchar_t) != 2. +#if !defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) + + // It's certain that sizeof(wchar_t) == 2 and alignment-tolerant. + + copyFromUTF16(reinterpret_cast(s), length / 2, t); + +#else + + // Maybe sizeof(wchar_t) != 2 or alignment-strict. bool swap; if(t == UTF16) { - if(length >= 2 && *reinterpret_cast(s) == 0xfeff) + if(length < 2) { + debug("String::copyFromUTF16() - Invalid UTF16 string."); + return; + } + + // Uses memcpy instead of reinterpret_cast to avoid an alignment exception. + ushort bom; + ::memcpy(&bom, s, 2); + + if(bom == 0xfeff) swap = false; // Same as CPU endian. No need to swap bytes. - else if(length >= 2 && *reinterpret_cast(s) == 0xfffe) + else if(bom == 0xfffe) swap = true; // Not same as CPU endian. Need to swap bytes. else { debug("String::copyFromUTF16() - Invalid UTF16 string."); @@ -816,19 +832,8 @@ void String::internalCopyFromUTF16(const char *s, size_t length, Type t) d->data[i] = swap ? combine(*s, *(s + 1)) : combine(*(s + 1), *s); s += 2; } -} - -template <> -void String::internalCopyFromUTF16<2>(const char *s, size_t length, Type t) -{ - // Specialized version for where sizeof(wchar_t) == 2. - - copyFromUTF16(reinterpret_cast(s), length / 2, t); -} -void String::copyFromUTF16(const char *s, size_t length, Type t) -{ - internalCopyFromUTF16(s, length, t); +#endif } const String::Type String::WCharByteOrder = isLittleEndianSystem ? String::UTF16LE : String::UTF16BE; diff --git a/taglib/toolkit/tstring.h b/taglib/toolkit/tstring.h index 150d7c37..7811ecf6 100644 --- a/taglib/toolkit/tstring.h +++ b/taglib/toolkit/tstring.h @@ -474,7 +474,6 @@ namespace TagLib { */ void copyFromUTF16(const char *s, size_t length, Type t); - template void internalCopyFromUTF16(const char *s, size_t length, Type t); /*! -- 2.40.0