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 <a.samirh78@gmail.com>
This commit is contained in:
Thiago Macieira 2024-07-23 15:35:46 -07:00
parent 3fe4ba7194
commit 8a768fe7db
2 changed files with 5 additions and 4 deletions

View File

@ -96,7 +96,8 @@ template <size_t Highest> constexpr auto minifyValue()
template <size_t StringLength, typename Extractor, typename... T>
constexpr auto makeStaticString(Extractor extract, const T &... entries)
{
std::array<char, StringLength> result = {};
// append an extra null terminator
std::array<char, StringLength + 1> result = {};
qptrdiff offset = 0;
const char *strings[] = { extract(entries).operator const char *()... };

View File

@ -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<decltype(messages.m_offsets)::value_type, quint8>);
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<decltype(messages257.m_offsets)::value_type, quint16>);
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<decltype(messagesBigOffsets.m_offsets)::value_type, quint16>);
}