From 0d6bffcd1bfd5b08e5b8984c9385b08d5137480c Mon Sep 17 00:00:00 2001 From: Paul Wicking Date: Fri, 2 Feb 2024 21:22:16 +0100 Subject: QDoc: Ensure help project QHP SHA1s work cross platform MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QDoc can generate Qt Help Project files (`.qhp`-files) through its `HelpProjectWriter` interface. When it does, it also generates a `.sha1` file for the generated `.qhp` file. The SHA1 hash it generates on Windows differs from that it generates on other platforms. This issue was uncovered when restructuring QDoc's tests for the files it generates. Upon investigating the problem, the initial hypothesis was that it was possibly caused by differing line endings on different platforms. This theory led to the discovery of a forum.qt.io discussion about a similar problem: https://forum.qt.io/topic/124177/qcryptographichash-results-mismatches/3 The forum discussion suggests dropping the `QFile::Text` flag when opening the file for writing. Indeed, the documentation for `QIODeviceBase::Text` (the implementation of said flag) states that "When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32." This means that on Windows, the `QFile::Text` flag can cause line ending conversions when writing files. Dropping that flag when opening the output file ensures that the data is written without any automatic line ending conversions, thus the result should be consistent across different platforms. This means QDoc must open the hashFile in binary mode by not passing the `QFile::Text` flag. The reason for this is that, on Windows, the `QFile::Text` flag can cause line ending conversions when reading or writing text files. Validating the fix is challenging, as `HelpProjectWriter` isn't covered by any unit tests. However; the change that follows this makes use of the QHP-generating capabilities of QDoc in a test, and serves to validate this fix. Fixes: QTBUG-121850 Task-number: QTBUG-119500 Pick-to: 6.5 Change-Id: I9c3c2242c7698e1b7bc031dd33cdbd305d96c713 Reviewed-by: Qt CI Bot Reviewed-by: Topi Reiniƶ (cherry picked from commit 473fbb486926ba8629447d2b91483d982075b4fb) Reviewed-by: Qt Cherry-pick Bot (cherry picked from commit 8dcf9dd90d0225ca4a1712a78bcfce7389c9b19a) Reviewed-by: Paul Wicking --- src/qdoc/qdoc/helpprojectwriter.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/qdoc/qdoc/helpprojectwriter.cpp b/src/qdoc/qdoc/helpprojectwriter.cpp index 4d9edbdf7..86e85bcaa 100644 --- a/src/qdoc/qdoc/helpprojectwriter.cpp +++ b/src/qdoc/qdoc/helpprojectwriter.cpp @@ -466,7 +466,7 @@ void HelpProjectWriter::writeHashFile(QFile &file) hash.addData(&file); QFile hashFile(file.fileName() + ".sha1"); - if (!hashFile.open(QFile::WriteOnly | QFile::Text)) + if (!hashFile.open(QFile::WriteOnly)) return; hashFile.write(hash.result().toHex()); @@ -578,7 +578,7 @@ void HelpProjectWriter::generateProject(HelpProject &project) project.m_keywords.clear(); QFile file(m_outputDir + QDir::separator() + project.m_fileName); - if (!file.open(QFile::WriteOnly | QFile::Text)) + if (!file.open(QFile::WriteOnly)) return; QXmlStreamWriter writer(&file); -- cgit v1.2.3