include/o3tl/BigEndianTypes.hxx | 24 +++++++++--------------- o3tl/qa/BigEndianTypesTest.cxx | 1 + 2 files changed, 10 insertions(+), 15 deletions(-)
New commits: commit fd8b5190236281554525eec757d506a14c763efd Author: Stephan Bergmann <stephan.bergm...@allotropia.de> AuthorDate: Wed Apr 30 14:14:14 2025 +0200 Commit: Stephan Bergmann <stephan.bergm...@allotropia.de> CommitDate: Wed Apr 30 17:00:24 2025 +0200 Avoid misaligned-pointer-use ...after d0ee08cfbf12145027eee7ad46448a8734693c06 "oox: export embedded font used in a presentation document" caused CppunitTest_vcl_font_ttf_structure_test to fail with > vcl/qa/cppunit/font/TTFStructureTest.cxx:47:5: runtime error: member call on misaligned address 0x7de9211e0262 for type 'o3tl::sal_uInt32_BE', which requires 4 byte alignment > 0x7de9211e0262: note: pointer points here > 00 00 00 00 80 00 00 af 10 00 20 48 00 00 00 00 00 00 00 00 57 33 43 00 00 40 00 20 f0 02 03 20 > ^ > #0 in (anonymous namespace)::testReadTTFStructure::TestBody() at vcl/qa/cppunit/font/TTFStructureTest.cxx:47:5 Change-Id: I4d46decd497fccf7eecdb38764ac82ed2a68b5d6 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/184829 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <stephan.bergm...@allotropia.de> diff --git a/include/o3tl/BigEndianTypes.hxx b/include/o3tl/BigEndianTypes.hxx index 5b637c4a5d62..21b085d960f5 100644 --- a/include/o3tl/BigEndianTypes.hxx +++ b/include/o3tl/BigEndianTypes.hxx @@ -11,49 +11,43 @@ #include <sal/config.h> #include <sal/types.h> -#include <osl/endian.h> namespace o3tl { -/** 16-bit unsigned integer type that can be used in a struct to read from data that is big endian. +/** 16-bit unsigned integer type that can be used in a struct to read from data that is big endian + and potentially misaligned. * * Type can't be instantiated but only used in a struct which is reinterpret_cast from bytes. */ class sal_uInt16_BE { private: - sal_uInt16 mnValue; + sal_uInt8 mnValue[2]; sal_uInt16_BE() = delete; public: constexpr operator sal_uInt16() const { -#ifdef OSL_LITENDIAN - return OSL_SWAPWORD(mnValue); -#else - return mnValue; -#endif + return sal_uInt32(mnValue[1]) | (sal_uInt32(mnValue[0]) << 8); } }; -/** 32-bit unsigned integer type that can be used in a struct to read from data that is big endian. +/** 32-bit unsigned integer type that can be used in a struct to read from data that is big endian + and potentially misaligned. * * Type can't be instantiated but only used in a struct which is reinterpret_cast from bytes. */ class sal_uInt32_BE { private: - sal_uInt32 mnValue; + sal_uInt8 mnValue[4]; sal_uInt32_BE() = delete; public: constexpr operator sal_uInt32() const { -#ifdef OSL_LITENDIAN - return OSL_SWAPDWORD(mnValue); -#else - return mnValue; -#endif + return sal_uInt32(mnValue[3]) | (sal_uInt32(mnValue[2]) << 8) + | (sal_uInt32(mnValue[1]) << 16) | (sal_uInt32(mnValue[0]) << 24); } }; diff --git a/o3tl/qa/BigEndianTypesTest.cxx b/o3tl/qa/BigEndianTypesTest.cxx index 055be80705f5..dccb0c172344 100644 --- a/o3tl/qa/BigEndianTypesTest.cxx +++ b/o3tl/qa/BigEndianTypesTest.cxx @@ -13,6 +13,7 @@ #include <cppunit/extensions/HelperMacros.h> #include <o3tl/BigEndianTypes.hxx> +#include <osl/endian.h> #include <array> namespace