Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make java UTF-8 decode more lenient #26886

Merged
merged 6 commits into from
May 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/lib/support/JniReferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,14 +402,45 @@ CHIP_ERROR JniReferences::CharToStringUTF(const chip::CharSpan & charSpan, jobje
jmethodID newDocoderMethod = env->GetMethodID(charSetClass, "newDecoder", "()Ljava/nio/charset/CharsetDecoder;");
jobject decoderObject = env->CallObjectMethod(charsetObject, newDocoderMethod);

// Even though spec requires UTF-8 strings, we have seen instances in the field of certified devices sending
// invalid strings like "startup?" (0x73 0x74 0x61 0x72 0x74 0x75 0x70 <0x91>) and we want to actually
// be lenient on those rather than failing an entire decode (which may fail an entire report for one invalid string,
// like in a very common 'subscribe *')
//
// As a result call:
// onMalformedInput(CodingErrorAction.REPLACE)
// onUnmappableCharacter(CodingErrorAction.REPLACE)
jclass codingErrorActionClass = env->FindClass("java/nio/charset/CodingErrorAction");
jobject replaceAction = env->GetStaticObjectField(
codingErrorActionClass, env->GetStaticFieldID(codingErrorActionClass, "REPLACE", "Ljava/nio/charset/CodingErrorAction;"));
{
jmethodID onMalformedInput = env->GetMethodID(charSetDocoderClass, "onMalformedInput",
"(Ljava/nio/charset/CodingErrorAction;)Ljava/nio/charset/CharsetDecoder;");
decoderObject = env->CallObjectMethod(decoderObject, onMalformedInput, replaceAction);
}
{
jmethodID onUnmappableCharacter =
env->GetMethodID(charSetDocoderClass, "onUnmappableCharacter",
"(Ljava/nio/charset/CodingErrorAction;)Ljava/nio/charset/CharsetDecoder;");
decoderObject = env->CallObjectMethod(decoderObject, onUnmappableCharacter, replaceAction);
}

jmethodID charSetDecodeMethod = env->GetMethodID(charSetDocoderClass, "decode", "(Ljava/nio/ByteBuffer;)Ljava/nio/CharBuffer;");
jobject decodeObject = env->CallObjectMethod(decoderObject, charSetDecodeMethod, jbyteBuffer);
env->DeleteLocalRef(jbyteBuffer);

// If decode exception occur, outStr will be set null.
outStr = nullptr;

VerifyOrReturnError(!env->ExceptionCheck(), CHIP_JNI_ERROR_EXCEPTION_THROWN);
if (env->ExceptionCheck())
{
// If there is an exception, decode will not fail. Instead just
// an error will be reported.
ChipLogError(Support, "Exception encountered trying to decode a UTF string.");
env->ExceptionDescribe();
env->ExceptionClear();
return CHIP_JNI_ERROR_EXCEPTION_THROWN;
}

jclass charBufferClass = env->FindClass("java/nio/CharBuffer");
jmethodID charBufferToString = env->GetMethodID(charBufferClass, "toString", "()Ljava/lang/String;");
Expand Down