Skip to content
Open
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
26 changes: 26 additions & 0 deletions src/pugixml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2154,6 +2154,19 @@ PUGI_IMPL_NS_BEGIN

template <typename D> PUGI_IMPL_FN bool convert_buffer_generic(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, D)
{
// copy to aligned buffer if input is misaligned for the source type
auto_deleter<void> aligned_guard(NULL, xml_memory::deallocate);

if (reinterpret_cast<uintptr_t>(contents) % sizeof(typename D::type) != 0)
{
void* aligned = xml_memory::allocate(size);
if (!aligned) return false;

memcpy(aligned, contents, size);
contents = aligned;
aligned_guard.data = aligned;
}

const typename D::type* data = static_cast<const typename D::type*>(contents);
size_t data_length = size / sizeof(typename D::type);

Expand Down Expand Up @@ -2224,6 +2237,19 @@ PUGI_IMPL_NS_BEGIN
#else
template <typename D> PUGI_IMPL_FN bool convert_buffer_generic(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, D)
{
// copy to aligned buffer if input is misaligned for the source type
auto_deleter<void> aligned_guard(NULL, xml_memory::deallocate);

if (reinterpret_cast<uintptr_t>(contents) % sizeof(typename D::type) != 0)
{
void* aligned = xml_memory::allocate(size);
if (!aligned) return false;

memcpy(aligned, contents, size);
contents = aligned;
aligned_guard.data = aligned;
}

const typename D::type* data = static_cast<const typename D::type*>(contents);
size_t data_length = size / sizeof(typename D::type);

Expand Down
Loading