-
Notifications
You must be signed in to change notification settings - Fork 2
Add files via upload #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| public static void main(String[] args) { | ||||||||||||||
| putData(5, 100); | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Array index out of bounds: attempting to access index 5 in array of size 4. The call to Apply this diff to use a valid index: public static void main(String[] args) {
- putData(5, 100);
+ putData(3, 100);
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||
| } | ||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing upper bound check causes ArrayIndexOutOfBoundsException.
The method only validates against negative indices but doesn't check if
posexceeds the array bounds. Whenpos >= store.length(i.e.,pos >= 4), line 8 will throwArrayIndexOutOfBoundsException.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
🤖 Prompt for AI Agents