From 7af0a91e851eae8a94daa96029f9f3e32b457ece Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claus=20R=C3=B8rbech?= Date: Wed, 3 Jul 2024 11:57:40 +0200 Subject: [PATCH] Guard encoding issues when core logs invalid utf-8 messages --- CHANGELOG.md | 1 + packages/cinterop/src/jvm/jni/utils.cpp | 7 +++---- .../jni-swig-stub/src/main/jni/realm_api_helpers.cpp | 10 +++++++++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4828478bd7..8e11806db1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * None. ### Fixed +* Fix crashes when core tries to log invalid utf-8 messages. (Issue [#1760](https://github.com/realm/realm-kotlin/issues/1760) [RKOTLIN-1089](https://jira.mongodb.org/browse/RKOTLIN-1089)). * [Sync] Fatal sync exceptions are now thrown as `UnrecoverableSyncException`. (Issue [#1767](https://github.com/realm/realm-kotlin/issues/1767) [RKOTLIN-1096](https://jira.mongodb.org/browse/RKOTLIN-1096)). * [Sync] Fix `NullPointerException` in `SubscriptionSet.waitForSynchronization`. (Issue [#1777](https://github.com/realm/realm-kotlin/issues/1777) [RKOTLIN-1102](https://jira.mongodb.org/browse/RKOTLIN-1102)). diff --git a/packages/cinterop/src/jvm/jni/utils.cpp b/packages/cinterop/src/jvm/jni/utils.cpp index 50cb51b80a..9ff5a234cb 100644 --- a/packages/cinterop/src/jvm/jni/utils.cpp +++ b/packages/cinterop/src/jvm/jni/utils.cpp @@ -82,13 +82,12 @@ static std::string string_to_hex(const std::string& message, realm::StringData& ret << "error_code = " << error_code << "; "; ret << "retcode = " << retcode << "; "; ret << "StringData.size = " << str.size() << "; "; - ret << "StringData.data = " << str << "; "; - ret << "StringData as hex = "; + ret << "StringData as hex ="; for (std::string::size_type i = 0; i < str.size(); ++i) ret << " 0x" << std::hex << std::setfill('0') << std::setw(2) << (int)s[i]; ret << "; "; - ret << "in_begin = " << in_begin << "; "; - ret << "in_end = " << in_end << "; "; + ret << "in_begin = " << (void*) in_begin << "; "; + ret << "in_end = " << (void*) in_end << "; "; ret << "out_curr = " << out_curr << "; "; ret << "out_end = " << out_end << ";"; return ret.str(); diff --git a/packages/jni-swig-stub/src/main/jni/realm_api_helpers.cpp b/packages/jni-swig-stub/src/main/jni/realm_api_helpers.cpp index c62c0757b2..240babf5f7 100644 --- a/packages/jni-swig-stub/src/main/jni/realm_api_helpers.cpp +++ b/packages/jni-swig-stub/src/main/jni/realm_api_helpers.cpp @@ -973,7 +973,15 @@ realm_set_log_callback([](void *userdata, const char *category, realm_log_level_ "(SLjava/lang/String;Ljava/lang/String;)V"); push_local_frame(jenv, 2); - jenv->CallVoidMethod(log_callback, log_method, java_level, to_jstring(jenv, category), to_jstring(jenv, message)); + jstring j_message = NULL; + try { + j_message = to_jstring(jenv, message); + } catch (RuntimeError exception) { + std::ostringstream ret; + ret << "Invalid data: " << exception.reason(); + j_message = to_jstring(jenv, ret.str()); + } + jenv->CallVoidMethod(log_callback, log_method, java_level, to_jstring(jenv, category), j_message); jni_check_exception(jenv); jenv->PopLocalFrame(NULL); },