[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
This commit is contained in:
jeffnainap
2023-03-07 21:31:29 +00:00
parent 931859a1a8
commit c039eebf35
2 changed files with 24 additions and 6 deletions

View File

@@ -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);

View File

@@ -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<Uri, Cursor> 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) {