Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid misaligned read when loading SGM files #5692

Merged
merged 2 commits into from
Jan 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/scenegraph/Serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "Color.h"
#include "Quaternion.h"
#include "vector3.h"
#include <cstring>
#include <stdexcept>
#include <string>

Expand Down Expand Up @@ -119,12 +120,15 @@ namespace Serializer {
public:
Reader() :
m_at(nullptr),
m_streamVersion(-1) {}
m_streamVersion(-1)
{
}

explicit Reader(const ByteRange &data) :
m_data(data),
m_at(data.begin)
{}
{
}

bool AtEnd() { return m_at != m_data.end; }

Expand All @@ -149,7 +153,7 @@ namespace Serializer {
throw std::out_of_range("Serializer::Reader encountered truncated stream.");
#endif

out = *reinterpret_cast<const T *>(m_at);
std::memcpy(&out, m_at, sizeof(T)); // use memcpy to handle unaligned reads
m_at += sizeof(T);
}

Expand Down