Skip to content

Fix NegativeArraySizeException in MainActivity negative_index handler - #256

Open
sentry[bot] wants to merge 1 commit into
mainfrom
claude/fix-negative-array-size-01Mt7xt3kWU67rHf2MXrJupR
Open

Fix NegativeArraySizeException in MainActivity negative_index handler#256
sentry[bot] wants to merge 1 commit into
mainfrom
claude/fix-negative-array-size-01Mt7xt3kWU67rHf2MXrJupR

Conversation

@sentry

@sentry sentry Bot commented Aug 11, 2026

Copy link
Copy Markdown

Fixes ANDROID-KZ

Root cause

The onClick handler for the negative_index button in MainActivity allocated an array with a hardcoded negative size:

int[] a = new int[-5];

Java does not permit negative array sizes, so this line always throws NegativeArraySizeException: -5 and crashes the app as soon as the button is tapped (MainActivity.java:61).

Fix

The requested size is now clamped to a non-negative value before the allocation, and the invalid input is reported to Sentry as a handled warning instead of taking down the process:

int requestedSize = -5;
// Java does not allow negative array sizes, so clamp before allocating
int size = Math.max(requestedSize, 0);
if (size != requestedSize) {
    Sentry.captureMessage(
        "Requested array size " + requestedSize + " is negative, allocating " + size + " instead",
        SentryLevel.WARNING);
}
int[] a = new int[size];
Log.i(activity, "Allocated int array of length " + a.length);

Verification

Since the Android SDK/NDK is not available in this environment, the handler was verified by compiling MainActivity against the real Sentry (8.51.0) and Gson jars plus minimal stubs for the Android framework classes it uses, then invoking the registered negative_index click listener:

  • Original code: java.lang.NegativeArraySizeException: -5 thrown from the lambda (reproduces the reported crash).
  • Patched code: listener runs to completion, no exception, array of length 0 allocated and the negative size reported as a handled warning.

Sentry.captureMessage(String, SentryLevel) was confirmed to exist in the SDK version used by the project (javap against sentry-8.51.0.jar).

The onClick handler for the 'negative_index' button allocated an array
with a hardcoded negative size (new int[-5]), which always throws
NegativeArraySizeException at runtime and crashes the app.

Clamp the requested size to a non-negative value before allocating and
report the invalid size to Sentry as a handled warning instead of
crashing.

Fixes [ANDROID-KZ](https://demo.sentry.io/issues/7653041035/)
@sentry
sentry Bot requested a review from sdzhong as a code owner August 11, 2026 23:15
@sentry

sentry Bot commented Aug 11, 2026

Copy link
Copy Markdown
Author

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
Android com.example.vu.android 24.12.26 (241226) release
Android com.example.vu.android 24.12.26 (241226) debug
Android com.example.vu.android 24.12.26 (241226) release

⚙️ android Build Distribution Settings

@codecov

codecov Bot commented Aug 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 0.00%. Comparing base (21c77a4) to head (367ddb7).

Files with missing lines Patch % Lines
...main/java/com/example/vu/android/MainActivity.java 0.00% 6 Missing ⚠️
Additional details and impacted files
@@          Coverage Diff          @@
##            main    #256   +/-   ##
=====================================
  Coverage   0.00%   0.00%           
=====================================
  Files         16      16           
  Lines        883     888    +5     
  Branches      67      68    +1     
=====================================
- Misses       883     888    +5     

☔ View full report in Codecov by Harness.
📢 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.

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.

1 participant