1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-15 15:49:36 +02:00

Improve changes to ProjectSerializer.cpp

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
This commit is contained in:
Emily Mabrey 2021-08-04 17:05:58 -04:00 committed by GitHub
parent b59753cf2e
commit 5c8f61af86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,6 +20,7 @@
#include <cstdint> #include <cstdint>
#include <mutex> #include <mutex>
#include <wx/ustring.h> #include <wx/ustring.h>
#include <cstring>
/// ///
/// ProjectSerializer class /// ProjectSerializer class
@ -389,7 +390,7 @@ wxString ProjectSerializer::Decode(const wxMemoryBuffer &buffer)
XMLStringWriter out; XMLStringWriter out;
std::vector<char> bytes; std::vector<char> bytes_vector;
IdMap mIds; IdMap mIds;
std::vector<IdMap> mIdStack; std::vector<IdMap> mIdStack;
char mCharSize = 0; char mCharSize = 0;
@ -407,30 +408,35 @@ wxString ProjectSerializer::Decode(const wxMemoryBuffer &buffer)
return iter->second; 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 ); bytes_vector.reserve(len);
auto data = bytes.data(); bytes_vector.resize(len);
in.Read( data, len );
// Make a null terminator of the widest type //Read the input memory into the internal array of the vector
memset( data + len, '\0', 4 ); in.Read( bytes_vector.data(), len);
switch (mCharSize) { switch (mCharSize) {
case 1: case 1:
wxASSERT(sizeof(decltype(*data)) == sizeof(char)); {
// The void* silences the CodeQL CWE-704 detection wxASSERT(sizeof(decltype(*bytes_vector.data())) == sizeof(char));
return wxUString().assignFromUTF8(static_cast<char*>(static_cast<void*>(data)), len); // The void* silences the CodeQL CWE-704 detection
return wxUString().assignFromUTF8(bytes_vector.data(), len);
}
case 2: case 2:
wxASSERT(sizeof(wxChar16) == 2 * sizeof(char)); {
// The void* silences the CodeQL CWE-704 detection wxASSERT(sizeof(wxChar16) == 2 * sizeof(char));
return wxUString().assignFromUTF16(static_cast<wxChar16*>(static_cast<void*>(data)), len / 2); 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: case 4:
wxASSERT(sizeof(wxChar32) == 4 * sizeof(char)); {
// The void* silences the CodeQL CWE-704 detection wxASSERT(sizeof(wxChar32) == 4 * sizeof(char));
return wxUString().assign(static_cast<wxChar32*>(static_cast<void*>(data)), len / 4); 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: default:
wxASSERT_MSG(false, wxT("Characters size not 1, 2, or 4")); wxASSERT_MSG(false, wxT("Characters size not 1, 2, or 4"));
return wxUString(); return wxUString();