From 0a49c93037e5f819f60486af318b6708e3b2b4aa Mon Sep 17 00:00:00 2001 From: Oliver Wolff Date: Fri, 17 May 2019 12:35:04 +0200 Subject: qbluetoothsocket_winrt: Fix clang warnings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I71eafba9e7ff47dbfd89e3a650b83b55d1196693 Reviewed-by: André de la Rocha Reviewed-by: Alex Blasche --- src/bluetooth/qbluetoothsocket_winrt.cpp | 64 +++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/src/bluetooth/qbluetoothsocket_winrt.cpp b/src/bluetooth/qbluetoothsocket_winrt.cpp index 72d72040..48b14757 100644 --- a/src/bluetooth/qbluetoothsocket_winrt.cpp +++ b/src/bluetooth/qbluetoothsocket_winrt.cpp @@ -94,15 +94,22 @@ static inline QString qt_QStringFromHString(const HString &string) { UINT32 length; PCWSTR rawString = string.GetRawBuffer(&length); - return QString::fromWCharArray(rawString, length); + if (length > INT_MAX) + length = INT_MAX; + return QString::fromWCharArray(rawString, int(length)); } static qint64 writeIOStream(ComPtr stream, const char *data, qint64 len) { ComPtr buffer; - HRESULT hr = g->bufferFactory->Create(len, &buffer); + if (len > UINT32_MAX) { + qCWarning(QT_BT_WINRT) << "writeIOStream can only write up to" << UINT32_MAX << "bytes."; + len = UINT32_MAX; + } + quint32 ulen = static_cast(len); + HRESULT hr = g->bufferFactory->Create(ulen, &buffer); Q_ASSERT_SUCCEEDED(hr); - hr = buffer->put_Length(len); + hr = buffer->put_Length(ulen); Q_ASSERT_SUCCEEDED(hr); ComPtr byteArrayAccess; hr = buffer.As(&byteArrayAccess); @@ -110,7 +117,7 @@ static qint64 writeIOStream(ComPtr stream, const char *data, qint byte *bytes; hr = byteArrayAccess->Buffer(&bytes); Q_ASSERT_SUCCEEDED(hr); - memcpy(bytes, data, len); + memcpy(bytes, data, ulen); ComPtr> op; hr = stream->WriteAsync(buffer.Get(), &op); RETURN_IF_FAILED("Failed to write to stream", return -1); @@ -257,7 +264,7 @@ public: return S_OK; } - QByteArray newData(reinterpret_cast(data), qint64(bufferLength)); + QByteArray newData(reinterpret_cast(data), int(bufferLength)); QMutexLocker readLocker(&m_mutex); if (m_pendingData.isEmpty()) QMetaObject::invokeMethod(this, "notifyAboutNewData", Qt::QueuedConnection); @@ -448,7 +455,8 @@ void QBluetoothSocketPrivateWinRT::connectToService( q->setSocketError(QBluetoothSocket::UnknownSocketError); return; } - connectToServiceHelper(service.device().address(), service.protocolServiceMultiplexer(), openMode); + connectToServiceHelper(service.device().address(), + quint16(service.protocolServiceMultiplexer()), openMode); } else if (!connectionHostName.isEmpty() && !connectionServiceName.isEmpty()) { Q_ASSERT(service.socketProtocol() == QBluetoothServiceInfo::RfcommProtocol); if (!ensureNativeSocket(QBluetoothServiceInfo::RfcommProtocol)) { @@ -472,7 +480,8 @@ void QBluetoothSocketPrivateWinRT::connectToService( q->setSocketError(QBluetoothSocket::UnknownSocketError); return; } - connectToServiceHelper(service.device().address(), service.serverChannel(), openMode); + connectToServiceHelper(service.device().address(), quint16(service.serverChannel()), + openMode); } else { // try doing service discovery to see if we can find the socket if (service.serviceUuid().isNull() @@ -597,7 +606,13 @@ quint16 QBluetoothSocketPrivateWinRT::localPort() const HString localPortString; hr = info->get_LocalPort(localPortString.GetAddressOf()); Q_ASSERT_SUCCEEDED(hr); - return qt_QStringFromHString(localPortString).toInt(); + bool ok = true; + const uint port = qt_QStringFromHString(localPortString).toUInt(&ok); + if (!ok || port > UINT16_MAX) { + qCWarning(QT_BT_WINRT) << "Unexpected local port"; + return 0; + } + return quint16(port); } QString QBluetoothSocketPrivateWinRT::peerName() const @@ -648,7 +663,13 @@ quint16 QBluetoothSocketPrivateWinRT::peerPort() const HString remotePortString; hr = info->get_LocalPort(remotePortString.GetAddressOf()); Q_ASSERT_SUCCEEDED(hr); - return qt_QStringFromHString(remotePortString).toInt(); + bool ok = true; + const uint port = qt_QStringFromHString(remotePortString).toUInt(&ok); + if (!ok || port > UINT16_MAX) { + qCWarning(QT_BT_WINRT) << "Unexpected remote port"; + return 0; + } + return quint16(port); } qint64 QBluetoothSocketPrivateWinRT::writeData(const char *data, qint64 maxSize) @@ -687,8 +708,11 @@ qint64 QBluetoothSocketPrivateWinRT::readData(char *data, qint64 maxSize) return -1; } - if (!buffer.isEmpty()) - return buffer.read(data, maxSize); + if (!buffer.isEmpty()) { + if (maxSize > INT_MAX) + maxSize = INT_MAX; + return buffer.read(data, int(maxSize)); + } return 0; } @@ -779,7 +803,7 @@ void QBluetoothSocketPrivateWinRT::addToPendingData(const QVector &d m_pendingData.append(data); for (const QByteArray &newData : data) { char *writePointer = buffer.reserve(newData.length()); - memcpy(writePointer, newData.data(), newData.length()); + memcpy(writePointer, newData.data(), size_t(newData.length())); } locker.unlock(); emit q->readyRead(); @@ -795,19 +819,24 @@ HRESULT QBluetoothSocketPrivateWinRT::handleConnectOpFinished(ABI::Windows::Foun return S_OK; } - DWORD hr = action->GetResults(); + HRESULT hr = action->GetResults(); switch (hr) { - case 0x8007274c: // A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. + + // A connection attempt failed because the connected party did not properly respond after a + // period of time, or established connection failed because connected host has failed to respond. + case HRESULT_FROM_WIN32(WSAETIMEDOUT): errorString = QBluetoothSocket::tr("Connection timed out"); q->setSocketError(QBluetoothSocket::NetworkError); q->setSocketState(QBluetoothSocket::UnconnectedState); return S_OK; - case 0x80072751: // A socket operation was attempted to an unreachable host. + // A socket operation was attempted to an unreachable host. + case HRESULT_FROM_WIN32(WSAEHOSTUNREACH): errorString = QBluetoothSocket::tr("Host not reachable"); q->setSocketError(QBluetoothSocket::HostNotFoundError); q->setSocketState(QBluetoothSocket::UnconnectedState); return S_OK; - case 0x8007274d: // No connection could be made because the target machine actively refused it. + // No connection could be made because the target machine actively refused it. + case HRESULT_FROM_WIN32(WSAECONNREFUSED): errorString = QBluetoothSocket::tr("Host refused connection"); q->setSocketError(QBluetoothSocket::HostNotFoundError); q->setSocketState(QBluetoothSocket::UnconnectedState); @@ -832,8 +861,7 @@ HRESULT QBluetoothSocketPrivateWinRT::handleConnectOpFinished(ABI::Windows::Foun hr = info->Close(); Q_ASSERT_SUCCEEDED(hr); } - hr = m_connectOp.Reset(); - Q_ASSERT_SUCCEEDED(hr); + m_connectOp.Reset(); } q->setOpenMode(requestedOpenMode); -- cgit v1.2.3