From 367ddb75ecff6daf6e0aa5dd183cfea574c7afb8 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 11 Aug 2026 23:14:50 +0000 Subject: [PATCH] Fix NegativeArraySizeException in MainActivity negative_index handler 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/) --- .../java/com/example/vu/android/MainActivity.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/example/vu/android/MainActivity.java b/app/src/main/java/com/example/vu/android/MainActivity.java index 340a636..af44f56 100644 --- a/app/src/main/java/com/example/vu/android/MainActivity.java +++ b/app/src/main/java/com/example/vu/android/MainActivity.java @@ -53,12 +53,22 @@ protected void onCreate(Bundle savedInstanceState) { int t = 5 / 0; }); - // Unhandled - NegativeArraySizeException + // Handled - negative array size is reported instead of crashing Button negative_index_button = findViewById(R.id.negative_index); negative_index_button.setOnClickListener(view -> { addAttachment(false); Sentry.addBreadcrumb("Button for NegativeArraySizeException clicked..."); - int[] a = new int[-5]; + + 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); }); // Handled - ArrayIndexOutOfBoundsException