Committing a fix for a potential unaligned memory access crash in CCBReader::readFloat(),

caused by incorrect compiler optimisations/assumptions surrounding the use of memcpy().
This commit is contained in:
Darragh Coy 2013-02-06 14:44:15 -08:00
parent f95279231e
commit 5902de93ce
1 changed files with 15 additions and 2 deletions

View File

@ -515,8 +515,21 @@ float CCBReader::readFloat() {
* TODO still applies in C++ ? */
float * pF = (float*)(this->mBytes + this->mCurrentByte);
float f = 0;
memcpy(&f, pF, sizeof(float));
this->mCurrentByte += 4;
// N.B - in order to avoid an unaligned memory access crash on 'memcpy()' the the (void*) casts of the source and
// destination pointers are EXTREMELY important for the ARM compiler.
//
// Without a (void*) cast, the ARM compiler makes the assumption that the float* pointer is naturally aligned
// according to it's type size (aligned along 4 byte boundaries) and thus tries to call a more optimized
// version of memcpy() which makes this alignment assumption also. When reading back from a file of course our pointers
// may not be aligned, hence we need to avoid the compiler making this assumption. The (void*) cast serves this purpose,
// and causes the ARM compiler to choose the slower, more generalized (unaligned) version of memcpy()
//
// For more about this compiler behavior, see:
// http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka3934.html
memcpy((void*) &f, (const void*) pF, sizeof(float));
this->mCurrentByte += sizeof(float);
return f;
}
}