mirror of https://github.com/axmolengine/axmol.git
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:
parent
f95279231e
commit
5902de93ce
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue