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 <oliver.wolff@qt.io>
bb10
Tor Arne Vestbø 2023-03-12 19:29:10 +01:00
parent 13951b44cd
commit b4814163c7
1 changed files with 11 additions and 5 deletions

View File

@ -235,16 +235,22 @@ QString QEdidParser::parseEdidString(const quint8 *data)
{
QByteArray buffer(reinterpret_cast<const char *>(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