From b4814163c78e396834ad54963073cf03449073db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Sun, 12 Mar 2023 19:29:10 +0100 Subject: [PATCH] QEdidParser: Fix parsing of string descriptors with length less than 13 The EDID 1.4 spec states that string descriptors with length less than 13 are terminated with \n and padded with spaces. Since we sanitize the string by replacing non-printable characters with '-', we can't start out by replacing \n and \r with \0 to simulate a zero-terminated string, as these null bytes will then be replaced with '-'. And even if they were not, QQtring::fromLatin1(QByteArray) in Qt 6 will include any null bytes verbatim as Unicode null characters, so we'd still end up with a wrongly parsed string. We simplify the process by following the spec, truncating the byte array at the first occurrence of \n (without any check that the following bytes are pure padding), and then doing the non-printable character replacement. Pick-to: 6.5 6.2 Change-Id: I9f4e77ddb7b1b759c08b94b29e28d8c148b5435f Reviewed-by: Oliver Wolff --- src/gui/util/qedidparser.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/gui/util/qedidparser.cpp b/src/gui/util/qedidparser.cpp index 9724fbc59c..4dae151e6a 100644 --- a/src/gui/util/qedidparser.cpp +++ b/src/gui/util/qedidparser.cpp @@ -235,16 +235,22 @@ QString QEdidParser::parseEdidString(const quint8 *data) { QByteArray buffer(reinterpret_cast(data), 13); - // Erase carriage return and line feed - buffer = buffer.replace('\r', '\0').replace('\n', '\0'); - - // Replace non-printable characters with dash for (int i = 0; i < buffer.size(); ++i) { + // If there are less than 13 characters in the string, the string + // is terminated with the ASCII code ‘0Ah’ (line feed) and padded + // with ASCII code ‘20h’ (space). See EDID 1.4, sections 3.10.3.1, + // 3.10.3.2, and 3.10.3.4. + if (buffer[i] == '\n') { + buffer.truncate(i); + break; + } + + // Replace non-printable characters with dash if (buffer[i] < '\040' || buffer[i] > '\176') buffer[i] = '-'; } - return QString::fromLatin1(buffer.trimmed()); + return QString::fromLatin1(buffer); } QT_END_NAMESPACE