From c039eebf35580b88cb258332be9fb492437b93fb Mon Sep 17 00:00:00 2001 From: jeffnainap Date: Tue, 7 Mar 2023 21:31:29 +0000 Subject: [PATCH] [People Service] Catch potential `IllegalArgumentException` in the event of a malformed contacts URI We pass the contact URI inside shortcuts to query from the CP2 associate the shortcut with its corresponding CP2 contact. However, these URIs come from external apps and might potentially be malformed. In those cases, we catch the `IllegalArgumentException` to prevent the device from crashing. Test: unit tests Bug: 271230172 Change-Id: Iba2a504e10312cce8f32fc72d5010d4efe417b74 --- .../people/data/ContactsQueryHelper.java | 2 ++ .../people/data/ContactsQueryHelperTest.java | 28 +++++++++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/services/people/java/com/android/server/people/data/ContactsQueryHelper.java b/services/people/java/com/android/server/people/data/ContactsQueryHelper.java index 0993295e162f4..2505abf2d160b 100644 --- a/services/people/java/com/android/server/people/data/ContactsQueryHelper.java +++ b/services/people/java/com/android/server/people/data/ContactsQueryHelper.java @@ -152,6 +152,8 @@ class ContactsQueryHelper { } } catch (SQLiteException exception) { Slog.w("SQLite exception when querying contacts.", exception); + } catch (IllegalArgumentException exception) { + Slog.w("Illegal Argument exception when querying contacts.", exception); } if (found && lookupKey != null && hasPhoneNumber) { return queryPhoneNumber(lookupKey); diff --git a/services/tests/servicestests/src/com/android/server/people/data/ContactsQueryHelperTest.java b/services/tests/servicestests/src/com/android/server/people/data/ContactsQueryHelperTest.java index 299f15344dfa7..16a02b678511d 100644 --- a/services/tests/servicestests/src/com/android/server/people/data/ContactsQueryHelperTest.java +++ b/services/tests/servicestests/src/com/android/server/people/data/ContactsQueryHelperTest.java @@ -91,8 +91,16 @@ public final class ContactsQueryHelperTest { } @Test - public void testQueryException_returnsFalse() { - contentProvider.setThrowException(true); + public void testQuerySQLiteException_returnsFalse() { + contentProvider.setThrowSQLiteException(true); + + Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, CONTACT_LOOKUP_KEY); + assertFalse(mHelper.query(contactUri.toString())); + } + + @Test + public void testQueryIllegalArgumentException_returnsFalse() { + contentProvider.setThrowIllegalArgumentException(true); Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, CONTACT_LOOKUP_KEY); assertFalse(mHelper.query(contactUri.toString())); @@ -178,14 +186,18 @@ public final class ContactsQueryHelperTest { private class ContactsContentProvider extends MockContentProvider { private Map mUriPrefixToCursorMap = new ArrayMap<>(); - private boolean throwException = false; + private boolean mThrowSQLiteException = false; + private boolean mThrowIllegalArgumentException = false; @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { - if (throwException) { + if (mThrowSQLiteException) { throw new SQLiteException(); } + if (mThrowIllegalArgumentException) { + throw new IllegalArgumentException(); + } for (Uri prefixUri : mUriPrefixToCursorMap.keySet()) { if (uri.isPathPrefixMatch(prefixUri)) { @@ -195,8 +207,12 @@ public final class ContactsQueryHelperTest { return mUriPrefixToCursorMap.get(uri); } - public void setThrowException(boolean throwException) { - this.throwException = throwException; + public void setThrowSQLiteException(boolean throwException) { + this.mThrowSQLiteException = throwException; + } + + public void setThrowIllegalArgumentException(boolean throwException) { + this.mThrowIllegalArgumentException = throwException; } private void registerCursor(Uri uriPrefix, Cursor cursor) {