QMessagePattern: add Q_ASSERT to setDefaultPattern() and improve codegen

Amends commit e04f109456bbcb184963b8ed71944b958b35c201.

This function does not free the pointers stored in literals, so add a
couple of assertions to indicate that it's only safe to call from the
constructor. And that being the case, we can update the tokens
unique_ptr in two steps, which makes the compiler not attempt to call
delete[] on the previous pointer.

Pick-to: 6.9
Change-Id: Ibe76a7df795e9c6d6191fffdf45e3b3ef6f0f344
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
This commit is contained in:
Thiago Macieira 2025-01-07 23:36:17 -03:00
parent c9ca74660b
commit b86bcf5c25

View File

@ -1136,9 +1136,15 @@ struct QMessagePattern
messageTokenC, messageTokenC,
}; };
// std::make_unique() value-initializes, so we have a nullptr at the end // we don't attempt to free the pointers, so only call from the ctor
tokens = std::make_unique<const char *[]>(std::size(defaultTokens) + 1); Q_ASSERT(!tokens);
std::copy(std::begin(defaultTokens), std::end(defaultTokens), tokens.get()); Q_ASSERT(!literals);
auto ptr = new const char *[std::size(defaultTokens) + 1];
auto end = std::copy(std::begin(defaultTokens), std::end(defaultTokens), ptr);
*end = nullptr;
tokens.release();
tokens.reset(ptr);
} }
// 0 terminated arrays of literal tokens / literal or placeholder tokens // 0 terminated arrays of literal tokens / literal or placeholder tokens