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 (4) 时,line 8 的 store[pos] 会触发 ArrayIndexOutOfBoundsException。

📍 问题详情

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

💬 详细说明:

  • 任何调用 putData 且传入 pos >= 4 的场景都会导致程序崩溃。虽然 main 函数中故意传入了 5 用于演示,但在实际业务逻辑中若未做上限校验,外部可控输入可导致服务不可用(DoS)。

📝 问题代码:

        store[pos] = num;

💡 修复建议:

在赋值前增加上界校验,确保 pos 在 [0, store.length) 范围内。

✅ 修复示例:

    public static void putData(int pos, int num) {
        if (pos < 0 || pos >= store.length) {
            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

缺少数组上界检查,存在 ArrayIndexOutOfBoundsException 风险

putData 仅校验了 pos < 0,未校验 pos >= store.length。当调用方传入 pos >= 4(如 main 中的 putData(5, 100))时,store[pos] = num; 会在运行时抛出 ArrayIndexOutOfBoundsException,导致程序崩溃。这是典型的边界检查绕过缺陷,应对上下界同时进行校验。

🛠️ 建议的修复方案
     public static void putData(int pos, int num) {
-        if (pos < 0) {
+        if (pos < 0 || pos >= store.length) {
             return;
         }
         store[pos] = num;
     }

如果希望让非法下标显式失败而不是静默忽略,也可以抛出 IndexOutOfBoundsException

     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

‼️ 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
Verify each finding against the current code and only fix it if needed.

In `@BadArray.java` around lines 4 - 9, The putData method currently only checks
pos < 0 and writes to store[pos] causing ArrayIndexOutOfBoundsException for pos
>= store.length; update putData to validate both bounds (pos >= 0 && pos <
store.length) before assigning to store, and choose either to return on invalid
index or throw a clear IndexOutOfBoundsException with a descriptive message;
reference the putData method and the store and store.length symbols when making
the change.


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 | 🟠 Major

main 中传入的下标 5 超出数组长度 4,必然触发越界异常

store 长度为 4(有效下标 0–3),此处 putData(5, 100) 在修复 putData 的上界检查之前会直接抛出 ArrayIndexOutOfBoundsException;即使按上一个评论补上上界校验,该调用也会被静默丢弃,无法达到示例目的。请确认该入口是否是有意的漏洞演示样例——若是,建议添加注释说明;若不是,请修正下标。

🛠️ 建议的修正
     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
Verify each finding against the current code and only fix it if needed.

In `@BadArray.java` around lines 11 - 13, The call in main passes index 5 to
putData while the array store has length 4 (valid indices 0–3), causing
ArrayIndexOutOfBoundsException; either change the argument in main to a valid
index (e.g., 0–3) so putData(storeIndex, value) exercises the function
correctly, or if the out‑of‑bounds call is an intentional demo of a
vulnerability, add an explicit comment in main stating this is deliberate and
keep the call; refer to main, putData and store when making the change.

}