sdl_java_suite icon indicating copy to clipboard operation
sdl_java_suite copied to clipboard

Mime NullPointerExceptions & ArrayIndexOutOfBoundsException

Open MikaylaRay44 opened this issue 10 years ago • 0 comments

com/smartdevicelink/trace/Mime

NullPointerException is thrown by the following unit tests on the Mime class.

String actualNullResult1 = Mime.base64Encode((byte[]) null); // Exception
String actualNullResult2 = Mime.base64Encode((String) null);
String actualNullResult3 = Mime.base64Encode(null, 0, 0);  // Exception

// Invalid/Null Tests
assertNull(Test.NULL, actualNullResult1);
assertNull(Test.NULL, actualNullResult2);
assertNull(Test.NULL, actualNullResult3);

The first test fails because it calls the third, which has no parameter checking.

However, the second test does not fail ...

public static String base64Encode(String str) {
    String b64String = "";
    try {
        byte[] strBytes = str.getBytes("US-ASCII");
        b64String = base64Encode(strBytes);
    } catch (Exception ex) {
        // Don't care?
    } 
    return b64String;
}

It does nothing with the exception that is caught. Instead of having the // Don't care? comment it should return that the encoding has failed via a null value.

Solution : To fix both occurrences of the exception some null-value parameter checking needs to be done.

The ArrayIndexOutOfBoundsException is thrown by the following test of the base64Encode(byte[], int, int) method.

String actualInvalidResult = Mime.base64Encode(testBytes, 35, 2); // Bytes,Offset,Length

Solution : It trusts that these values will be accurate and not conflict, minimal checking can be done to handle both issues by adding a try/catch block that returns null on either occurrence, logging the issue as well.

MikaylaRay44 avatar May 19 '15 13:05 MikaylaRay44