From 62899da626b663e679ec7128f29c15161c968b2d Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Tue, 4 Aug 2009 03:27:56 +0000 Subject: [PATCH] make CVolumeWAD::GetName() safer git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@3938 8ced0084-cf51-0410-be5f-012b33b47a6e --- Source/Core/DiscIO/Src/VolumeWad.cpp | 35 +++++++++++----------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/Source/Core/DiscIO/Src/VolumeWad.cpp b/Source/Core/DiscIO/Src/VolumeWad.cpp index 32caec2cb0..bc4c658b35 100644 --- a/Source/Core/DiscIO/Src/VolumeWad.cpp +++ b/Source/Core/DiscIO/Src/VolumeWad.cpp @@ -113,34 +113,25 @@ std::string CVolumeWAD::GetName() const return "Unknown"; // Offset to the english title - char temp[85]; + char temp[84]; if (!Read(0xF1 + OpeningBnrOffset, 84, (u8*)&temp) || Common::swap32(footer_size) < 0xF1) return "Unknown"; - char out_temp[43]; - int j = 0; - - for (int i=0; i < 84; i++) + // Remove the null bytes due to 16bit char length + std::string out_temp; + for (int i = 0; i < sizeof(temp); i+=2) { - if (!(i & 1)) - { - if (temp[i] != 0) - out_temp[j] = temp[i]; - else - { - // Some wads have a few consecutive Null chars followed by text - // as i don't know what to do there: replace the frst one with space - if (out_temp[j-1] != 32) - out_temp[j] = 32; - else - continue; - } - - j++; + // Replace null chars with a single space per null section + if (temp[i] == '\0' && i > 0) + { + if (out_temp.at(out_temp.size()-1) != ' ') + out_temp.push_back(' '); } + else + out_temp.push_back(temp[i]); } - - out_temp[j] = 0; + // Make it a null terminated string + out_temp.replace(out_temp.end()-1, out_temp.end(), 1, '\0'); return out_temp; }