From 3c346b94096eb7e1eef7546723476732a3ac7990 Mon Sep 17 00:00:00 2001 From: Simon Redman Date: Fri, 7 Jun 2019 16:39:05 -0600 Subject: [PATCH] Block matching empty phone numbers --- smsapp/smshelper.cpp | 5 +++++ tests/testsmshelper.cpp | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/smsapp/smshelper.cpp b/smsapp/smshelper.cpp index 1ceb378f8..a20c0d720 100644 --- a/smsapp/smshelper.cpp +++ b/smsapp/smshelper.cpp @@ -28,6 +28,11 @@ Q_LOGGING_CATEGORY(KDECONNECT_SMS_SMSHELPER, "kdeconnect.sms.smshelper") bool SmsHelper::isPhoneNumberMatchCanonicalized(const QString& canonicalPhone1, const QString& canonicalPhone2) { + if (canonicalPhone1.isEmpty() || canonicalPhone2.isEmpty()) { + // The empty string is not a valid phone number so does not match anything + return false; + } + // To decide if a phone number matches: // 1. Are they similar lengths? If two numbers are very different, probably one is junk data and should be ignored // 2. Is one a superset of the other? Phone number digits get more specific the further towards the end of the string, diff --git a/tests/testsmshelper.cpp b/tests/testsmshelper.cpp index 56e6cb632..89a723fe3 100644 --- a/tests/testsmshelper.cpp +++ b/tests/testsmshelper.cpp @@ -48,6 +48,7 @@ private Q_SLOTS: void testDifferentPhoneNumbers1(); void testDifferentPhoneNumbers2(); void testAllZeros(); + void testEmptyInput(); }; /** @@ -254,5 +255,18 @@ void SmsHelperTest::testAllZeros() QCOMPARE(canonicalized.length(), zeros.length()); } +/** + * An empty string is not a valid phone number and should not match anything + */ +void SmsHelperTest::testEmptyInput() +{ + const QString& empty = QLatin1String(""); + const QString& shortCode = QLatin1String("44455"); + const QString& realNumber = QLatin1String("12223334444"); + + QVERIFY2(!SmsHelper::isPhoneNumberMatch(empty, shortCode), "The empty string matched a shortcode phone number"); + QVERIFY2(!SmsHelper::isPhoneNumberMatch(empty, realNumber), "The empty string matched a real phone number"); +} + QTEST_MAIN(SmsHelperTest); #include "testsmshelper.moc"