Fix UB in tst_QIODevice::getSetCheck()

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 <alex1973tr@gmail.com>
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
Marc Mutz 2016-01-06 12:51:02 +01:00
parent 4f48c3a309
commit 033205bb59

View File

@ -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<MyIODevice*>(&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());
}
//----------------------------------------------------------------------------------