Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixes

- Handle native NDK init failure by checking return code and throwing an IllegalStateException to disable NDK integration early. This prevents the SDK from assuming a successful NDK initialization when the underlying native init failed.

### Dependencies

- Bump Native SDK from v0.11.2 to v0.11.3 ([#4810](https://github.com/getsentry/sentry-java/pull/4810))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class NdkIntegrationTest {
assertFalse(options.isEnableScopeSync)
}


@Test
fun `NdkIntegration won't init if cache dir is null`() {
val integration = fixture.getSut()
Expand Down Expand Up @@ -179,4 +180,5 @@ class NdkIntegrationTest {
@JvmStatic fun init(options: SentryAndroidOptions) {}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ public static void init(@NotNull final SentryAndroidOptions options) {
}

//noinspection UnstableApiUsage
io.sentry.ndk.SentryNdk.init(ndkOptions);
final int initResult = io.sentry.ndk.SentryNdk.init(ndkOptions);
if (initResult != 0) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: SentryNdk.init mock returns null, causing NullPointerException when auto-unboxed to int in tests.
Severity: HIGH | Confidence: 1.00

🔍 Detailed Analysis

The SentryNdk.init method is mocked using doAnswer in SentryNdkTest.kt. The production code expects SentryNdk.init to return an int, but the doAnswer lambda does not explicitly return a value. Mockito defaults to returning null in this scenario. When the code attempts to assign this null to final int initResult, Java's auto-unboxing mechanism triggers a NullPointerException. This prevents the tests from executing successfully.

💡 Suggested Fix

Modify the doAnswer lambda for io.sentry.ndk.SentryNdk.init in SentryNdkTest.kt to explicitly return an int value, such as 0, to match the expected return type.

🤖 Prompt for AI Agent
Fix this bug. In sentry-android-ndk/src/main/java/io/sentry/android/ndk/SentryNdk.java
at line 78: The `SentryNdk.init` method is mocked using `doAnswer` in
`SentryNdkTest.kt`. The production code expects `SentryNdk.init` to return an `int`, but
the `doAnswer` lambda does not explicitly return a value. Mockito defaults to returning
`null` in this scenario. When the code attempts to assign this `null` to `final int
initResult`, Java's auto-unboxing mechanism triggers a `NullPointerException`. This
prevents the tests from executing successfully.

Did we get this right? 👍 / 👎 to inform future reviews.

throw new IllegalStateException(
"Failed to initialize Sentry NDK. Native init returned code " + initResult);
}

// only add scope sync observer if the scope sync is enabled.
if (options.isEnableScopeSync()) {
Expand Down