From b9f7add531ceab2590e2b24dec7b495950f9870a Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 30 Sep 2021 12:50:05 +0200 Subject: Verify returns from QXmlTestLogger's xmlQuote() and xmlCdata() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Using partially-converted text would lead to invalid XML, so don't use the buffer contents if the return is zero. As a result, QTestJUnitStreamer::formatEnd() needs to return some indication of whether *it* succeeded, so change it to forward their int returns; and, as it's in fact only used internally by the streamer, make it private. Make these functions [[nodiscard]] so that further uses of them will be discouraged from ignoring the possibility of failure. Make the public versions return bool so that they can correctly succeed on empty input. Assert various conditions we can infer to save work we don't need to do. Change-Id: I899bad23d1dfbd05fc725de269def4ce213dbc5a Reviewed-by: Tor Arne Vestbø --- src/testlib/qtestjunitstreamer.cpp | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'src/testlib/qtestjunitstreamer.cpp') diff --git a/src/testlib/qtestjunitstreamer.cpp b/src/testlib/qtestjunitstreamer.cpp index 72ab7d1534..3e67e5c4bf 100644 --- a/src/testlib/qtestjunitstreamer.cpp +++ b/src/testlib/qtestjunitstreamer.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtTest module of the Qt Toolkit. @@ -106,23 +106,26 @@ void QTestJUnitStreamer::formatEnd(const QTestElement *element, QTestCharBuffer QTest::qt_asprintf(formatted, "%s\n", indent, element->elementName()); } -void QTestJUnitStreamer::formatAttributes(const QTestElement* element, const QTestElementAttribute *attribute, QTestCharBuffer *formatted) const +bool QTestJUnitStreamer::formatAttributes(const QTestElement* element, + const QTestElementAttribute *attribute, + QTestCharBuffer *formatted) const { if (!attribute || !formatted ) - return; + return false; QTest::AttributeIndex attrindex = attribute->index(); if (element && element->elementType() == QTest::LET_Text) { QTEST_ASSERT(attrindex == QTest::AI_Value); - QXmlTestLogger::xmlCdata(formatted, attribute->value()); - return; + return QXmlTestLogger::xmlCdata(formatted, attribute->value()); } QTestCharBuffer quotedValue; - QXmlTestLogger::xmlQuote("edValue, attribute->value()); - QTest::qt_asprintf(formatted, " %s=\"%s\"", - attribute->name(), quotedValue.constData()); + if (QXmlTestLogger::xmlQuote("edValue, attribute->value())) { + return QTest::qt_asprintf(formatted, " %s=\"%s\"", + attribute->name(), quotedValue.constData()) != 0; + } + return false; } void QTestJUnitStreamer::formatAfterAttributes(const QTestElement *element, QTestCharBuffer *formatted) const @@ -176,8 +179,8 @@ void QTestJUnitStreamer::outputElementAttributes(const QTestElement* element, co QTestCharBuffer buf; for (auto *attribute : attributes) { - formatAttributes(element, attribute, &buf); - outputString(buf.data()); + if (formatAttributes(element, attribute, &buf)) + outputString(buf.data()); } } -- cgit v1.2.3