From 8a768fe7db6ac5388fec6a68687817796b44439e Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 23 Jul 2024 15:35:46 -0700 Subject: [PATCH] QOffsetStringArray: fix off-by-one error for the past-the-end elements For legacy reasons, the accessor class allows operator[] to go out of bounds and just returns an empty string. However, we were returning a pointer to the one-past-end of the array, which appears to have been a null byte in the test. Instead of adding branching code to load the length of the first string in operator[], we can just add an extra null character at the position we were returning anyway. Pick-to: 6.8 6.5 Change-Id: I4878533dcb2d4b3e8efefffd17e4f876b43e9ee3 Reviewed-by: Ahmad Samir --- src/corelib/tools/qoffsetstringarray_p.h | 3 ++- .../tools/qoffsetstringarray/tst_qoffsetstringarray.cpp | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/corelib/tools/qoffsetstringarray_p.h b/src/corelib/tools/qoffsetstringarray_p.h index c3392789c7a..a4ef8a3e48f 100644 --- a/src/corelib/tools/qoffsetstringarray_p.h +++ b/src/corelib/tools/qoffsetstringarray_p.h @@ -96,7 +96,8 @@ template constexpr auto minifyValue() template constexpr auto makeStaticString(Extractor extract, const T &... entries) { - std::array result = {}; + // append an extra null terminator + std::array result = {}; qptrdiff offset = 0; const char *strings[] = { extract(entries).operator const char *()... }; diff --git a/tests/auto/corelib/tools/qoffsetstringarray/tst_qoffsetstringarray.cpp b/tests/auto/corelib/tools/qoffsetstringarray/tst_qoffsetstringarray.cpp index dbb24e7af45..3265a9d9979 100644 --- a/tests/auto/corelib/tools/qoffsetstringarray/tst_qoffsetstringarray.cpp +++ b/tests/auto/corelib/tools/qoffsetstringarray/tst_qoffsetstringarray.cpp @@ -65,16 +65,16 @@ constexpr const auto messagesBigOffsets = qOffsetStringArray( void tst_QOffsetStringArray::init() { - static_assert(messages.m_string.size() == 50); + static_assert(messages.m_string.size() == 51); static_assert(messages.m_offsets.size() == 6); static_assert(std::is_same_v); static_assert(messages257.m_offsets.size() == 258); - static_assert(messages257.m_string.size() == 260); + static_assert(messages257.m_string.size() == 261); static_assert(std::is_same_v); static_assert(messagesBigOffsets.m_offsets.size() == 5); - static_assert(messagesBigOffsets.m_string.size() == 364); + static_assert(messagesBigOffsets.m_string.size() == 365); static_assert(std::is_same_v); }