From 033205bb598fe49e6ca4c90fb4e046e996c20252 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 6 Jan 2016 12:51:02 +0100 Subject: Fix UB in tst_QIODevice::getSetCheck() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The reinterpret cast from a QTcpSocket → QAbstractSocket → QIODevice to MyIODevice → QIODevice was undefined. Fix by simply instantiating a MyIODevice, which must then inherit from QTcpSocket, of course. Instead of fixing the class name in the overridden setOpenMode() method, simply make the base class' implementation public with a using declaration. Found by UBSan: qtbase/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp:84:22: runtime error: member call on address 0x7ffcca2d23f0 which does not point to an object of type 'MyIODevice' 0x7ffcca2d23f0: note: object is of type 'QTcpSocket' Change-Id: I939b3548949b9b5765df4a6cc81875e169fd69dd Reviewed-by: Alex Trotsenko Reviewed-by: Olivier Goffart (Woboq GmbH) --- tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'tests/auto/corelib/io') diff --git a/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp b/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp index 565ca18899..af79de06d5 100644 --- a/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp +++ b/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp @@ -74,16 +74,15 @@ void tst_QIODevice::getSetCheck() { // OpenMode QIODevice::openMode() // void QIODevice::setOpenMode(OpenMode) - class MyIODevice : public QIODevice { + class MyIODevice : public QTcpSocket { public: - void setOpenMode(OpenMode openMode) { QIODevice::setOpenMode(openMode); } + using QTcpSocket::setOpenMode; }; - QTcpSocket var1; - MyIODevice *obj1 = reinterpret_cast(&var1); - obj1->setOpenMode(QIODevice::OpenMode(QIODevice::NotOpen)); - QCOMPARE(QIODevice::OpenMode(QIODevice::NotOpen), obj1->openMode()); - obj1->setOpenMode(QIODevice::OpenMode(QIODevice::ReadWrite)); - QCOMPARE(QIODevice::OpenMode(QIODevice::ReadWrite), obj1->openMode()); + MyIODevice var1; + var1.setOpenMode(QIODevice::OpenMode(QIODevice::NotOpen)); + QCOMPARE(QIODevice::OpenMode(QIODevice::NotOpen), var1.openMode()); + var1.setOpenMode(QIODevice::OpenMode(QIODevice::ReadWrite)); + QCOMPARE(QIODevice::OpenMode(QIODevice::ReadWrite), var1.openMode()); } //---------------------------------------------------------------------------------- -- cgit v1.2.3 From 8dad3bf2121e3ad5e405665fefa28c4d53192bf7 Mon Sep 17 00:00:00 2001 From: Kevin Funk Date: Thu, 14 Jan 2016 23:33:28 +0100 Subject: Fix toDisplayString(QUrl::PreferLocalFile) on Win When using QUrl::PreferLocalFile we do want to strip the leading slash, as toLocalFile() would do as well. Behavior change by means of an example: QUrl url(QUrl::fromLocalFile("C:/file.txt") url.toLocalFile() --> "C:/file.txt" Before: url.toDisplayString(QUrl::PreferLocalFile) --> "/C:/file.txt" After: url.toDisplayString(QUrl::PreferLocalFile) --> "C:/file.txt" Task-number: QTBUG-41729 Change-Id: I7d425541f6077ebcf3fcf46feeb7e0f03a0d7fe2 Reviewed-by: Dominik Haumann Reviewed-by: Thiago Macieira --- tests/auto/corelib/io/qurl/tst_qurl.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'tests/auto/corelib/io') diff --git a/tests/auto/corelib/io/qurl/tst_qurl.cpp b/tests/auto/corelib/io/qurl/tst_qurl.cpp index 031a35b380..519b05f492 100644 --- a/tests/auto/corelib/io/qurl/tst_qurl.cpp +++ b/tests/auto/corelib/io/qurl/tst_qurl.cpp @@ -69,6 +69,8 @@ private slots: void resolving(); void toString_data(); void toString(); + void toString_PreferLocalFile_data(); + void toString_PreferLocalFile(); void toString_constructed_data(); void toString_constructed(); void toAndFromStringList_data(); @@ -1050,6 +1052,29 @@ void tst_QUrl::toString() QCOMPARE(url.adjusted(opt).toString(), string); } +void tst_QUrl::toString_PreferLocalFile_data() +{ + QTest::addColumn("url"); + QTest::addColumn("string"); + +#ifdef Q_OS_WIN + QTest::newRow("win-drive") << QUrl(QString::fromLatin1("file:///c:/windows/regedit.exe")) + << QString::fromLatin1("c:/windows/regedit.exe"); + QTest::newRow("win-share") << QUrl(QString::fromLatin1("//Anarki/homes")) + << QString::fromLatin1("//anarki/homes"); +#else + QTest::newRow("unix-path") << QUrl(QString::fromLatin1("file:///tmp")) + << QString::fromLatin1("/tmp"); +#endif +} + +void tst_QUrl::toString_PreferLocalFile() +{ + QFETCH(QUrl, url); + QFETCH(QString, string); + + QCOMPARE(url.toString(QUrl::PreferLocalFile), string); +} void tst_QUrl::toAndFromStringList_data() { -- cgit v1.2.3