diff options
author | Denis Shienkov <denis.shienkov@gmail.com> | 2015-07-09 16:09:36 +0300 |
---|---|---|
committer | Denis Shienkov <denis.shienkov@gmail.com> | 2015-07-10 09:37:23 +0000 |
commit | 7844ad652c00b7730adb14b9035f2f9380bd3758 (patch) | |
tree | 59833b1bba1326a9c07ed3ed9420f9e1567af8e5 | |
parent | 3a3942f085e5aee712ccb2f80c41615e9a086c74 (diff) | |
download | qtserialport-7844ad652c00b7730adb14b9035f2f9380bd3758.tar.gz |
Fix getting duplicate serial ports with null characters
The function toStringAndTrimNullCharacter() deletes only trailing zeroes
from right to left direction up to the first non-zero character were meet.
This can return the wrong string in case there are the zero characters to
the left of the non-zero characters.
We should look for the first zero-character from left to right direction,
and discard all right part of a string, including the zero-character.
(cherry-picked from 84de1b10d242551a51b6c4ce5eda657c4743bc63)
Task-number: QTBUG-47127
Change-Id: I4d101b49bbb153bddbe535a920d616b5fef130e9
Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
-rw-r--r-- | src/serialport/qserialportinfo_win.cpp | 9 |
1 files changed, 4 insertions, 5 deletions
diff --git a/src/serialport/qserialportinfo_win.cpp b/src/serialport/qserialportinfo_win.cpp index caf70f4..d5e991b 100644 --- a/src/serialport/qserialportinfo_win.cpp +++ b/src/serialport/qserialportinfo_win.cpp @@ -73,11 +73,10 @@ static inline const QList<GuidFlagsPair>& guidFlagsPairs() 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; + const QString result = QString::fromWCharArray(reinterpret_cast<const wchar_t *>(buffer.constData()), + buffer.size() / sizeof(wchar_t)); + const int index = result.indexOf(QChar(0)); + return index == -1 ? result : result.mid(0, index); } static QStringList portNamesFromHardwareDeviceMap() |