java
runtime_error
ai_generated
true
java.lang.NegativeArraySizeException: -1
ID: java/negative-array-size-exception
90%修复率
82%置信度
1证据数
2023-08-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Java 8 | active | — | — | — |
| Java 11 | active | — | — | — |
| Java 17 | active | — | — | — |
| Java 21 | active | — | — | — |
根因分析
尝试创建大小为负数的数组,通常由整数溢出、大小计算错误或格式错误的输入数据引起。
English
An attempt to create an array with a negative size, typically caused by an integer overflow, a bug in size calculation, or malformed input data.
官方文档
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/NegativeArraySizeException.html解决方案
-
Validate the array size before creation: if (size < 0) throw new IllegalArgumentException("Size must be non-negative: " + size); -
Use Math.max(0, size) to clamp the size to a non-negative value as a temporary fix, but investigate the root cause.
无效尝试
常见但无效的做法:
-
90% 失败
The issue is not about memory limits but a negative value; increasing heap does not fix the logic error.
-
80% 失败
This masks the underlying bug, potentially leading to data corruption or silent failures.