summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Shienkov <denis.shienkov@gmail.com>2014-07-10 16:46:28 +0400
committerDenis Shienkov <denis.shienkov@gmail.com>2014-08-01 20:55:31 +0200
commit72c6169b21793da91b3e652ae5f1e21c45c067d3 (patch)
tree7219f008538b773e209357ffbbd361c4f01f395f
parent5002109313f914674d20a2fac1c38ce5360fb67d (diff)
downloadqtserialport-72c6169b21793da91b3e652ae5f1e21c45c067d3.tar.gz
Pass a length of string to QString::fromWCharArray
Using of the QString::fromWCharArray() we didn't pass length of a string, hoping that all WCHAR strings are terminated by the null-character. But it can lead to the wrong results if the initial WCHAR string has no null character. Therefore it is reasonable to pass length of a string to exclude an error. Besides, to do not break of the algorithm of string comparison it is necessary to return not null-terminated strings. Also now there is no need to initialize of some allocated arrays by zero values. Tested on Windows 7/8 with the on-board, the virtual com0com, the USB FTDI and the PL2303 serial ports, using Qt4 and then Qt5. Change-Id: I382cf8eacf4ab4d21c54de17fcdd6b9fcfa3d02c Reviewed-by: Konstantin Ritt <ritt.ks@gmail.com> Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
-rw-r--r--src/serialport/qserialportinfo_win.cpp23
1 files changed, 17 insertions, 6 deletions
diff --git a/src/serialport/qserialportinfo_win.cpp b/src/serialport/qserialportinfo_win.cpp
index 0e01ac7..8250d2a 100644
--- a/src/serialport/qserialportinfo_win.cpp
+++ b/src/serialport/qserialportinfo_win.cpp
@@ -71,6 +71,15 @@ static inline const QList<GuidFlagsPair>& guidFlagsPairs()
return guidFlagsPairList;
}
+static QString toStringAndTrimNullCharacter(const QByteArray &buffer)
+{
+ QString result = QString::fromWCharArray(reinterpret_cast<const wchar_t *>(buffer.constData()),
+ buffer.size() / sizeof(wchar_t));
+ while (!result.isEmpty() && (result.at(result.size() - 1).unicode() == 0))
+ result.chop(1);
+ return result;
+}
+
static QStringList portNamesFromHardwareDeviceMap()
{
HKEY hKey = 0;
@@ -80,7 +89,8 @@ static QStringList portNamesFromHardwareDeviceMap()
QStringList result;
DWORD index = 0;
static const DWORD maximumValueNameInChars = 16383;
- QByteArray outputValueName(maximumValueNameInChars * sizeof(wchar_t), 0);
+ QByteArray outputValueName;
+ outputValueName.resize(maximumValueNameInChars * sizeof(wchar_t));
QByteArray outputBuffer;
DWORD requiredDataBytes = 0;
forever {
@@ -90,7 +100,7 @@ static QStringList portNamesFromHardwareDeviceMap()
if (ret == ERROR_MORE_DATA) {
outputBuffer.resize(requiredDataBytes);
} else if (ret == ERROR_SUCCESS) {
- result.append(QString::fromWCharArray(reinterpret_cast<const wchar_t *>(outputBuffer.constData())));
+ result.append(toStringAndTrimNullCharacter(outputBuffer));
++index;
} else {
break;
@@ -120,7 +130,7 @@ static QString deviceRegistryProperty(HDEVINFO deviceInfoSet,
}
devicePropertyByteArray.resize(requiredSize);
}
- return QString::fromWCharArray(reinterpret_cast<const wchar_t *>(devicePropertyByteArray.constData()));
+ return toStringAndTrimNullCharacter(devicePropertyByteArray);
}
static QString deviceInstanceIdentifier(DEVINST deviceInstanceNumber)
@@ -130,12 +140,13 @@ static QString deviceInstanceIdentifier(DEVINST deviceInstanceNumber)
return QString();
// The size does not include the terminating null character.
++numberOfChars;
- QByteArray outputBuffer(numberOfChars * sizeof(wchar_t), 0);
+ QByteArray outputBuffer;
+ outputBuffer.resize(numberOfChars * sizeof(wchar_t));
if (::CM_Get_Device_ID(deviceInstanceNumber, reinterpret_cast<wchar_t *>(outputBuffer.data()),
outputBuffer.size(), 0) != CR_SUCCESS) {
return QString();
}
- return QString::fromWCharArray(reinterpret_cast<const wchar_t *>(outputBuffer.constData()));
+ return toStringAndTrimNullCharacter(outputBuffer);
}
static DEVINST parentDeviceInstanceNumber(DEVINST childDeviceInstanceNumber)
@@ -176,7 +187,7 @@ static QString devicePortName(HDEVINFO deviceInfoSet, PSP_DEVINFO_DATA deviceInf
continue;
} else if (ret == ERROR_SUCCESS) {
if (dataType == REG_SZ)
- portName = QString::fromWCharArray((reinterpret_cast<const wchar_t *>(outputBuffer.constData())));
+ portName = toStringAndTrimNullCharacter(outputBuffer);
else if (dataType == REG_DWORD)
portName = QStringLiteral("COM%1").arg(*(PDWORD(outputBuffer.constData())));
}