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

Commit 45bf5774 authored by Andrew Cheng's avatar Andrew Cheng Committed by Gerrit Code Review
Browse files

Merge "NPE from bMessage MIME parsing crashes BT"

parents 61085671 23ce5469
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -744,6 +744,12 @@ public class BluetoothMapbMessageMime extends BluetoothMapbMessage {
    }

    private void parseMime(String message) {
        // Check for null String, otherwise NPE will cause BT to crash
        if (message == null) {
            Log.e(TAG, "parseMime called with a NULL message, terminating early");
            return;
        }

        /* Overall strategy for decoding:
         * 1) split on first empty line to extract the header
         * 2) unfold and parse headers
@@ -755,6 +761,7 @@ public class BluetoothMapbMessageMime extends BluetoothMapbMessage {
        String[] mimeParts;
        String remaining = null;
        String messageBody = null;

        message = message.replaceAll("\\r\\n[ \\\t]+", ""); // Unfold
        messageParts = message.split("\r\n\r\n", 2); // Split the header from the body
        if (messageParts.length != 2) {
+5 −2
Original line number Diff line number Diff line
@@ -289,9 +289,12 @@ class BmessageParser {
         * non-text content. If the charset is not set to UTF-8, it is safe to set the message as
         * empty. We force the getMessage (see Client) to only call getMessage with
         * UTF-8 as the MCE is not obliged to support native charset.
         *
         * 2020-06-01: we could now expect MMS to be more than text, e.g., image-only, so charset
         * not always UTF-8, downgrading log message from ERROR to DEBUG.
         */
        if (!"UTF-8".equals(mBmsg.mBbodyCharset)) {
            Log.e(TAG, "The charset was not set to charset UTF-8: " + mBmsg.mBbodyCharset);
            Log.d(TAG, "The charset was not set to charset UTF-8: " + mBmsg.mBbodyCharset);
        }

        /*
@@ -325,7 +328,7 @@ class BmessageParser {
                if ("UTF-8".equals(mBmsg.mBbodyCharset)) {
                    mBmsg.mMessage = new String(data, 0, messageLen, StandardCharsets.UTF_8);
                } else {
                    mBmsg.mMessage = null;
                    mBmsg.mMessage = new String(data, 0, messageLen);
                }
            } else {
                /* Handle possible exception for incorrect LENGTH value
+37 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.bluetooth.map;

import static org.mockito.Mockito.*;

import androidx.test.filters.MediumTest;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

@MediumTest
@RunWith(AndroidJUnit4.class)
public class BluetoothMapbMessageMimeTest {
    private static final String TAG = BluetoothMapbMessageMimeTest.class.getSimpleName();

    @Test
    public void testParseNullMsgPart_NoExceptionsThrown() {
        BluetoothMapbMessageMime bMessageMime = new BluetoothMapbMessageMime();
        bMessageMime.parseMsgPart(null);
    }
}