From 7270e2ab7538b4943842674abd41d1f065c54275 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 17 Oct 2023 18:42:24 +0200 Subject: [PATCH] Fix QStringConverter::encodingForName() for trailing `-`, `_` The (internal) docs say that - and _ are ignored, and they're ignored everywhere, except as suffixes. If the old code only ignored them as infixes, fine, that would make some sense, but it ignored infixes and prefixes, so there's no reason for it to not ignore suffixes, too. Fix by continuing the loop until both input ranges are exhausted, or a mismatch was found. [ChangeLog][QtCore][QStringConverter] Fixed a bug where encodingForName() failed due to trailing characters (`_`, `-`) that ought to have been ignored. Pick-to: 6.5 Change-Id: Iec21489d988eda7d33c744c170f88cd665b73f34 Reviewed-by: Ivan Solovev (cherry picked from commit 5f775e671973b1549a48ae8c69c5db7494f6a6d3) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/text/qstringconverter.cpp | 21 +++++++------------ .../qstringconverter/tst_qstringconverter.cpp | 2 ++ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/corelib/text/qstringconverter.cpp b/src/corelib/text/qstringconverter.cpp index 65a365870cb..216ec91bf3f 100644 --- a/src/corelib/text/qstringconverter.cpp +++ b/src/corelib/text/qstringconverter.cpp @@ -1761,21 +1761,16 @@ const QStringConverter::Interface QStringConverter::encodingInterfaces[QStringCo // match names case insensitive and skipping '-' and '_' static bool nameMatch(const char *a, const char *b) { - while (*a && *b) { - if (*a == '-' || *a == '_') { + do { + while (*a == '-' || *a == '_') ++a; - continue; - } - if (*b == '-' || *b == '_') { + while (*b == '-' || *b == '_') ++b; - continue; - } - if (QtMiscUtils::toAsciiLower(*a) != QtMiscUtils::toAsciiLower(*b)) - return false; - ++a; - ++b; - } - return !*a && !*b; + if (!*a && !*b) // end of both strings + return true; + } while (QtMiscUtils::toAsciiLower(*a++) == QtMiscUtils::toAsciiLower(*b++)); + + return false; } diff --git a/tests/auto/corelib/text/qstringconverter/tst_qstringconverter.cpp b/tests/auto/corelib/text/qstringconverter/tst_qstringconverter.cpp index 79626e68874..2aa7616b191 100644 --- a/tests/auto/corelib/text/qstringconverter/tst_qstringconverter.cpp +++ b/tests/auto/corelib/text/qstringconverter/tst_qstringconverter.cpp @@ -2238,6 +2238,8 @@ void tst_QStringConverter::encodingForName_data() row("ISO8859-1", QStringConverter::Latin1); row("iso8859-1", QStringConverter::Latin1); row("latin1", QStringConverter::Latin1); + row("latin-1_-", QStringConverter::Latin1); + row("latin_1-_", QStringConverter::Latin1); row("-_latin-1", QStringConverter::Latin1); row("_-latin_1", QStringConverter::Latin1);