Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 649d6ef9 authored by Ta-wei Yen's avatar Ta-wei Yen
Browse files

Fix VisualVoicemailSmsFilter NPE on invalid SmsMessage

Certain PDU will cause createFromPdu() to return a SmsMessage with null
mWrappedSmsMessage, throwing NPE on any method called. In this case, just
ignore the message.

Bug:29123941
Change-Id: Id649af4b7a7a9f5d7b7a08d007b619a4c98f81cb
parent fddcc60b
Loading
Loading
Loading
Loading
+12 −2
Original line number Diff line number Diff line
@@ -60,7 +60,9 @@ public class VisualVoicemailSmsFilter {
        // TODO: filter base on originating number and destination port.

        String messageBody = getFullMessage(pdus, format);

        if(messageBody == null){
            return false;
        }
        String clientPrefix = settings.clientPrefix;

        WrappedMessageData messageData = VisualVoicemailSmsParser.parse(clientPrefix, messageBody);
@@ -81,7 +83,15 @@ public class VisualVoicemailSmsFilter {
    private static String getFullMessage(byte[][] pdus, String format) {
        StringBuilder builder = new StringBuilder();
        for (byte pdu[] : pdus) {
            String body = SmsMessage.createFromPdu(pdu, format).getMessageBody();
            SmsMessage message =SmsMessage.createFromPdu(pdu, format);

            if(message == null || message.mWrappedSmsMessage == null) {
                // b/29123941 Certain PDU will cause createFromPdu() to return a SmsMessage with
                // null mWrappedSmsMessage, throwing NPE on any method called. In this case, just
                // ignore the message.
                return null;
            }
            String body = message.getMessageBody();
            if (body != null) {
                builder.append(body);
            }
+49 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.internal.telephony;

import android.content.Context;
import android.telephony.TelephonyManager;
import android.telephony.VisualVoicemailSmsFilterSettings;

import junit.framework.TestCase;

import org.mockito.Mockito;

public class VisualVoicemailSmsFilterTest extends TestCase {

    public void testUnsupportedPdu() {
        Context context = Mockito.mock(Context.class);
        TelephonyManager telephonyManager = Mockito.mock(TelephonyManager.class);
        Mockito.when(context.getSystemServiceName(TelephonyManager.class))
                .thenReturn(Context.TELEPHONY_SERVICE);
        Mockito.when(context.getSystemService(Mockito.anyString())).thenReturn(telephonyManager);

        VisualVoicemailSmsFilterSettings settings = new VisualVoicemailSmsFilterSettings.Builder()
                .build();

        Mockito.when(telephonyManager
                .getVisualVoicemailSmsFilterSettings(Mockito.anyString(), Mockito.anyInt()))
                .thenReturn(settings);

        byte[][] pdus = {
                ("MBOXUPDATE?m=11;server=example.com;"
                        + "port=143;name=1234567890@example.com;pw=CphQJKnYS4jEiDO").getBytes()};
        VisualVoicemailSmsFilter.filter(context, pdus, SmsConstants.FORMAT_3GPP2, 0, 0);
    }

}