]> granicus.if.org Git - taglib/commitdiff
Fix -Wcast-align violation
authorTsuda kageyu <tsuda.kageyu@gmail.com>
Fri, 19 Apr 2013 20:54:06 +0000 (05:54 +0900)
committerTsuda kageyu <tsuda.kageyu@gmail.com>
Fri, 19 Apr 2013 20:54:06 +0000 (05:54 +0900)
taglib/riff/aiff/aiffproperties.cpp
taglib/toolkit/tbytevector.cpp
taglib/toolkit/tstring.cpp
taglib/toolkit/tstring.h

index 1ed03d6bff5ea1201719a2b4f4dccecf47a72c7e..18b973eb06c0c0c41d898e372d5ef395ce971629 100644 (file)
@@ -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<unsigned char *>(data.mid(8, 10).data()));
+  double sampleRate = ConvertFromIeeeExtended(reinterpret_cast<const uchar *>(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;
index e6e8cb852577002b005531b65190fbd1054f5415..f1ca4c78769931256d927697f8f383951734ff35 100644 (file)
@@ -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<T>(*reinterpret_cast<const T*>(v.data() + offset));
+    return byteSwap<T>(tmp);
   else
-    return *reinterpret_cast<const T*>(v.data() + offset);
+    return tmp;
 }
 
 template <class T>
index bb0b0b9390d73181d727c2cd291cafd5ff3d1a87..22590c898fc6d5bd092528fb5ce701fea2a59d2d 100644 (file)
@@ -789,16 +789,32 @@ void String::copyFromUTF16(const wchar_t *s, size_t length, Type t)
   }
 }
 
-template <size_t sizeOfWcharT>
-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<const wchar_t*>(s), length / 2, t);
+
+#else
+
+  // Maybe sizeof(wchar_t) != 2 or alignment-strict.
 
   bool swap;
   if(t == UTF16) {
-    if(length >= 2 && *reinterpret_cast<const TagLib::ushort*>(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<const TagLib::ushort*>(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<const wchar_t*>(s), length / 2, t);
-}
 
-void String::copyFromUTF16(const char *s, size_t length, Type t)
-{
-  internalCopyFromUTF16<sizeof(wchar_t)>(s, length, t);
+#endif
 }
 
 const String::Type String::WCharByteOrder = isLittleEndianSystem ? String::UTF16LE : String::UTF16BE;
index 150d7c37a61765003f38f470aea6e562cb921cbe..7811ecf69aac52e20086f745bc18898dbc07de03 100644 (file)
@@ -474,7 +474,6 @@ namespace TagLib {
      */
     void copyFromUTF16(const char *s, size_t length, Type t);
     
-    template <size_t sizeOfWcharT>
     void internalCopyFromUTF16(const char *s, size_t length, Type t);
 
     /*!