summaryrefslogtreecommitdiffstats
path: root/src/corelib/serialization/qxmlstream.cpp
Commit message (Collapse)AuthorAgeFilesLines
* Doc: Qt Core: Fix documentation linking issuesTopi Reinio6 days1-3/+3
| | | | | | | | | | | | | | | | | | | | Fixes the following QDoc warnings: * (qdoc) warning: Can't link to 'qt_add_android_permission()' * (qdoc) warning: Can't link to 'raiseError(const QString &message)' -> raiseError() now takes QAnyStringView * (qdoc) warning: Can't link to 'I18N Example' * (qdoc) warning: Can't link to 'Hello tr() Example' -> Examples were removed, replace with 'Localized Clock' example * (qdoc) warning: Can't link to 'QCharConverter::FinalizeResult::Error::NoError' Pick-to: 6.11 Change-Id: I8e11a8896dd10652852e81c5d7ddf080f69aba37 Reviewed-by: Topi Reinio <topi.reinio@qt.io>
* QUtf8Functions: wrap fromUtf8() in a simpler APIMarc Mutz2025-08-271-5/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Coverity had an issue with how we called this function to get the next character from a UTF-8 sequence. In particular, it complained about passing the address of a char32_t object to fromUtf8(), which "treats it as an array", as Coverity notes. The C++ standard says this is fine¹, but callers of this function, specifically, are almost by definition security-critical, so we shouldn't leave Coverity issues unfixed in them. The fix is to use an array of one element instead: char32_t[1]. We have already applied this fix in qstring.cpp (4eb9e0d3eedfc1b6de968308167af01b19f6ffe7) and qurlrecode.cpp (7a32a2238f52217bc4f0dc4c9620a2a2d350a1ca), but there are more cases in QXmlStream and QStringConverter, some of which this patch fixes. In order to simplify the task, package up the fix in a small function for easier reuse and so that the proverbial maintenance programmer is less likely to undo these fixes again. Incidentally, this function makes those callers that can use it much more readable than before. Many other calls (incl. the one in qurlrecode.cpp and some others in qstringconverter.cpp) cannot be ported over, as they are using the return value of fromUtf8(), or have already advanced the input pointer themselves when they call fromUtf8(). Amends 94c62e322264e2e7d61193ae74ba8556a330385c, ea0a08c898fed9cfd8d8eb16613e352740d3eb02 and b977ae371a753a82e1d0bb32c5b62099da663721. ¹ https://eel.is/c++draft/basic.compound#3.sentence-11 Pick-to: 6.10 6.9 6.8 Coverity-Id: 378348 Coverity-Id: 403740 Coverity-Id: 403748 Coverity-Id: 459945 Change-Id: I4957ea2ee7b6e0e9c361eb2bcd4351708762a6e9 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Doc: Remove spurious charactersDavid Boddie2025-08-201-1/+0
| | | | | | Pick-to: 6.10 Change-Id: Ibfd907bf8ea80063cb896d2ba36cfd2a2ebfa241 Reviewed-by: Paul Wicking <paul.wicking@qt.io>
* QXmlStream: don't use Structured BindingsMarc Mutz2025-08-201-4/+4
| | | | | | | | | | | | | | | | | | | | | Decoder::operator() already returns a proper struct with named members, so decomposing that using SB can do no good, except introducing a dependency on the order of the struct, which is counter-productive. Fix by receiving the result in a proper variable and using the struct's member names. Amends 53622aca2ad0d13bd16d8307dc28f915c8878b75. The idea may have been to avoid churning the surrounding code, but follow-up c08766abf2267f4eeda159c5688309d22d319722 touched all lines using the result, anyway, so we don't need SB anymore to keep the old names. Pick-to: 6.10 Change-Id: I2890754c2c2189cc2fa395f4500ca6c3cb115d7d Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
* QXmlStreamWriter: replace Q_ASSERT() with static_assert()Marc Mutz2025-08-191-1/+1
| | | | | | | | | | | The condition is a compile-time constant expression, so we can check it at compile-time. No run-time check needed. Amends 53622aca2ad0d13bd16d8307dc28f915c8878b75. Pick-to: 6.10 Change-Id: Ia73dd99c3670a279ec7f740eecd623d51ae647d4 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QXmlStream: silence clang.exe warningTim Blechmann2025-08-061-1/+1
| | | | | | | | StreamEOF is an enum, but compared against uint instances, emitting signed/unsigned comparison warnings when building with clang.exe using the MSVC ABI. Using a constexpr variable of the correct type to fix the warning. Pick-to: 6.10 Change-Id: I1fd29e0bcc46fc8a75c7024605ba68f68df82af4 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QXmlStreamWriter: use QASV in raiseError()Ivan Solovev2025-07-281-4/+4
| | | | | | | | | | | Amends 53622aca2ad0d13bd16d8307dc28f915c8878b75. Found in 6.10 API review. Pick-to: 6.10 Change-Id: Ib53d4306f6345befd39d1273ae44997b41163b78 Reviewed-by: Marc Mutz <marc.mutz@qt.io> Reviewed-by: Magdalena Stojek <magdalena.stojek@qt.io>
* QXmlStream: fix incorrect use of realloc()Marc Mutz2025-07-011-4/+6
| | | | | | | | | | | | | | | | | One must never store the result of realloc() back into the first realloc() argument, because, if relloc() fails, one still has to free() the old pointer, but one has just overwritten its value with nullptr, leaking memory. To fix, first check, and then assign. Found in manual review of realloc() uses. Amends the start of the public history. Pick-to: 6.10 6.9 6.8 6.5 Change-Id: Id6d369116d4e01fb9cb1d6db3627b9956d4b6201 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QXmlStreamReader: fix a missing std::move()Marc Mutz2025-07-011-1/+1
| | | | | | | | | | | | | | | | Coverity (rightfully) complains that the assignment uses the copy- and not the move-assignment operator. Fix by adding the missing move(). Amends 927798f5deb0bd95fc8ce5be9fb0cfceef94a46b. Coverity-Id: 481486 Pick-to: 6.10 Change-Id: I9044847abb88adaaa55f49e7578290d11ddb8655 Reviewed-by: Magdalena Stojek <magdalena.stojek@qt.io> Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
* QXmlStreamWriter: Clear errorString when resetting error codeMagdalena Stojek2025-06-161-0/+1
| | | | | | | | | | | | This change ensures that errorString is explicitly cleared when resetting the error code to NoError. Resulted from API review. Pick-to: 6.10 Change-Id: I182575df7a058045ea58424b64063cd6026fb21d Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> Reviewed-by: Marc Mutz <marc.mutz@qt.io>
* Change enum value names in QXmlStreamWriter::Error enum classMagdalena Stojek2025-06-161-23/+23
| | | | | | | | | | | Renamed enum values to avoid redundant naming. Resulted from API review. Pick-to: 6.10 Change-Id: I5213695c02a763d4689c5df97b20c282368b4fbd Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Marc Mutz <marc.mutz@qt.io>
* Mark QXmlStream{Reader,Writer} as security-criticalMarc Mutz2025-05-211-0/+1
| | | | | | | | | | | | | | | | If QXmlStream isn't security-criticial, what is? qxmlstream.h contains the definition of the QXmlString work-horse, so it has to be security-critical, too (until we remove it). Amends 8df072fc8006510c9b743e8ffedaaf51a876883a. QUIP: 23 Task-number: QTBUG-135194 Pick-to: 6.9 6.8 Change-Id: Ib366e63fb89aa0b69ad437f6688285b2c390c5c1 Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> Reviewed-by: Kai Köhne <kai.koehne@qt.io>
* QXmlStreamWriter: add option to stop writing after an errorMagdalena Stojek2025-05-201-7/+49
| | | | | | | | | | | | | | | | | | | By default, QXmlStreamWriter continues writing even after encountering an InvalidCharacter, EncodingError or CustomError, which contradicts expected behavior. This change introduces property stopWritingOnError with two new functions: setStopWritingOnError() and stopWritingOnError(), allowing users to control whether output halts immediately after the first such error. [ChangeLog][QtCore][QXmlStreamWriter] Added setStopWritingOnError() and stopWritingOnError() functions. Fixes: QTBUG-135861 Change-Id: Ia3ba894fc5bd8c5ff3a548e2585af9d435dec9b2 Reviewed-by: Safiyyah Moosa <safiyyah.moosa@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
* QXmlStreamReader: try to keep the source encoding of the added dataIvan Solovev2025-05-191-72/+132
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The QXmlStreamReader(QAnyStringView) constructor, as well as the QXmlStreamReader::addData(QAnyStringView) method were almost always converting the UTF-16 and Latin1 data to UTF-8. This commit tries to avoid unnecessary conversions by storing the data together with its encoding. As a result, we now have two decoders: * a chunkDecoder is used when the encoding of the provided chunk is supplied along with the data; * a document-global decoder is used when decoding raw data that is provided by QBA overloads. It tries to infer the encoding from the content, or explicitly uses the "encoding" attribute, if it was provided. This is the pre-existing behavior. This patch has several corner-cases, mostly related to the fact that the XML parser tries to read the XML prolog and extract the encoding from it. For example, what happens if we pass a QString (UTF-16 encoded) that has an XML prolog with Latin1 encoding? const QString data = u"<? xml encoding=\"iso-8859-1\"?><foo>ÄÖÜ</foo>"_s; QXmlStreamReader reader; reader.addData(data); reader.readNextStartElement(); const QString text = reader.readElementText(); QCOMPARE(text, u"ÄÖÜ"_s); The data inside the "<foo>" block can be represented by Latin1, but because we used QString to store it, it's already in UTF-16. This patch uses UTF-16 chunkDecoder to decode the data, but at the same time sets the document-global decoder to Latin1, so that the next raw data will be interpreted as Latin1. At the same time, if the provided encoding is not recognized, the document-global decoder is set to UTF-8, for backwards compatibility. The code path that uses an underlying QIODevice is unmodified. The patch does not add any new public APIs, so theoretically can be cherry-picked to older branches, but I'd prefer to have some testing on the current dev to make sure that it does not introduce any regressions. Fixes: QTBUG-124636 Change-Id: I89bf0cbbcf21d7533d99e56b2522dcced9f675eb Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
* QXmlStreamWriter: Refactor writeEscaped() to use switchMagdalena Stojek2025-05-101-22/+32
| | | | | | | | | | | | | The character escaping logic in writeEscaped() previously relied on a long if/else cascade to handle escaping and error detection. This change replaces the cascade with a switch statement, improving the control flow. The loop now exits when a non-null replacement is set. To support this, cases that previously raised an error without assigning a replacement now assign an empty, non-null string to trigger the break. Fixes: QTBUG-136681 Change-Id: I4a584738bcda741f881712547af12abd35e2fd0a Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
* QXmlStreamWriter: add error-handling APIMagdalena Stojek2025-05-061-23/+138
| | | | | | | | | | | | | | | | | | | | This change introduces QXmlStreamWriter::Error enum and three related functions to enable error reporting during XML writing operations: - error(): returns the current error state of the writer. - errorString(): returns the corresponding error message. - raiseError(): allows applications to raise custom write errors. This complements the existing hasError() method and aligns the writer with QXmlStreamReader's error handling. [ChangeLog][QtCore][QXmlStreamWriter] Added error handling API with QXmlStreamWriter::Error enum, error(), errorString(), and raiseError() functions. Fixes: QTBUG-82389 Change-Id: I4d57a9f611a303cf8dc05caf23b6d331a61684f9 Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
* QXmlStreamReader: add support for retrieving raw inner XML contentMagdalena Stojek2025-04-291-0/+109
| | | | | | | | | | | | | This change introduces a new function, readRawInnerData(), that returns the raw inner XML content of the current element, including nested tags, comments, CDATA, and processing instructions. [ChangeLog][QtCore][QXmlStreamReader] Added readRawInnerData() for retrieving the raw inner XML content of an element. Fixes: QTBUG-85141 Change-Id: I96dd0790d726cf8a196125384cbf8f8fa2587880 Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
* Refactor QXmlStreamWriterPrivate constructor to modernize initializationMagdalena Stojek2025-04-141-20/+10
| | | | | | | | | Apply NSDMI and move initializations to the constructor initializer list Fixes: QTBUG-135897 Pick-to: 6.9 6.8 Change-Id: I7adff41fa006669677df457704d20c02f1af7197 Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
* Clarify XML version support and improve documentation for QXmlStreamMagdalena Stojek2025-04-081-30/+58
| | | | | | | | | | | | | This updates the documentation for QXmlStreamReader and QXmlStreamWriter to explicitly state that only XML 1.0 is supported. While the writer allows specifying arbitrary version strings, it does not implement XML 1.1 features. The reader strictly enforces XML 1.0 and rejects unsupported versions. Fixes: QTBUG-43029 Pick-to: 6.9 6.8 6.5 Change-Id: I771de29e7915d7a4daafe2fe14d600adfbbac4ac Reviewed-by: Mate Barany <mate.barany@qt.io>
* QXmlStreamReader: fix addData() unnecessary conversion to UTF-8Ivan Solovev2025-04-041-0/+23
| | | | | | | | | | | | | | | | | | | The addData(QASV) overload was unconditionally converting UTF-16 and Latin1 data to UTF-8. However, if we already started reading the XML document, and we know that its encoding is UTF-16 or Latin1, then we know for sure that the new data has to be added as-is. Amends 6bc227a06a0d1392d220aa79ddb1cdc145d4f76e. [ChangeLog][QtCore][QXmlStreamReader] Fixed a bug when addData(QAnyStringView) was incorrectly recoding UTF-16 and Latin1 data to UTF-8, thus potentially mangling it. Fixes: QTBUG-135129 Pick-to: 6.9 6.8 6.5 Change-Id: Ie1171a5e5596b72a6f160031a4c5a9df3baae4fd Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QXmlStreamReader: fix parsing of non-wellformed inputsIvan Solovev2025-04-031-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The tst_QXmlStream::runTestSuite() had two issues which resulted in non-wellformed files not being checked in the incremental parsing mode. The first issue is that the TestSuiteHandler::endElement() function calls isWellformed() twice with two different modes, passing a pointer to a previously opened QFile to both calls. As a result, the first call uses ParseSinglePass mode, which reads the entire file at once. After that, the second call with ParseIncrementally mode already uses a file which has atEnd() == true. So, the call to readAll() simply returns an empty QByteArray. So, the ParseIncrementally test was effectively a no-op. This commit fixes it by explicitly calling seek(0) at the beginning of the isWellformed() function and also changing the type of the input parameter to be a QFile pointer instead of QIODevice pointer, to make sure that seek(0) actually makes sense. After that issue was solved, I noticed that in the incremental mode the algorithm that was feeding the data to QXmlStreamReader byte-by-byte was always skipping the first character (and also reading past the end of the buffer). As a result, all the XML files parsed in this mode were malformed. This was fixed by moving the increment of the index after the addData() call. Finally, after these two things were fixed, six test cases from the XML test suite started to fail, because the non-wellformed files were incorrectly reported as well-formed. Fix it by using StreamEOF instead of 0 in QXmlStreamReaderPrivate::fastScanContentCharList(). The test case fixes amend the beginning of the public history. The parser fix amends 817800ad39df10ca78e2c965a61d4d2025df622b which introduced StreamEOF instead of 0. Fixes: QTBUG-135471 Pick-to: 6.9 6.8 6.5 5.15 Change-Id: I885166252d40819a4460ec246db10bf448e4a8e2 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Put qxmlstreamreader code behind its feature-flagJuha Vuolle2025-03-311-1/+3
| | | | | | | | | | | | | | Missed feature guards from: - QXmlStreamReaderPrivate class declaration - QXmlStreamWriter::writeCurrentToken definition [*] [*] Function takes in a qxmlstreamreader. The declaration of the function already had the necessary feature guard. Pick-to: 6.9 6.8 Fixes: QTBUG-135230 Change-Id: I78741bc75a6baa8bc86ec5ce7d34a4333d8bdad4 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
* QXmlStreamReader::addData: lock encoding for QLatin1 caseIvan Solovev2025-03-251-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | This fixes a bug when addData() is used to add a full Latin1-encoded XML document with a proper "encoding" attribute. Once addData() is called, it immediately converts the data to UTF-8. However, if the encoding is not locked, the parser will later see the "encoding" attribute, and try to interpret the data according to the specified encoding. The QXSR(QASV) constructor is not affected, because it already locks the encoding. Add a unit-test for the issue. Amends 6bc227a06a0d1392d220aa79ddb1cdc145d4f76e. [ChangeLog][QtCore][QXmlStreamReader] Fixed a bug when calling addData() with a Latin1-encoded string containing a full XML document with an encoding attribute, could result in incorrect parsing of this document. Fixes: QTBUG-135033 Pick-to: 6.9 6.8 6.5 Change-Id: I9a35d16d743050ea4feccab3d1336747ce0abff4 Reviewed-by: Marc Mutz <marc.mutz@qt.io>
* QXmlStreamWriter: Ensure correct newline handling with auto-formattingMagdalena Stojek2025-03-191-2/+17
| | | | | | | | | | | | | | | | | | | Previously, QXmlStreamWriter would incorrectly insert a newline before the first token when writeStartDocument() was not used, while auto-formatting was enabled. This fix ensures that the first token is written inline without an extra leading newline, while preserving expected formatting for subsequent tokens. To achieve this, two new flags have been introduced: - didWriteStartDocument: Tracks whether writeStartDocument() was called. - didWriteAnyToken: Ensures that at least one token has been written before allowing newlines. Fixes: QTBUG-28721 Pick-to: 6.9 6.8 Change-Id: I8be7e8fc6ac0e63304359d24c6c8372e5ba42bb4 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QXmlStreamReader: use QOffsetStringArray::viewAt()Marc Mutz2024-12-191-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | This auto-computes the string's length without a NUL-byte scan, and also fixes a GCC 14 unity-build C++23 warning: In function ‘constexpr qsizetype QtPrivate::lengthHelperPointer(const Char*) [with Char = char]’, inlined from ‘constexpr qsizetype QtPrivate::lengthHelperPointer(const Char*) [with Char = char]’ at qbytearrayview.h:69:28, inlined from ‘constexpr QLatin1String::QLatin1String(const char*)’ at qlatin1stringview.h:52:62, inlined from ‘constexpr QLatin1StringView contextString(QXmlStreamReaderPrivate::XmlContext)’ at qxmlstream.cpp:814:90, inlined from ‘void QXmlStreamReaderPrivate::checkToken()’ at qxmlstream.cpp:4018:85: qbytearrayview.h:77:16: warning: ‘strlen’ argument missing terminating nul [-Wstringop-overread] 77 | while (data[i] != Char(0)) | ~~~~^ In file included from unity_0_cxx.cxx:412: qxmlstream.cpp: In member function ‘void QXmlStreamReaderPrivate::checkToken()’: qxmlstream.cpp:769:23: note: referenced argument declared here 769 | static constexpr auto QXmlStreamReader_XmlContextString = qOffsetStringArray( | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Amends c4301be7d5f94852e1b17f2c2989d5ca807855d4, which was picked to 5.15, but didn't use QOffsetStringArray there (and unity-builds exist only since 6.5), so only picking to 6.5. Pick-to: 6.9 6.8 6.5 Change-Id: Ib50369aed6e8248fb88f43c7569c8a435c2b152e Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Fix -no-feature-datestring and -no-feature-xmlstreamreaderJari Helaakoski2024-10-281-2/+2
| | | | | | Task-number: QTBUG-112830 Change-Id: I25dad19dee98d64eb5c226cbcc2b628f2a371ea4 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QXmlStreamWriter: fix attempts to write bad QStringsThiago Macieira2024-04-181-2/+6
| | | | | | | | | | | | | We weren't doing the decoding from UTF-16 to UTF-32, so weren't catching invalid code sequences [ChangeLog][QtCore][QXmlStreamWriter] The class now rejects writing UTF-8 and UTF-16 invalid input (improper code unit sequences). Task-number: QTBUG-122241 Pick-to: 6.5 6.6 6.7 Change-Id: I83dda2d36c904517b3c0fffd17b42d17c637fdc4 Reviewed-by: Mate Barany <mate.barany@qt.io>
* QXmlStreamWriter: decode UTF-8 into code pointsThiago Macieira2024-04-181-10/+36
| | | | | | | | | | | | | | | | | | | | | We were iterating over code *units* and that yielded wrong results. The one from the bug report was simply caused by the fact that QUtf8StringView::value_type is char, which is signed on x86, so the expression: *it <= u'\x1F' was true for all non-Latin1 content. But in attempting to fix this, I needed to do the proper UTF-8 decoding, as otherwise we wouldn't catch non-Latin1 sequences and such. [ChangeLog][QtCore][QXmlStreamWriter] Fixed a bug that caused the class to fail to write UTF-8 strings with non-US-ASCII content when passed as a QUtf8StringView. Fixes: QTBUG-122241 Pick-to: 6.5 6.6 6.7 Change-Id: I83dda2d36c904517b3c0fffd17b42bbf09a493d0 Reviewed-by: Mate Barany <mate.barany@qt.io>
* Xml: use new comparison helper macrosTatiana Borisova2024-03-201-16/+23
| | | | | | | | | | | | | | | | | | | | | | New comparison macros are used for following classes: -QXmlStreamAttribute -QXmlStreamNamespaceDeclaration -QXmlStreamNotationDeclaration -QXmlStreamEntityDeclaration Replace public operators operator==(), operator!=() of classes to friend methods comparesEqual(); Use QT_CORE_REMOVED_SINCE to get rid of current comparison methods and replace them with a friend. Add checkStreamNotationDeclarations()/checkStreamEntityDeclarations() test-cases to test change. Task-number: QTBUG-120300 Change-Id: I0b5642b2e23cc21ede7bc4888f0a9bddd6c08d07 Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Fix renamed and duplicated namespaces in QXmlStreamWriterØystein Heskestad2023-09-201-14/+54
| | | | | | | | | | | | | | | The XML stream writer previously added namespace declarations with the same URL as existing ones, but new names, and renamed the XML elements to use the new namespaces instead of the existing ones. [ChangeLog] Fix renamed and duplicated namespaces in QXmlStreamWriter. Pick-to: 6.5 6.6 Fixes: QTBUG-75456 Change-Id: I90706e067ac9991e9e6cd79ccb2373e4c6210b7b Done-With: Philip Allgaier <philip.allgaier@bpcompass.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
* QXmlStreamReader::readNextStartElement() - return false on document endAxel Spoerl2023-08-151-1/+1
| | | | | | | | | | | | | | | | | | | | | | | The method reads the next element in a loop, as long as valid elements exist. Within the loop, it returns - false if the end of an element has been reached - true if a new element has started When the document end has been reached, the loop continues, until readNext() returns Invalid. Then, PrematureEndOfDocumentError is launched. This is wrong, because reading beyond the document end is caused by a missing return condition in the loop. => Treat document end like element end and return false without reading beyond it. => Test correct behavior in tst_QXmlStream::readNextStartElement() Fixes: QTBUG-25944 Pick-to: 6.6 6.5 6.2 Change-Id: I0160b65880756a2be541e9f55dc79557fcb1f09f Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QXmlStreamReader: Fix translation context of error messagesFriedemann Kleint2023-07-241-2/+2
| | | | | | | | | | | | QObject::tr() should not be used. Amends c4301be7d5f94852e1b17f2c2989d5ca807855d4. Task-number: QTBUG-92113 Task-number: QTBUG-95188 Pick-to: 6.6 6.5 Change-Id: I09547c3d048d6b3726e33be74b06035f0eec4f31 Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
* QXmlStreamReader: Fix variable namingAxel Spoerl2023-07-111-4/+4
| | | | | | | | | | | Replace variable names referring to XML "location" with names referring to XML context. Task-number: QTBUG-92113 Task-number: QTBUG-95188 Pick-to: 6.6 6.5 Change-Id: If00e92dce237d95fa1850f0b45192995724ba99f Reviewed-by: Jan Arve Sæther <jan-arve.saether@qt.io>
* QXmlStreamReader: Raise error on unexpected tokensAxel Spoerl2023-07-101-8/+137
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | QXmlStreamReader accepted multiple DOCTYPE elements, containing DTD fragments in the XML prolog, and in the XML body. Well-formed but invalid XML files - with multiple DTD fragments in prolog and body, combined with recursive entity expansions - have caused infinite loops in QXmlStreamReader. This patch implements a token check in QXmlStreamReader. A stream is allowed to start with an XML prolog. StartDocument and DOCTYPE elements are only allowed in this prolog, which may also contain ProcessingInstruction and Comment elements. As soon as anything else is seen, the prolog ends. After that, the prolog-specific elements are treated as unexpected. Furthermore, the prolog can contain at most one DOCTYPE element. Update the documentation to reflect the new behavior. Add an autotest that checks the new error cases are correctly detected, and no error is raised for legitimate input. The original OSS-Fuzz files (see bug reports) are not included in this patch for file size reasons. They have been tested manually. Each of them has more than one DOCTYPE element, causing infinite loops in recursive entity expansions. The newly implemented functionality detects those invalid DTD fragments. By raising an error, it aborts stream reading before an infinite loop occurs. Thanks to OSS-Fuzz for finding this. Fixes: QTBUG-92113 Fixes: QTBUG-95188 Pick-to: 6.6 6.5 6.2 5.15 Change-Id: I0a082b9188b2eee50b396c4d5b1c9e1fd237bbdd Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
* QXmlStreamReader: use std::optionalAhmad Samir2023-07-061-7/+6
| | | | | | | | | | | | The optional-like FastScanNameResult was used to make some previous changes backport-able to Qt 5.15 (std::optional is C++17 whereas Qt 5.15 requires C++14). Amends commit 6326bec46a618c72feba4a2bb994c4d475050aed. Pick-to: 6.6 6.5 Change-Id: I409e1da83f82927c1eb24c47c1414c0c7ab1bf5b Reviewed-by: Marc Mutz <marc.mutz@qt.io>
* Fix typo in QXmlStreamReader error messageFriedemann Kleint2023-06-301-1/+1
| | | | | | | | | | Amends 6326bec46a618c72feba4a2bb994c4d475050aed. Pick-to: 6.6 6.5 6.2 5.15 Task-number: QTBUG-109781 Task-number: QTBUG-114829 Change-Id: Ib5189dc908cd61c6c6fa23024776a4a5baa75ca5 Reviewed-by: Robert Löhning <robert.loehning@qt.io>
* QXmlStreamReader: make fastScanName() indicate parsing status to callersAhmad Samir2023-06-281-6/+17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This fixes a crash while parsing an XML file with garbage data, the file starts with '<' then garbage data: - The loop in the parse() keeps iterating until it hits "case 262:", which calls fastScanName() - fastScanName() iterates over the text buffer scanning for the attribute name (e.g. "xml:lang"), until it finds ':' - Consider a Value val, fastScanName() is called on it, it would set val.prefix to a number > val.len, then it would hit the 4096 condition and return (returned 0, now it returns the equivalent of std::null_opt), which means that val.len doesn't get modified, making it smaller than val.prefix - The code would try constructing an XmlStringRef with negative length, which would hit an assert in one of QStringView's constructors Add an assert to the XmlStringRef constructor. Add unittest based on the file from the bug report. Later on I will replace FastScanNameResult with std::optional<qsizetype> (std::optional is C++17, which isn't required by Qt 5.15, and we want to backport this fix). Credit to OSS-Fuzz. Fixes: QTBUG-109781 Fixes: QTBUG-114829 Pick-to: 6.6 6.5 6.2 5.15 Change-Id: I455a5eeb47870c2ac9ffd0cbcdcd99c1ae2dd374 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
* QXmlStreamReader: change fastScanName() to take a Value*Ahmad Samir2023-06-281-8/+8
| | | | | | | | For easier debugging, e.g. to print out value.len and value.prefix. Pick-to: 6.6 6.5 6.5.2 6.2 5.15 Change-Id: Ib0eed38772f899502962f578775d34ea2744fdde Reviewed-by: Marc Mutz <marc.mutz@qt.io>
* QXmlStreamReader: Add key/value to "Invalid attribute" error messageAxel Spoerl2023-06-041-1/+1
| | | | | | | | | | | | | | When QXmlReaderPrivate::startDocument() parses declarations, it reports an error if a declaration contains an unknown attribute. The message doesn't mention the invalid attribute's key and value, so the user has to guess which one is the faulty declaration. This patch extends the error message by adding the respective key/value pair. Pick-to: 6.5 Change-Id: I7f5a228bed44937472880c3b3d63d5e816b39361 Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
* Convert remaining QT_NO_XMLSTREAM* to use feature systemEdward Welbourne2023-05-151-13/+14
| | | | | | | | | | | | | | | | Replace the check in qxmlstream.h with a QT_REQUIRE_CONFIG since the code that includes this header does no checking, whether on the define or the feature, so is better off getting told about the missing feature at the point of include than complaining about an undefined type despite its header being overtly included. For the rest, just do the usual transformation to QT_CONFIG(), flipping the ones that were #ifdef rather than #ifndef, so the condition is positive. Shifted a couple of open-braces of classes to the next line, where they belong, in the process. Change-Id: If9c496082f0489b0f9f4070364cbf840e603fce1 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
* Doc: Fix documentation warningsTopi Reinio2023-04-291-11/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | These warnings slipped in during a time period where documentation testing in the CI was disabled. src/network/kernel/qhostinfo.cpp:254: (qdoc) warning: clang couldn't find function when parsing \fn template<typename Functor> int QHostInfo::lookupHost(const QString &name, Functor functor) src/widgets/widgets/qcheckbox.cpp:102: (qdoc) warning: clang couldn't find function when parsing \fn void QCheckBox::stateChanged(Qt::CheckState state) src/corelib/kernel/qcoreapplication.cpp:2769: (qdoc) warning: clang couldn't find function when parsing \fn template<typename Functor> void QCoreApplication::requestPermission( const QPermission &permission, Functor functor) src/corelib/serialization/qxmlstream.cpp:3806: (qdoc) warning: clang couldn't find function when parsing \fn bool QXmlStreamAttributes::hasAttribute( const QString &qualifiedName) const src/corelib/text/qtliterals.qdoc:11: (qdoc) warning: Multiple topic commands found in comment: \namespace and \headerfile. Pick-to: 6.5 Change-Id: I38c605f358dbca1ef3e2bfe20a6424f7a4d44b4a Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
* Implement QXmlStreamReader::hasStandaloneDeclaration()Axel Spoerl2023-04-261-0/+17
| | | | | | | | | | | | | | | This patch implements a public getter for the hasStandalone attribute. It returns true, if standalone has been explicitly declared in an XML header and false otherwise. As this is no longer necessary it removes accessing QXmlStreamPrivate from QDomParser. [ChangeLog][QtCore][QXmlStreamReader] added hasStandaloneDeclaration() Change-Id: Iaaa0a728a6f7186e40637186077f7b49c112f7a9 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> Reviewed-by: Yuhang Zhao <yuhangzhao@deepin.org>
* QXmlStreamAttributes: port value()/hasAttribute() to QAnyStringViewMarc Mutz2023-04-251-49/+6
| | | | | | | | | | [ChangeLog][QtCore][QXmlStreamAttributes] Ported value() and hasAttribute() to QAnyStringView. Change-Id: I771b9cede1d581d3f1142246e7a25c36bcc850d6 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
* QDomDocument: no longer drop a provided 'standalone' attribute if 'no'Axel Spoerl2023-04-241-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | - Definition of 'standalone' attribute: An XML declaration containing the attribute 'standalone' with its value set to 'yes', tells the parser to ignore markup declarations in the DTD and to use them only for validation. The declaration attribute is optional and defaults to 'no'. - Behavior Qt5 In qt5, DOM documents contained the standalone attribute, regardless of whether or not it was explicitly specified. - Behavior Qt6 In Qt6, the standalone attribute was only contained in a DOM document, if its value was 'yes'. If it was explicitly declared with the value being 'no', it was dropped in the DOM document. - Expected behavior If the source specified it overtly, then the generated XML should contain the attribute, even when it takes its default value. - Code base QXmlStreamReader provides a public bool getter isStandaloneDocument(). This says whether the document is standalone or not. The information whether the attribute was actually specified gets lost. In consequence, the attribute was always dropped on non-standalone documents. - Fix This patch makes hasStandalone a member of QXmlStreamReaderPrivate, to record whether the attribute has been explicitly specified. QDomParser is modified to retain the standalone attribute, if QXmlStreamReaderPrivate::hasStandalone is true. - Test The patch adds a test function in tst_QDom. Fixes: QTBUG-111200 Pick-to: 6.5 6.2 Change-Id: I06a0f230a2d69597dd6453f8fd3b036943d08735 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
* Replace ushort*/uint* with char16_t*/char32_t* in private API [1]Ahmad Samir2023-03-151-1/+1
| | | | | | | Task-number: QTBUG-110403 Pick-to: 6.5 Change-Id: Ie20a831f22212d56659cf3c6940d17134ab5f2c5 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QXmlStreamWriter: hold the indent in std::string, not QByteArrayMarc Mutz2023-02-281-3/+4
| | | | | | | | | | | This means that, thanks to std::string's SSO, we won't allocate to hold the indent step string anymore, at least for non-pathological indents of up to 15-23 characters, depending on the particular std::string implementation. Task-number: QTBUG-103302 Change-Id: I63685619e86a3aa7bcfac41db84f64a78859bdb7 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
* Doc: Remove duplicate wordsAndreas Eliasson2023-02-281-1/+1
| | | | | | Change-Id: Ia7a38a1035bd34d00f20351a0adc3927e473b2e7 Pick-to: 6.5 6.4 6.2 Reviewed-by: Topi Reiniö <topi.reinio@qt.io>
* QtMiscUtils: add some more character helpersAhmad Samir2023-02-071-3/+3
| | | | | | | | | | | | | isHexDigit, isOctalDigit, isAsciiDigit, isAsciiLower, isAsciiUpper, isAsciiLetterOrNumber. This de-duplicates some code through out. Rename two local lambdas that were called "isAsciiLetterOrNumber" to not conflict with the method in QtMiscUtils. Change-Id: I5b631f95b9f109136d19515f7e20b8e2fbca3d43 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* QtCore: Disambiguate static variablesFriedemann Kleint2023-02-021-1/+1
| | | | | | | | | They cause clashes in CMake Unity (Jumbo) builds. Pick-to: 6.5 Task-number: QTBUG-109394 Change-Id: I5f1fbee07872a742a78adc9864fe00c710ca24d0 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
* Optimize QXmlStreamWriterPrivate::doWriteToDevice(QLatin1StringView)Marc Mutz2023-01-191-4/+8
| | | | | | | | | | | | Use a stack buffer, and perform the recoding from L1 to U8 in chunks. Solves the L1 case of Task-number: QTBUG-109284 Pick-to: 6.5 Change-Id: Ia9ac7d8b27fd452d24d9e27f0575f9fc83b6dcbc Reviewed-by: Mate Barany <mate.barany@qt.io> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>