Modified: trunk/Source/WTF/ChangeLog (160459 => 160460)
--- trunk/Source/WTF/ChangeLog 2013-12-11 22:47:07 UTC (rev 160459)
+++ trunk/Source/WTF/ChangeLog 2013-12-11 23:00:08 UTC (rev 160460)
@@ -1,3 +1,15 @@
+2013-12-11 Brendan Long <[email protected]>
+
+ Type punning error in MD5.cpp
+ https://bugs.webkit.org/show_bug.cgi?id=125412
+
+ Reviewed by Darin Adler.
+
+ * wtf/MD5.cpp:
+ (WTF::toLittleEndian): Renamed, and use memcpy instead of casting.
+ (WTF::MD5::addBytes): Renamed reverseBytes to toLittleEndian.
+ (WTF::MD5::checksum): Same, and use memcpy instead of casting to prevent type punning error.
+
2013-12-11 Laszlo Vidacs <[email protected]>
Store SHA1 hash in std::array
Modified: trunk/Source/WTF/wtf/MD5.cpp (160459 => 160460)
--- trunk/Source/WTF/wtf/MD5.cpp 2013-12-11 22:47:07 UTC (rev 160459)
+++ trunk/Source/WTF/wtf/MD5.cpp 2013-12-11 23:00:08 UTC (rev 160460)
@@ -60,14 +60,14 @@
// Note: this code is harmless on little-endian machines.
-static void reverseBytes(uint8_t* buf, unsigned longs)
+static void toLittleEndian(uint8_t* buf, unsigned longs)
{
ASSERT(longs > 0);
do {
uint32_t t = static_cast<uint32_t>(buf[3] << 8 | buf[2]) << 16 | buf[1] << 8 | buf[0];
ASSERT_WITH_MESSAGE(!(reinterpret_cast<uintptr_t>(buf) % sizeof(t)), "alignment error of buf");
- *reinterpret_cast_ptr<uint32_t *>(buf) = t;
- buf += 4;
+ memcpy(buf, &t, sizeof(t));
+ buf += sizeof(t);
} while (--longs);
}
@@ -199,7 +199,7 @@
return;
}
memcpy(p, buf, t);
- reverseBytes(m_in, 16);
+ toLittleEndian(m_in, 16);
MD5Transform(m_buf, reinterpret_cast_ptr<uint32_t*>(m_in)); // m_in is 4-byte aligned.
buf += t;
length -= t;
@@ -209,7 +209,7 @@
while (length >= 64) {
memcpy(m_in, buf, 64);
- reverseBytes(m_in, 16);
+ toLittleEndian(m_in, 16);
MD5Transform(m_buf, reinterpret_cast_ptr<uint32_t*>(m_in)); // m_in is 4-byte aligned.
buf += 64;
length -= 64;
@@ -236,7 +236,7 @@
if (count < 8) {
// Two lots of padding: Pad the first block to 64 bytes
memset(p, 0, count);
- reverseBytes(m_in, 16);
+ toLittleEndian(m_in, 16);
MD5Transform(m_buf, reinterpret_cast_ptr<uint32_t *>(m_in)); // m_in is 4-byte aligned.
// Now fill the next block with 56 bytes
@@ -245,15 +245,13 @@
// Pad block to 56 bytes
memset(p, 0, count - 8);
}
- reverseBytes(m_in, 14);
+ toLittleEndian(m_in, 14);
// Append length in bits and transform
- // m_in is 4-byte aligned.
- (reinterpret_cast_ptr<uint32_t*>(m_in))[14] = m_bits[0];
- (reinterpret_cast_ptr<uint32_t*>(m_in))[15] = m_bits[1];
+ memcpy(m_in + 56, m_bits, sizeof(m_bits));
MD5Transform(m_buf, reinterpret_cast_ptr<uint32_t*>(m_in));
- reverseBytes(reinterpret_cast<uint8_t*>(m_buf), 4);
+ toLittleEndian(reinterpret_cast<uint8_t*>(m_buf), 4);
// Now, m_buf contains checksum result.
uint8_t* mBufUInt8 = reinterpret_cast<uint8_t*>(m_buf);