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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 AI 代码审查发现问题

📋 问题概述

数组索引校验不完整导致越界写入
line 5-7 仅检查了 pos < 0 的情况并返回,但未检查 pos >= store.length 的上限。当传入 pos = 5(如 line 12 所示)时,程序会尝试访问 store[5],而数组长度仅为 4(line 2),导致 ArrayIndexOutOfBoundsException。

📍 问题详情

🔴 问题 1 | 严重程度: HIGH | 行号: 8

💬 详细说明:

  • 攻击者或错误输入可触发运行时异常,导致服务崩溃或不可用。在更复杂的场景中,若此类逻辑存在于关键业务路径,可能引发拒绝服务(DoS)。

📝 问题代码:

        store[pos] = num;

💡 修复建议:

在写入数组前增加上限校验,确保索引在 [0, store.length) 范围内。

✅ 修复示例:

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

🔗 参考链接

}

public static void main(String[] args) {
putData(5, 100);
}
}