Skip to content

Commit

Permalink
[Android] Fix to avoid NewStringUTF crash (#25936)
Browse files Browse the repository at this point in the history
* Fix to avoid NewStringUTF crash

* restyle

* Modify comments

* Modify error check, add comments

* restyle

* Update CharToStringUTF input parameter, add errorCode return

* restyle

* Add String decode fail log

* Add to set null
  • Loading branch information
joonhaengHeo authored and pull[bot] committed Aug 9, 2023
1 parent b62492b commit db5bf85
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 222 deletions.
2 changes: 1 addition & 1 deletion src/controller/java/templates/partials/decode_value.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ if ({{source}}.IsNull()) {
env->SetByteArrayRegion({{target}}ByteArray, 0, static_cast<jsize>({{source}}.size()), reinterpret_cast<const jbyte *>({{source}}.data()));
{{target}} = {{target}}ByteArray;
{{else if (isCharString type)}}
{{target}} = env->NewStringUTF(std::string({{source}}.data(), {{source}}.size()).c_str());
LogErrorOnFailure(chip::JniReferences::GetInstance().CharToStringUTF({{source}}, {{target}}));
{{else}}
std::string {{target}}ClassName = "{{asJniClassName type null cluster}}";
std::string {{target}}CtorSignature = "({{asJniSignature type null cluster false}})V";
Expand Down
218 changes: 104 additions & 114 deletions src/controller/java/zap-generated/CHIPAttributeTLVValueDecoder.cpp

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 43 additions & 56 deletions src/controller/java/zap-generated/CHIPInvokeCallbacks.cpp

Large diffs are not rendered by default.

90 changes: 43 additions & 47 deletions src/controller/java/zap-generated/CHIPReadCallbacks.cpp

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions src/lib/support/JniReferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,34 @@ CHIP_ERROR JniReferences::GetObjectField(jobject objectToRead, const char * name
return err;
}

CHIP_ERROR JniReferences::CharToStringUTF(const chip::CharSpan & charSpan, jobject & outStr)
{
JNIEnv * env = GetEnvForCurrentThread();
jobject jbyteBuffer = env->NewDirectByteBuffer((void *) charSpan.data(), static_cast<jlong>(charSpan.size()));

jclass charSetClass = env->FindClass("java/nio/charset/Charset");
jmethodID charsetForNameMethod =
env->GetStaticMethodID(charSetClass, "forName", "(Ljava/lang/String;)Ljava/nio/charset/Charset;");
jobject charsetObject = env->CallStaticObjectMethod(charSetClass, charsetForNameMethod, env->NewStringUTF("UTF-8"));

jclass charSetDocoderClass = env->FindClass("java/nio/charset/CharsetDecoder");
jmethodID newDocoderMethod = env->GetMethodID(charSetClass, "newDecoder", "()Ljava/nio/charset/CharsetDecoder;");
jobject decoderObject = env->CallObjectMethod(charsetObject, newDocoderMethod);

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);

jclass charBufferClass = env->FindClass("java/nio/CharBuffer");
jmethodID charBufferToString = env->GetMethodID(charBufferClass, "toString", "()Ljava/lang/String;");
outStr = static_cast<jstring>(env->CallObjectMethod(decodeObject, charBufferToString));

return CHIP_NO_ERROR;
}

} // namespace chip
10 changes: 10 additions & 0 deletions src/lib/support/JniReferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <jni.h>
#include <lib/core/CHIPError.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/Span.h>
#include <lib/support/TypeTraits.h>
#include <string>

Expand Down Expand Up @@ -160,6 +161,15 @@ class JniReferences
return CreateBoxedObject(boxedTypeClsName, constructorSignature, chip::to_underlying(value), outObj);
}

/**
* Use instead of 'NewStringUTF' function
* If the value is not decoded with "UTF-8", the error will be returned.
* (The NewStringUTF function crashes when the value can not decoded as "UTF-8".)
*
* Creates a java string type based on char array.
*/
CHIP_ERROR CharToStringUTF(const chip::CharSpan & charSpan, jobject & outString);

private:
JniReferences() {}

Expand Down

0 comments on commit db5bf85

Please sign in to comment.