From 84de1b10d242551a51b6c4ce5eda657c4743bc63 Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Thu, 9 Jul 2015 16:09:36 +0300 Subject: 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 Reviewed-by: Denis Shienkov --- src/serialport/qserialportinfo_win.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/serialport/qserialportinfo_win.cpp b/src/serialport/qserialportinfo_win.cpp index 546d5e18..b3333fa6 100644 --- a/src/serialport/qserialportinfo_win.cpp +++ b/src/serialport/qserialportinfo_win.cpp @@ -65,11 +65,10 @@ static inline const QList& guidFlagsPairs() static QString toStringAndTrimNullCharacter(const QByteArray &buffer) { - QString result = QString::fromWCharArray(reinterpret_cast(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(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() -- cgit v1.2.3