Skip to content
Open
Changes from all commits
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
14 changes: 14 additions & 0 deletions BadArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class BadArray {
private static int[] store = new int[4];

public static void putData(int pos, int num) {
if (pos < 0) {
return;
}
store[pos] = num;
}
Comment on lines +4 to +9

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Missing upper bound check causes ArrayIndexOutOfBoundsException.

The method only validates against negative indices but doesn't check if pos exceeds the array bounds. When pos >= store.length (i.e., pos >= 4), line 8 will throw ArrayIndexOutOfBoundsException.

Apply this diff to add the missing upper bound validation:

 public static void putData(int pos, int num) {
-    if (pos < 0) {
+    if (pos < 0 || pos >= store.length) {
         return;
     }
     store[pos] = num;
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public static void putData(int pos, int num) {
if (pos < 0) {
return;
}
store[pos] = num;
}
public static void putData(int pos, int num) {
if (pos < 0 || pos >= store.length) {
return;
}
store[pos] = num;
}
🤖 Prompt for AI Agents
In BadArray.java around lines 4 to 9, the method validates only negative indices
and misses an upper bound check, causing ArrayIndexOutOfBoundsException when pos
>= store.length; add a guard that checks if pos is greater than or equal to
store.length (e.g., if (pos >= store.length) return; or throw an appropriate
exception) before writing to store[pos] so out-of-range indices are handled
safely.


public static void main(String[] args) {
putData(5, 100);
}
Comment on lines +11 to +13

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Array index out of bounds: attempting to access index 5 in array of size 4.

The call to putData(5, 100) will throw ArrayIndexOutOfBoundsException because store has size 4 (valid indices: 0-3), but the code attempts to write to index 5.

Apply this diff to use a valid index:

 public static void main(String[] args) {
-    putData(5, 100);
+    putData(3, 100);
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public static void main(String[] args) {
putData(5, 100);
}
public static void main(String[] args) {
putData(3, 100);
}
🤖 Prompt for AI Agents
In BadArray.java around lines 11 to 13, the call putData(5, 100) writes to index
5 while the underlying array has length 4 (valid indices 0–3), causing an
ArrayIndexOutOfBoundsException; change the call to use a valid index (for
example putData(3, 100)) or otherwise ensure the index is within bounds
(validate the index before calling or increase the array size) so the write does
not exceed the array length.

}