Fix MSVC warnings in tests.

- Unused variables
- conversion truncations
- Overflow in expressions like '-1 + sizeof()'

Change-Id: Ibbd18497951e9e7e9dccaf596cb4e864b69ec02c
Reviewed-by: Jason McDonald <jason.mcdonald@nokia.com>
This commit is contained in:
Friedemann Kleint 2012-04-02 14:22:04 +02:00 committed by Qt by Nokia
parent b08f74f089
commit 8f2a088028
15 changed files with 70 additions and 48 deletions

View File

@ -381,7 +381,7 @@ void tst_QtConcurrentRun::exceptions()
bool caught = false; bool caught = false;
try { try {
QtConcurrent::run(throwFunction).waitForFinished(); QtConcurrent::run(throwFunction).waitForFinished();
} catch (Exception &e) { } catch (Exception &) {
caught = true; caught = true;
} }
if (!caught) if (!caught)
@ -390,7 +390,7 @@ void tst_QtConcurrentRun::exceptions()
caught = false; caught = false;
try { try {
QtConcurrent::run(throwFunctionReturn).waitForFinished(); QtConcurrent::run(throwFunctionReturn).waitForFinished();
} catch (Exception &e) { } catch (Exception &) {
caught = true; caught = true;
} }
if (!caught) if (!caught)

View File

@ -185,7 +185,7 @@ void tst_Utf8::charByChar()
} }
if (encoded.startsWith(utf8bom)) if (encoded.startsWith(utf8bom))
encoded = encoded.mid(strlen(utf8bom)); encoded = encoded.mid(int(strlen(utf8bom)));
QCOMPARE(encoded, utf8); QCOMPARE(encoded, utf8);
} }
{ {

View File

@ -53,7 +53,7 @@
#include "../../../network-settings.h" #include "../../../network-settings.h"
#endif #endif
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN) && !defined(_WIN32_WINNT)
#define _WIN32_WINNT 0x500 #define _WIN32_WINNT 0x500
#endif #endif

View File

@ -42,10 +42,10 @@
#include <windows.h> #include <windows.h>
int APIENTRY WinMain(HINSTANCE hInstance, int APIENTRY WinMain(HINSTANCE /* hInstance */,
HINSTANCE hPrevInstance, HINSTANCE /* hPrevInstance */,
LPSTR lpCmdLine, LPSTR /* lpCmdLine */,
int nCmdShow) int /* nCmdShow */)
{ {
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
@ -64,4 +64,4 @@ int APIENTRY WinMain(HINSTANCE hInstance,
WriteFile(hStderr, &c, 1, &wrote, 0); WriteFile(hStderr, &c, 1, &wrote, 0);
} }
return 0; return 0;
} }

View File

@ -429,7 +429,7 @@ template<> struct TestValueFactory<QMetaType::UChar> {
static uchar *create() { return new uchar('u'); } static uchar *create() { return new uchar('u'); }
}; };
template<> struct TestValueFactory<QMetaType::Float> { template<> struct TestValueFactory<QMetaType::Float> {
static float *create() { return new float(3.14); } static float *create() { return new float(3.14f); }
}; };
template<> struct TestValueFactory<QMetaType::QObjectStar> { template<> struct TestValueFactory<QMetaType::QObjectStar> {
static QObject * *create() { return new QObject *(0); } static QObject * *create() { return new QObject *(0); }

View File

@ -984,7 +984,7 @@ void tst_QXmlStream::writeAttributesWithSpace() const
QXmlStreamWriter writer(&buffer); QXmlStreamWriter writer(&buffer);
writer.writeStartDocument(); writer.writeStartDocument();
writer.writeEmptyElement("A"); writer.writeEmptyElement("A");
writer.writeAttribute("attribute", QString("value")+QChar::Nbsp); writer.writeAttribute("attribute", QStringLiteral("value") + QChar(QChar::Nbsp));
writer.writeEndDocument(); writer.writeEndDocument();
QString s = QString("<?xml version=\"1.0\" encoding=\"UTF-8\"?><A attribute=\"value%1\"/>\n").arg(QChar(QChar::Nbsp)); QString s = QString("<?xml version=\"1.0\" encoding=\"UTF-8\"?><A attribute=\"value%1\"/>\n").arg(QChar(QChar::Nbsp));
QCOMPARE(buffer.buffer().data(), s.toUtf8().data()); QCOMPARE(buffer.buffer().data(), s.toUtf8().data());
@ -1512,35 +1512,38 @@ void tst_QXmlStream::hasError() const
// Failure caused by write(QString) // Failure caused by write(QString)
FakeBuffer fb; FakeBuffer fb;
QVERIFY(fb.open(QBuffer::ReadWrite)); QVERIFY(fb.open(QBuffer::ReadWrite));
fb.setCapacity(strlen("<?xml version=\"")); const QByteArray expected = QByteArrayLiteral("<?xml version=\"");
fb.setCapacity(expected.size());
QXmlStreamWriter writer(&fb); QXmlStreamWriter writer(&fb);
writer.writeStartDocument(); writer.writeStartDocument();
QVERIFY(writer.hasError()); QVERIFY(writer.hasError());
QCOMPARE(fb.data(), QByteArray("<?xml version=\"")); QCOMPARE(fb.data(), expected);
} }
{ {
// Failure caused by write(char *) // Failure caused by write(char *)
FakeBuffer fb; FakeBuffer fb;
QVERIFY(fb.open(QBuffer::ReadWrite)); QVERIFY(fb.open(QBuffer::ReadWrite));
fb.setCapacity(strlen("<?xml version=\"1.0")); const QByteArray expected = QByteArrayLiteral("<?xml version=\"1.0");
fb.setCapacity(expected.size());
QXmlStreamWriter writer(&fb); QXmlStreamWriter writer(&fb);
writer.writeStartDocument(); writer.writeStartDocument();
QVERIFY(writer.hasError()); QVERIFY(writer.hasError());
QCOMPARE(fb.data(), QByteArray("<?xml version=\"1.0")); QCOMPARE(fb.data(), expected);
} }
{ {
// Failure caused by write(QStringRef) // Failure caused by write(QStringRef)
FakeBuffer fb; FakeBuffer fb;
QVERIFY(fb.open(QBuffer::ReadWrite)); QVERIFY(fb.open(QBuffer::ReadWrite));
fb.setCapacity(strlen("<?xml version=\"1.0\" encoding=\"UTF-8\"?><test xmlns:")); const QByteArray expected = QByteArrayLiteral("<?xml version=\"1.0\" encoding=\"UTF-8\"?><test xmlns:");
fb.setCapacity(expected.size());
QXmlStreamWriter writer(&fb); QXmlStreamWriter writer(&fb);
writer.writeStartDocument(); writer.writeStartDocument();
writer.writeStartElement("test"); writer.writeStartElement("test");
writer.writeNamespace("http://foo.bar", "foo"); writer.writeNamespace("http://foo.bar", "foo");
QVERIFY(writer.hasError()); QVERIFY(writer.hasError());
QCOMPARE(fb.data(), QByteArray("<?xml version=\"1.0\" encoding=\"UTF-8\"?><test xmlns:")); QCOMPARE(fb.data(), expected);
} }
{ {

View File

@ -422,7 +422,9 @@ void tst_QKeySequence::mnemonic()
QFETCH(QString, key); QFETCH(QString, key);
QFETCH(bool, warning); QFETCH(bool, warning);
#ifndef QT_NO_DEBUG #ifdef QT_NO_DEBUG
Q_UNUSED(warning)
#else
if (warning) { if (warning) {
QString str = QString::fromLatin1("QKeySequence::mnemonic: \"%1\" contains multiple occurrences of '&'").arg(string); QString str = QString::fromLatin1("QKeySequence::mnemonic: \"%1\" contains multiple occurrences of '&'").arg(string);
QTest::ignoreMessage(QtWarningMsg, qPrintable(str)); QTest::ignoreMessage(QtWarningMsg, qPrintable(str));

View File

@ -42,7 +42,7 @@
#include <QtTest/QtTest> #include <QtTest/QtTest>
#include <qmatrix.h> #include <qmatrix.h>
#include <math.h> #include <qmath.h>
#include <qpolygon.h> #include <qpolygon.h>
Q_DECLARE_METATYPE(QRect) Q_DECLARE_METATYPE(QRect)

View File

@ -118,6 +118,7 @@ struct SharedResource : public QOpenGLSharedResource
void freeResource(QOpenGLContext *context) void freeResource(QOpenGLContext *context)
{ {
Q_ASSERT(context == QOpenGLContext::currentContext()); Q_ASSERT(context == QOpenGLContext::currentContext());
Q_UNUSED(context)
resource = 0; resource = 0;
if (tracker) if (tracker)
tracker->freeResourceCalls++; tracker->freeResourceCalls++;

View File

@ -309,9 +309,10 @@ void tst_QUdpSocket::broadcasting()
QByteArray arr; arr.resize(serverSocket.pendingDatagramSize() + 1); QByteArray arr; arr.resize(serverSocket.pendingDatagramSize() + 1);
QHostAddress host; QHostAddress host;
quint16 port; quint16 port;
const int messageLength = int(strlen(message[i]));
QCOMPARE((int) serverSocket.readDatagram(arr.data(), arr.size() - 1, &host, &port), QCOMPARE((int) serverSocket.readDatagram(arr.data(), arr.size() - 1, &host, &port),
(int) strlen(message[i])); messageLength);
arr.resize(strlen(message[i])); arr.resize(messageLength);
QCOMPARE(arr, QByteArray(message[i])); QCOMPARE(arr, QByteArray(message[i]));
} while (serverSocket.hasPendingDatagrams()); } while (serverSocket.hasPendingDatagrams());
} }

View File

@ -863,7 +863,7 @@ static const char connect2[] = "\5\1\0\3\11localhost\0\25"; // Connect hostname
static const char connect2a[] = "\5\1\0\3"; // just "Connect to hostname" static const char connect2a[] = "\5\1\0\3"; // just "Connect to hostname"
static const char connected[] = "\5\0\0"; static const char connected[] = "\5\0\0";
#define QBA(x) (QByteArray::fromRawData(x, -1 + sizeof(x))) #define QBA(x) (QByteArray::fromRawData(x, int(sizeof(x)) - 1))
void tst_NetworkSelfTest::socks5Proxy() void tst_NetworkSelfTest::socks5Proxy()
{ {
@ -873,42 +873,48 @@ void tst_NetworkSelfTest::socks5Proxy()
} ip4Address; } ip4Address;
ip4Address.data = qToBigEndian(serverIpAddress().toIPv4Address()); ip4Address.data = qToBigEndian(serverIpAddress().toIPv4Address());
const QByteArray handshakeNoAuthData = QByteArray(handshakeNoAuth, int(sizeof handshakeNoAuth) - 1);
const QByteArray handshakeOkNoAuthData = QByteArray(handshakeOkNoAuth, int(sizeof handshakeOkNoAuth) - 1);
const QByteArray connect1Data = QByteArray(connect1, int(sizeof connect1) - 1);
const QByteArray connectedData = QByteArray(connected, int(sizeof connected) - 1);
const QByteArray connect2Data = QByteArray(connect2, int(sizeof connect2) - 1);
netChat(1080, QList<Chat>() netChat(1080, QList<Chat>()
// IP address connection // IP address connection
<< Chat::send(QByteArray(handshakeNoAuth, -1 + sizeof handshakeNoAuth)) << Chat::send(handshakeNoAuthData)
<< Chat::expect(QByteArray(handshakeOkNoAuth, -1 + sizeof handshakeOkNoAuth)) << Chat::expect(handshakeOkNoAuthData)
<< Chat::send(QByteArray(connect1, -1 + sizeof connect1)) << Chat::send(connect1Data)
<< Chat::expect(QByteArray(connected, -1 + sizeof connected)) << Chat::expect(connectedData)
<< Chat::expect("\1") // IPv4 address following << Chat::expect("\1") // IPv4 address following
<< Chat::skipBytes(6) // the server's local address and port << Chat::skipBytes(6) // the server's local address and port
<< ftpChat() << ftpChat()
// connect by IP // connect by IP
<< Chat::Reconnect << Chat::Reconnect
<< Chat::send(QByteArray(handshakeNoAuth, -1 + sizeof handshakeNoAuth)) << Chat::send(handshakeNoAuthData)
<< Chat::expect(QByteArray(handshakeOkNoAuth, -1 + sizeof handshakeOkNoAuth)) << Chat::expect(handshakeOkNoAuthData)
<< Chat::send(QBA(connect1a) + QByteArray::fromRawData(ip4Address.buf, 4) + QBA(connect1b)) << Chat::send(QBA(connect1a) + QByteArray::fromRawData(ip4Address.buf, 4) + QBA(connect1b))
<< Chat::expect(QByteArray(connected, -1 + sizeof connected)) << Chat::expect(connectedData)
<< Chat::expect("\1") // IPv4 address following << Chat::expect("\1") // IPv4 address following
<< Chat::skipBytes(6) // the server's local address and port << Chat::skipBytes(6) // the server's local address and port
<< ftpChat() << ftpChat()
// connect to "localhost" by hostname // connect to "localhost" by hostname
<< Chat::Reconnect << Chat::Reconnect
<< Chat::send(QByteArray(handshakeNoAuth, -1 + sizeof handshakeNoAuth)) << Chat::send(handshakeNoAuthData)
<< Chat::expect(QByteArray(handshakeOkNoAuth, -1 + sizeof handshakeOkNoAuth)) << Chat::expect(handshakeOkNoAuthData)
<< Chat::send(QByteArray(connect2, -1 + sizeof connect2)) << Chat::send(connect2Data)
<< Chat::expect(QByteArray(connected, -1 + sizeof connected)) << Chat::expect(connectedData)
<< Chat::expect("\1") // IPv4 address following << Chat::expect("\1") // IPv4 address following
<< Chat::skipBytes(6) // the server's local address and port << Chat::skipBytes(6) // the server's local address and port
<< ftpChat() << ftpChat()
// connect to server by its official name // connect to server by its official name
<< Chat::Reconnect << Chat::Reconnect
<< Chat::send(QByteArray(handshakeNoAuth, -1 + sizeof handshakeNoAuth)) << Chat::send(handshakeNoAuthData)
<< Chat::expect(QByteArray(handshakeOkNoAuth, -1 + sizeof handshakeOkNoAuth)) << Chat::expect(handshakeOkNoAuthData)
<< Chat::send(QBA(connect2a) + char(QtNetworkSettings::serverName().size()) + QtNetworkSettings::serverName().toLatin1() + QBA(connect1b)) << Chat::send(QBA(connect2a) + char(QtNetworkSettings::serverName().size()) + QtNetworkSettings::serverName().toLatin1() + QBA(connect1b))
<< Chat::expect(QByteArray(connected, -1 + sizeof connected)) << Chat::expect(connectedData)
<< Chat::expect("\1") // IPv4 address following << Chat::expect("\1") // IPv4 address following
<< Chat::skipBytes(6) // the server's local address and port << Chat::skipBytes(6) // the server's local address and port
<< ftpChat() << ftpChat()
@ -917,18 +923,25 @@ void tst_NetworkSelfTest::socks5Proxy()
void tst_NetworkSelfTest::socks5ProxyAuth() void tst_NetworkSelfTest::socks5ProxyAuth()
{ {
const QByteArray handshakeNoAuthData = QByteArray(handshakeNoAuth, int(sizeof handshakeNoAuth) - 1);
const QByteArray connect1Data = QByteArray(connect1, int(sizeof connect1) - 1);
const QByteArray connectedData = QByteArray(connected, int(sizeof connected) - 1);
const QByteArray handshakeAuthNotOkData = QByteArray(handshakeAuthNotOk, int(sizeof(handshakeAuthNotOk)) - 1);
const QByteArray handshakeAuthPasswordData = QByteArray(handshakeAuthPassword, int(sizeof(handshakeAuthPassword)) - 1);
const QByteArray handshakeOkPasswdAuthData = QByteArray(handshakeOkPasswdAuth, int(sizeof(handshakeOkPasswdAuth)) - 1);
netChat(1081, QList<Chat>() netChat(1081, QList<Chat>()
// unauthenticated connect -- will get error // unauthenticated connect -- will get error
<< Chat::send(QByteArray(handshakeNoAuth, -1 + sizeof handshakeNoAuth)) << Chat::send(handshakeNoAuthData)
<< Chat::expect(QByteArray(handshakeAuthNotOk, -1 + sizeof handshakeAuthNotOk)) << Chat::expect(handshakeAuthNotOkData)
<< Chat::RemoteDisconnect << Chat::RemoteDisconnect
// now try to connect with authentication // now try to connect with authentication
<< Chat::Reconnect << Chat::Reconnect
<< Chat::send(QByteArray(handshakeAuthPassword, -1 + sizeof handshakeAuthPassword)) << Chat::send(handshakeAuthPasswordData)
<< Chat::expect(QByteArray(handshakeOkPasswdAuth, -1 + sizeof handshakeOkPasswdAuth)) << Chat::expect(handshakeOkPasswdAuthData)
<< Chat::send(QByteArray(connect1, -1 + sizeof connect1)) << Chat::send(connect1Data)
<< Chat::expect(QByteArray(connected, -1 + sizeof connected)) << Chat::expect(connectedData)
<< Chat::expect("\1") // IPv4 address following << Chat::expect("\1") // IPv4 address following
<< Chat::skipBytes(6) // the server's local address and port << Chat::skipBytes(6) // the server's local address and port
<< ftpChat() << ftpChat()

View File

@ -199,7 +199,8 @@ QString tst_QPrinterInfo::getOutputFromCommand(const QStringList& command)
return QString(array); return QString(array);
} }
#else #else
return QString(); Q_UNUSED(command)
return QString();
#endif #endif
} }

View File

@ -135,7 +135,7 @@ static QList<QByteArray> splitLines(QByteArray ba)
if (index == -1) { if (index == -1) {
continue; continue;
} }
int end = line.indexOf('"', index + strlen(markers[j][0])); const int end = line.indexOf('"', index + int(strlen(markers[j][0])));
if (end == -1) { if (end == -1) {
continue; continue;
} }
@ -686,11 +686,12 @@ QString extractXmlAttribute(const QString &line, const char *attribute)
int index = line.indexOf(attribute); int index = line.indexOf(attribute);
if (index == -1) if (index == -1)
return QString(); return QString();
int end = line.indexOf('"', index + strlen(attribute)); const int attributeLength = int(strlen(attribute));
const int end = line.indexOf('"', index + attributeLength);
if (end == -1) if (end == -1)
return QString(); return QString();
QString result = line.mid(index + strlen(attribute), end - index - strlen(attribute)); const QString result = line.mid(index + attributeLength, end - index - attributeLength);
if (result.isEmpty()) if (result.isEmpty())
return ""; // ensure empty but not null return ""; // ensure empty but not null
return result; return result;

View File

@ -3712,7 +3712,7 @@ void tst_QGraphicsView::render()
view.show(); view.show();
QTest::qWaitForWindowShown(&view); QTest::qWaitForWindowShown(&view);
QApplication::processEvents(); QApplication::processEvents();
QTRY_VERIFY(view.painted > 0); QTRY_VERIFY(view.painted);
RenderTester *r1 = new RenderTester(QRectF(0, 0, 50, 50)); RenderTester *r1 = new RenderTester(QRectF(0, 0, 50, 50));
RenderTester *r2 = new RenderTester(QRectF(50, 50, 50, 50)); RenderTester *r2 = new RenderTester(QRectF(50, 50, 50, 50));

View File

@ -149,7 +149,7 @@ struct FileSystem
memset( reparseInfo, 0, sizeof( *reparseInfo )); memset( reparseInfo, 0, sizeof( *reparseInfo ));
reparseInfo->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT; reparseInfo->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
reparseInfo->ReparseTargetLength = target.size() * sizeof(wchar_t); reparseInfo->ReparseTargetLength = DWORD(target.size() * sizeof(wchar_t));
reparseInfo->ReparseTargetMaximumLength = reparseInfo->ReparseTargetLength + sizeof(wchar_t); reparseInfo->ReparseTargetMaximumLength = reparseInfo->ReparseTargetLength + sizeof(wchar_t);
target.toWCharArray(reparseInfo->ReparseTarget); target.toWCharArray(reparseInfo->ReparseTarget);
reparseInfo->ReparseDataLength = reparseInfo->ReparseTargetLength + 12; reparseInfo->ReparseDataLength = reparseInfo->ReparseTargetLength + 12;