From b86bcf5c25d0072300a8b0a4b5cfe942cabf53ad Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 7 Jan 2025 23:36:17 -0300 Subject: [PATCH] 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 --- src/corelib/global/qlogging.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/corelib/global/qlogging.cpp b/src/corelib/global/qlogging.cpp index 699cd9911de..7b1a1f1db49 100644 --- a/src/corelib/global/qlogging.cpp +++ b/src/corelib/global/qlogging.cpp @@ -1136,9 +1136,15 @@ struct QMessagePattern messageTokenC, }; - // std::make_unique() value-initializes, so we have a nullptr at the end - tokens = std::make_unique(std::size(defaultTokens) + 1); - std::copy(std::begin(defaultTokens), std::end(defaultTokens), tokens.get()); + // we don't attempt to free the pointers, so only call from the ctor + Q_ASSERT(!tokens); + 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