Fix for undefined behavior when loading binary PLY

This commit fix undefined behavior reported by UBSAN when loading a binary
PLY file.
pull/1825/head
Alexandre Avenel 2018-03-04 21:41:19 +01:00
parent bd80e92f78
commit d2547e84f5
1 changed files with 44 additions and 24 deletions

View File

@ -1043,71 +1043,91 @@ bool PLY::PropertyInstance::ParseValueBinary(IOStreamBuffer<char> &streamBuffer,
switch (eType) switch (eType)
{ {
case EDT_UInt: case EDT_UInt:
out->iUInt = (uint32_t)*((uint32_t*)pCur); {
pCur += 4; uint32_t t;
memcpy(&t, pCur, sizeof(uint32_t));
pCur += sizeof(uint32_t);
// Swap endianness // Swap endianness
if (p_bBE)ByteSwap::Swap((int32_t*)&out->iUInt); if (p_bBE)ByteSwap::Swap(&t);
out->iUInt = t;
break; break;
}
case EDT_UShort: case EDT_UShort:
{ {
uint16_t i = *((uint16_t*)pCur); uint16_t t;
memcpy(&t, pCur, sizeof(uint16_t));
pCur += sizeof(uint16_t);
// Swap endianness // Swap endianness
if (p_bBE)ByteSwap::Swap(&i); if (p_bBE)ByteSwap::Swap(&t);
out->iUInt = (uint32_t)i; out->iUInt = t;
pCur += 2;
break; break;
} }
case EDT_UChar: case EDT_UChar:
{ {
out->iUInt = (uint32_t)(*((uint8_t*)pCur)); uint8_t t;
pCur++; memcpy(&t, pCur, sizeof(uint8_t));
pCur += sizeof(uint8_t);
out->iUInt = t;
break; break;
} }
case EDT_Int: case EDT_Int:
out->iInt = *((int32_t*)pCur); {
pCur += 4; int32_t t;
memcpy(&t, pCur, sizeof(int32_t));
pCur += sizeof(int32_t);
// Swap endianness // Swap endianness
if (p_bBE)ByteSwap::Swap(&out->iInt); if (p_bBE)ByteSwap::Swap(&t);
out->iInt = t;
break; break;
}
case EDT_Short: case EDT_Short:
{ {
int16_t i = *((int16_t*)pCur); int16_t t;
memcpy(&t, pCur, sizeof(int16_t));
pCur += sizeof(int16_t);
// Swap endianness // Swap endianness
if (p_bBE)ByteSwap::Swap(&i); if (p_bBE)ByteSwap::Swap(&t);
out->iInt = (int32_t)i; out->iInt = t;
pCur += 2;
break; break;
} }
case EDT_Char: case EDT_Char:
out->iInt = (int32_t)*((int8_t*)pCur); {
pCur++; int8_t t;
memcpy(&t, pCur, sizeof(int8_t));
pCur += sizeof(int8_t);
out->iInt = t;
break; break;
}
case EDT_Float: case EDT_Float:
{ {
out->fFloat = *((float*)pCur); float t;
memcpy(&t, pCur, sizeof(float));
pCur += sizeof(float);
// Swap endianness // Swap endianness
if (p_bBE)ByteSwap::Swap((int32_t*)&out->fFloat); if (p_bBE)ByteSwap::Swap(&t);
pCur += 4; out->fFloat = t;
break; break;
} }
case EDT_Double: case EDT_Double:
{ {
out->fDouble = *((double*)pCur); double t;
memcpy(&t, pCur, sizeof(double));
pCur += sizeof(double);
// Swap endianness // Swap endianness
if (p_bBE)ByteSwap::Swap((int64_t*)&out->fDouble); if (p_bBE)ByteSwap::Swap(&t);
pCur += 8; out->fDouble = t;
break; break;
} }
default: default: