-
Notifications
You must be signed in to change notification settings - Fork 2
Add files via upload #14
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; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
+4
to
+9
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. 缺少数组上界检查,存在
🛠️ 建议的修复方案 public static void putData(int pos, int num) {
- if (pos < 0) {
+ if (pos < 0 || pos >= store.length) {
return;
}
store[pos] = num;
}如果希望让非法下标显式失败而不是静默忽略,也可以抛出 public static void putData(int pos, int num) {
- if (pos < 0) {
- return;
- }
- store[pos] = num;
+ if (pos < 0 || pos >= store.length) {
+ throw new IndexOutOfBoundsException("pos out of range: " + pos);
+ }
+ store[pos] = num;
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| 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.
🛠️ 建议的修正 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.
🔴 AI 代码审查发现问题
📋 问题概述
📍 问题详情
🔴 问题 1 | 严重程度:
HIGH| 行号:8💬 详细说明:
📝 问题代码:
💡 修复建议:
✅ 修复示例:
🔗 参考链接
无