Fix wrong index access in getSingleMessageFromIcc()
SmsManager#getMessagesFromIcc() returns a list containing only valid
messages in the ICC. To get single message in the ICC by an ICC index,
the index value in SmsMessage objects in the list should be checked.
Bug: 161478651
Test: Manual
Signed-off-by: Taesu Lee <taesu82.lee@samsung.com>
Change-Id: Ifaf5d5284633629d91d130823469d19b4ae0efb0
diff --git a/src/com/android/providers/telephony/SmsProvider.java b/src/com/android/providers/telephony/SmsProvider.java
index 99e9ddd..d378c64 100644
--- a/src/com/android/providers/telephony/SmsProvider.java
+++ b/src/com/android/providers/telephony/SmsProvider.java
@@ -84,7 +84,7 @@
"body", // getDisplayMessageBody
"date", // getTimestampMillis
"status", // getStatusOnIcc
- "index_on_icc", // getIndexOnIcc
+ "index_on_icc", // getIndexOnIcc (1-based index)
"is_status_report", // isStatusReportMessage
"transport_type", // Always "sms".
"type", // depend on getStatusOnIcc
@@ -402,7 +402,7 @@
* Gets single message from the ICC for a subscription ID.
*
* @param subId the subscription ID.
- * @param messageIndex the message index of the messaage in the ICC.
+ * @param messageIndex the message index of the messaage in the ICC (1-based index).
* @return a cursor containing just one message from the ICC for the subscription ID.
*/
private Cursor getSingleMessageFromIcc(int subId, int messageIndex) {
@@ -415,19 +415,24 @@
// Use phone app permissions to avoid UID mismatch in AppOpsManager.noteOp() call.
long token = Binder.clearCallingIdentity();
try {
+ // getMessagesFromIcc() returns a zero-based list of valid messages in the ICC.
messages = smsManager.getMessagesFromIcc();
} finally {
Binder.restoreCallingIdentity(token);
}
- final SmsMessage message = messages.get(messageIndex);
- if (message == null) {
- throw new IllegalArgumentException(
- "No message in index " + messageIndex + " for subId " + subId);
+ final int count = messages.size();
+ for (int i = 0; i < count; i++) {
+ SmsMessage message = messages.get(i);
+ if (message != null && message.getIndexOnIcc() == messageIndex) {
+ MatrixCursor cursor = new MatrixCursor(ICC_COLUMNS, 1);
+ cursor.addRow(convertIccToSms(message, 0));
+ return cursor;
+ }
}
- MatrixCursor cursor = new MatrixCursor(ICC_COLUMNS, 1);
- cursor.addRow(convertIccToSms(message, 0));
- return cursor;
+
+ throw new IllegalArgumentException(
+ "No message in index " + messageIndex + " for subId " + subId);
}
/**
@@ -446,6 +451,7 @@
// Use phone app permissions to avoid UID mismatch in AppOpsManager.noteOp() call
long token = Binder.clearCallingIdentity();
try {
+ // getMessagesFromIcc() returns a zero-based list of valid messages in the ICC.
messages = smsManager.getMessagesFromIcc();
} finally {
Binder.restoreCallingIdentity(token);
@@ -975,7 +981,7 @@
* Deletes the message at index from the ICC for a subscription ID.
*
* @param subId the subscription ID.
- * @param messageIndex the message index of the message in the ICC.
+ * @param messageIndex the message index of the message in the ICC (1-based index).
* @return true for succeess. Otherwise false.
*/
private boolean deleteMessageFromIcc(int subId, int messageIndex) {
@@ -1010,6 +1016,7 @@
try {
int deletedCnt = 0;
int maxIndex = smsManager.getSmsCapacityOnIcc();
+ // messageIndex is 1-based index of the message in the ICC.
for (int messageIndex = 1; messageIndex <= maxIndex; messageIndex++) {
if (smsManager.deleteMessageFromIcc(messageIndex)) {
deletedCnt++;