mirror of
https://github.com/cookiengineer/audacity
synced 2025-06-15 07:40:23 +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:
parent
b59753cf2e
commit
5c8f61af86
@ -20,6 +20,7 @@
|
||||
#include <cstdint>
|
||||
#include <mutex>
|
||||
#include <wx/ustring.h>
|
||||
#include <cstring>
|
||||
|
||||
///
|
||||
/// ProjectSerializer class
|
||||
@ -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;
|
||||
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user