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-09 17:16:24 +0000 |
commit | 84de1b10d242551a51b6c4ce5eda657c4743bc63 (patch) | |
tree | 443e91e9dcd75b159d0dd86c921c16b17e152b38 | |
parent | a2758cf594dd08a21037873f64f72166a353aa29 (diff) | |
download | qtserialport-84de1b10d242551a51b6c4ce5eda657c4743bc63.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.
Task-number: QTBUG-47127
Change-Id: I4d101b49bbb153bddbe535a920d616b5fef130e9
Reviewed-by: Sergey Belyashov <Sergey.Belyashov@gmail.com>
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 546d5e1..b3333fa 100644 --- a/src/serialport/qserialportinfo_win.cpp +++ b/src/serialport/qserialportinfo_win.cpp @@ -65,11 +65,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() |