Skip to content

Fix JNI crash when jBytes is null in SafeConvertToStdString#3254

Merged
domchen merged 1 commit intomainfrom
fix/android-jni-null-check
Feb 4, 2026
Merged

Fix JNI crash when jBytes is null in SafeConvertToStdString#3254
domchen merged 1 commit intomainfrom
fix/android-jni-null-check

Conversation

@kevingpqi123
Copy link
Collaborator

Problem

When calling SafeConvertToStdString, if String.getBytes() returns null (due to OOM or other JNI exceptions), the subsequent GetArrayLength(jBytes) call will crash with:

JNI DETECTED ERROR IN APPLICATION: java_array == null

Related issue: #3045

Solution

Add null check and exception handling for jBytes before calling GetArrayLength:

if (jBytes == nullptr || env->ExceptionCheck()) {
  env->ExceptionClear();
  return "";
}

Changes

  • Added null check for jBytes after CallObjectMethod
  • Added ExceptionCheck() to detect pending JNI exceptions
  • Call ExceptionClear() to clear any pending exception before returning

Add null check and exception handling for jBytes before calling
GetArrayLength to prevent crash when String.getBytes() returns null
due to OOM or other JNI exceptions.

Fixes: #3045
@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.11%. Comparing base (187bd07) to head (bf16908).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3254   +/-   ##
=======================================
  Coverage   77.10%   77.11%           
=======================================
  Files         415      415           
  Lines       22040    22040           
  Branches     6307     6307           
=======================================
+ Hits        16995    16997    +2     
+ Misses       3818     3817    -1     
+ Partials     1227     1226    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@domchen domchen merged commit adad2be into main Feb 4, 2026
9 checks passed
@domchen domchen deleted the fix/android-jni-null-check branch February 4, 2026 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants