From 427be739ff63eac32cae4f44260f52ccece14be7 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 30 Sep 2021 12:35:25 +0200 Subject: [PATCH] Tidy up QTest::qt_asprintf() and remove a spurious declaration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The extern declaration for filter_unprintable() was nowhere used and no such function is anywhere defined. The for (;;) loop with lots of reasons to break; was better structured as a do {...} while loop. A comment on -1 as return from qvsprintf() was misplaced. The '\0'-termination after calling qvsprintf() was redundant as vsprintf() reliably '\0'-terminates anyway. Turned a static const into a constexpr. Assert size (a QTestCharBuffer necessarily has size >= 512). Change-Id: I5b7729b9bd66fea0ee7ce3e7cfdde6770f10b36c Reviewed-by: Thiago Macieira Reviewed-by: Tor Arne Vestbø --- src/testlib/qabstracttestlogger.cpp | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/src/testlib/qabstracttestlogger.cpp b/src/testlib/qabstracttestlogger.cpp index 9bd91cf20f..ce9e0c593a 100644 --- a/src/testlib/qabstracttestlogger.cpp +++ b/src/testlib/qabstracttestlogger.cpp @@ -398,41 +398,34 @@ void QAbstractTestLogger::addMessage(QtMsgType type, const QMessageLogContext &c namespace QTest { -extern void filter_unprintable(char *str); - /*! \fn int QTest::qt_asprintf(QTestCharBuffer *buf, const char *format, ...); \internal */ int qt_asprintf(QTestCharBuffer *str, const char *format, ...) { - static const int MAXSIZE = 1024*1024*2; - + constexpr int MAXSIZE = 1024 * 1024 * 2; Q_ASSERT(str); - int size = str->size(); + Q_ASSERT(size > 0); va_list ap; int res = 0; - for (;;) { + do { va_start(ap, format); res = qvsnprintf(str->data(), size, format, ap); va_end(ap); - str->data()[size - 1] = '\0'; - if (res >= 0 && res < size) { - // We succeeded - break; - } - // buffer wasn't big enough, try again. + // vsnprintf() reliably '\0'-terminates + Q_ASSERT(res < 0 || str->data()[res < size ? res : size - 1] == '\0'); // Note, we're assuming that a result of -1 is always due to running out of space. - size *= 2; - if (size > MAXSIZE) { + if (res >= 0 && res < size) // Success break; - } - if (!str->reset(size)) - break; // out of memory - take what we have - } + + // Buffer wasn't big enough, try again: + size *= 2; + // If too large or out of memory, take what we have: + } while (size <= MAXSIZE && str->reset(size)); return res; }