From a15724e4a29430d315ab060e0cab05bff09ff27b Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 17 Sep 2026 01:59:54 +0000 Subject: [PATCH] Guard against negative array size in MainActivity The 'negative_index' button allocated `new int[-5]`, which always threw an unhandled NegativeArraySizeException and crashed the app on tap. Validate the requested size before allocating and report the invalid size to Sentry as a handled event instead of crashing. Fixes [ANDROID-NG](https://demo.sentry.io/issues/7734837700/) --- .../java/com/example/vu/android/MainActivity.java | 11 +++++++++-- 1 file changed, 9 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 340a6369..c07e2e85 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,19 @@ protected void onCreate(Bundle savedInstanceState) { int t = 5 / 0; }); - // Unhandled - NegativeArraySizeException + // Handled - invalid array size is reported instead of crashing with NegativeArraySizeException 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; + if (requestedSize < 0) { + Sentry.captureException( + new IllegalArgumentException("Invalid array size requested: " + requestedSize)); + return; + } + int[] a = new int[requestedSize]; }); // Handled - ArrayIndexOutOfBoundsException