Make the URL Recode function to fix bad input in FullyDecoded mode too

So far, this function hasn't been used for input coming in from the
user, so it wasn't necessary. But we may want to do it, or we may
already be doing it accidentally somewhere that isn't triggering the
failed assertions during unit testing.

So let's be on the safe side and allow it. And test it too.

Change-Id: Ib63addd8da468ad6908278d07a4829f1bdc26a07
Reviewed-by: David Faure (KDE) <faure@kde.org>
This commit is contained in:
Thiago Macieira 2013-07-02 13:29:13 -07:00 committed by The Qt Project
parent b60cac3602
commit 3d77406e27
2 changed files with 21 additions and 6 deletions

View File

@ -573,6 +573,13 @@ static int decode(QString &appendTo, const ushort *begin, const ushort *end)
continue;
}
if (Q_UNLIKELY(end - input < 3 || !isHex(input[1]) || !isHex(input[2]))) {
// badly-encoded data
appendTo.resize(origSize + (end - begin));
memcpy(appendTo.begin() + origSize, begin, (end - begin) * sizeof(ushort));
return end - begin;
}
if (Q_UNLIKELY(!output)) {
// detach
appendTo.resize(origSize + (end - begin));
@ -582,9 +589,6 @@ static int decode(QString &appendTo, const ushort *begin, const ushort *end)
}
++input;
Q_ASSERT(input <= end - 2); // we need two characters
Q_ASSERT(isHex(input[0]));
Q_ASSERT(isHex(input[1]));
*output++ = decodeNibble(input[0]) << 4 | decodeNibble(input[1]);
input += 2;
}
@ -635,6 +639,9 @@ static void maskTable(uchar (&table)[N], const uchar (&mask)[N])
handled. It consists of a sequence of 16-bit values, where the low 8 bits
indicate the character in question and the high 8 bits are either \c
EncodeCharacter, \c LeaveCharacter or \c DecodeCharacter.
This function corrects percent-encoded errors by interpreting every '%' as
meaning "%25" (all percents in the same content).
*/
Q_AUTOTEST_EXPORT int

View File

@ -824,12 +824,20 @@ void tst_QUrlInternal::correctEncodedMistakes()
QFETCH(QString, expected);
// prepend some data to be sure that it remains there
QString output = QTest::currentDataTag();
expected.prepend(output);
QString dataTag = QTest::currentDataTag();
QString output = dataTag;
if (!qt_urlRecode(output, input.constData(), input.constData() + input.length(), 0))
output += input;
QCOMPARE(output, expected);
QCOMPARE(output, dataTag + expected);
// now try the full decode mode
output = dataTag;
QString expected2 = QUrl::fromPercentEncoding(expected.toLatin1());
if (!qt_urlRecode(output, input.constData(), input.constData() + input.length(), QUrl::FullyDecoded))
output += input;
QCOMPARE(output, dataTag + expected2);
}
static void addUtf8Data(const char *name, const char *data)