QString: fix regression comparing null QString with null const char *

Commit 4a40c717f3 optimized
QString::compare_helper(QChar*, int, char*, int), but got
the case wrong where the rhs is null, but the lhs is empty,
not null (which is the case even with a null QString, as
QString().constData() != nullptr). The correct result in
this case is 0, since in Qt empty and null strings compare
equal.

Fix by checking the length of lhs, not its pointer.

Task-number: QTBUG-55154
Change-Id: I3ec2cd25d9bdca90cf3f5568a875b1e52c779979
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2016-08-05 16:01:39 +03:00 committed by Liang Qi
parent 4b63ab9a93
commit 009f1f2812
2 changed files with 15 additions and 1 deletions

View File

@ -5402,7 +5402,7 @@ int QString::compare_helper(const QChar *data1, int length1, const char *data2,
Qt::CaseSensitivity cs) Qt::CaseSensitivity cs)
{ {
if (!data2) if (!data2)
return int(bool(data1)); return length1;
if (Q_UNLIKELY(length2 < 0)) if (Q_UNLIKELY(length2 < 0))
length2 = int(strlen(data2)); length2 = int(strlen(data2));
// ### make me nothrow in all cases // ### make me nothrow in all cases

View File

@ -5012,6 +5012,12 @@ void tst_QString::operator_eqeq_nullstring()
QVERIFY( QString("") == "" ); QVERIFY( QString("") == "" );
QVERIFY( "" == QString("") ); QVERIFY( "" == QString("") );
QVERIFY(QString() == nullptr);
QVERIFY(nullptr == QString());
QVERIFY(QString("") == nullptr);
QVERIFY(nullptr == QString(""));
QVERIFY( QString().size() == 0 ); QVERIFY( QString().size() == 0 );
QVERIFY( QString("").size() == 0 ); QVERIFY( QString("").size() == 0 );
@ -5025,6 +5031,8 @@ void tst_QString::operator_smaller()
QString null; QString null;
QString empty(""); QString empty("");
QString foo("foo"); QString foo("foo");
const char *nullC = nullptr;
const char *emptyC = "";
QVERIFY( !(null < QString()) ); QVERIFY( !(null < QString()) );
QVERIFY( !(null > QString()) ); QVERIFY( !(null > QString()) );
@ -5035,6 +5043,12 @@ void tst_QString::operator_smaller()
QVERIFY( !(null < empty) ); QVERIFY( !(null < empty) );
QVERIFY( !(null > empty) ); QVERIFY( !(null > empty) );
QVERIFY( !(nullC < empty) );
QVERIFY( !(nullC > empty) );
QVERIFY( !(null < emptyC) );
QVERIFY( !(null > emptyC) );
QVERIFY( null < foo ); QVERIFY( null < foo );
QVERIFY( !(null > foo) ); QVERIFY( !(null > foo) );
QVERIFY( foo > null ); QVERIFY( foo > null );