Merge pull request #5174 from feuerste/fix_magic

Fix violation of strict aliasing rule in `BaseImporter::CheckMagicToken`.
pull/5199/head^2
Kim Kulling 2023-08-12 08:04:34 +02:00 committed by GitHub
commit 461601e9ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 12 deletions

View File

@ -312,12 +312,7 @@ std::string BaseImporter::GetExtension(const std::string &pFile) {
if (!pIOHandler) { if (!pIOHandler) {
return false; return false;
} }
union { const char *magic = reinterpret_cast<const char *>(_magic);
const char *magic;
const uint16_t *magic_u16;
const uint32_t *magic_u32;
};
magic = reinterpret_cast<const char *>(_magic);
std::unique_ptr<IOStream> pStream(pIOHandler->Open(pFile)); std::unique_ptr<IOStream> pStream(pIOHandler->Open(pFile));
if (pStream) { if (pStream) {
@ -339,15 +334,15 @@ std::string BaseImporter::GetExtension(const std::string &pFile) {
// that's just for convenience, the chance that we cause conflicts // that's just for convenience, the chance that we cause conflicts
// is quite low and it can save some lines and prevent nasty bugs // is quite low and it can save some lines and prevent nasty bugs
if (2 == size) { if (2 == size) {
uint16_t rev = *magic_u16; uint16_t magic_u16;
ByteSwap::Swap(&rev); memcpy(&magic_u16, magic, 2);
if (data_u16[0] == *magic_u16 || data_u16[0] == rev) { if (data_u16[0] == magic_u16 || data_u16[0] == ByteSwap::Swapped(magic_u16)) {
return true; return true;
} }
} else if (4 == size) { } else if (4 == size) {
uint32_t rev = *magic_u32; uint32_t magic_u32;
ByteSwap::Swap(&rev); memcpy(&magic_u32, magic, 4);
if (data_u32[0] == *magic_u32 || data_u32[0] == rev) { if (data_u32[0] == magic_u32 || data_u32[0] == ByteSwap::Swapped(magic_u32)) {
return true; return true;
} }
} else { } else {