diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2021-09-30 12:35:25 +0200 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2021-10-01 19:24:31 +0200 |
commit | 427be739ff63eac32cae4f44260f52ccece14be7 (patch) | |
tree | bee8b968386ffeeb8f6ab065aa93b9a107f64e01 /src/testlib | |
parent | b88095e644d0845e4532edf0ccdbca3a33a68c08 (diff) |
Tidy up QTest::qt_asprintf() and remove a spurious declaration
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 <thiago.macieira@intel.com>
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Diffstat (limited to 'src/testlib')
-rw-r--r-- | src/testlib/qabstracttestlogger.cpp | 29 |
1 files 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; } |