Skip to content
This repository has been archived by the owner on Dec 18, 2022. It is now read-only.

Commit

Permalink
Improve changes to ProjectSerializer.cpp
Browse files Browse the repository at this point in the history
Remove pointless addition of null-terminator bytes.
Replace `static_cast` usage with `memcpy` for aliasing reasons.

Signed-off-by: Emily Mabrey <emabrey@tenacityaudio.org>
Reference-to: https://github.com/tenacityteam/tenacity/pull/422
Helped-by: nyanpasu64
  • Loading branch information
emabrey committed Aug 4, 2021
1 parent b59753c commit 5c8f61a
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions src/ProjectSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <cstdint>
#include <mutex>
#include <wx/ustring.h>
#include <cstring>

///
/// ProjectSerializer class
Expand Down Expand Up @@ -389,7 +390,7 @@ wxString ProjectSerializer::Decode(const wxMemoryBuffer &buffer)

XMLStringWriter out;

std::vector<char> bytes;
std::vector<char> bytes_vector;
IdMap mIds;
std::vector<IdMap> mIdStack;
char mCharSize = 0;
Expand All @@ -407,30 +408,35 @@ wxString ProjectSerializer::Decode(const wxMemoryBuffer &buffer)
return iter->second;
};

auto ReadString = [&mCharSize, &in, &bytes](size_t len) -> wxString
auto ReadString = [&mCharSize, &in, &bytes_vector](size_t len) -> wxString
{
bytes.reserve( len + 4 );
auto data = bytes.data();
in.Read( data, len );
// Make a null terminator of the widest type
memset( data + len, '\0', 4 );
bytes_vector.reserve(len);
bytes_vector.resize(len);

//Read the input memory into the internal array of the vector
in.Read( bytes_vector.data(), len);

switch (mCharSize) {
case 1:
wxASSERT(sizeof(decltype(*data)) == sizeof(char));
// The void* silences the CodeQL CWE-704 detection
return wxUString().assignFromUTF8(static_cast<char*>(static_cast<void*>(data)), len);

{
wxASSERT(sizeof(decltype(*bytes_vector.data())) == sizeof(char));
// The void* silences the CodeQL CWE-704 detection
return wxUString().assignFromUTF8(bytes_vector.data(), len);
}
case 2:
wxASSERT(sizeof(wxChar16) == 2 * sizeof(char));
// The void* silences the CodeQL CWE-704 detection
return wxUString().assignFromUTF16(static_cast<wxChar16*>(static_cast<void*>(data)), len / 2);

{
wxASSERT(sizeof(wxChar16) == 2 * sizeof(char));
std::vector<wxChar16> converted_data_16(len / 2);
std::memcpy(converted_data_16.data(), bytes_vector.data(), len);
return wxUString().assignFromUTF16(converted_data_16.data(), converted_data_16.size());
}
case 4:
wxASSERT(sizeof(wxChar32) == 4 * sizeof(char));
// The void* silences the CodeQL CWE-704 detection
return wxUString().assign(static_cast<wxChar32*>(static_cast<void*>(data)), len / 4);

{
wxASSERT(sizeof(wxChar32) == 4 * sizeof(char));
std::vector<wxChar32> converted_data_32(len / 4);
std::memcpy(converted_data_32.data(), bytes_vector.data(), len);
return wxUString().assign(converted_data_32.data(), converted_data_32.size());
}
default:
wxASSERT_MSG(false, wxT("Characters size not 1, 2, or 4"));
return wxUString();
Expand Down

0 comments on commit 5c8f61a

Please sign in to comment.