0
0
mirror of https://github.com/zeux/pugixml.git synced 2024-12-26 12:41:06 +08:00

ubsan: Fix undefined behavior for signed left shift in compact mode

We were using << compact_alignment_log2 instead of * compact_alignment
for symmetry with the encoding where >> is crucial to keep code fast and
round to negative infinity.

For decoding, the results are the same and any reasonable compiler
should convert *4 into <<2 so just use a multiplication - that doesn't
trigger UB on negative numbers.
This commit is contained in:
Arseny Kapoulkine 2018-03-16 21:25:12 -07:00
parent 9540016f6d
commit e50672cf37

View File

@ -852,7 +852,7 @@ PUGI__NS_BEGIN
{
uintptr_t base = reinterpret_cast<uintptr_t>(this) & ~(compact_alignment - 1);
return reinterpret_cast<T*>(base + ((_data - 1 + start) << compact_alignment_log2));
return reinterpret_cast<T*>(base + (_data - 1 + start) * compact_alignment);
}
else
return compact_get_value<header_offset, T>(this);
@ -930,7 +930,7 @@ PUGI__NS_BEGIN
{
uintptr_t base = reinterpret_cast<uintptr_t>(this) & ~(compact_alignment - 1);
return reinterpret_cast<T*>(base + ((_data - 1 - 65533) << compact_alignment_log2));
return reinterpret_cast<T*>(base + (_data - 1 - 65533) * compact_alignment);
}
else if (_data == 65534)
return static_cast<T*>(compact_get_page(this, header_offset)->compact_shared_parent);