{"record":{"id":"3ae013a020f78c52","repo":"chinabugotech/hutool","slug":"index-is-too-large-for-limit","errorCode":null,"errorMessage":"Index [{}] is too large for limit: [{}]","messagePattern":"Index \\[(.+?)\\] is too large for limit: \\[(.+?)\\]","errorType":"validation","errorClass":"ValidateException","httpStatus":null,"severity":"warning","filePath":"hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java","lineNumber":451,"sourceCode":"\t * @param <T>            元素类型\n\t * @param list           List列表\n\t * @param index          位置\n\t * @param element        新元素\n\t * @param paddingElement 填充的值\n\t * @param indexLimit     最大索引限制\n\t * @return 原List\n\t * @since 5.8.28\n\t */\n\tpublic static <T> List<T> setOrPadding(List<T> list, int index, T element, T paddingElement, int indexLimit) {\n\t\tAssert.notNull(list, \"List must be not null !\");\n\t\tfinal int size = list.size();\n\t\tif (index < size) {\n\t\t\tlist.set(index, element);\n\t\t} else {\n\t\t\tif (indexLimit > 0) {\n\t\t\t\t// issue#3286, 增加安全检查\n\t\t\t\tif (index > indexLimit) {\n\t\t\t\t\tthrow new ValidateException(\"Index [{}] is too large for limit: [{}]\", index, indexLimit);\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (int i = size; i < index; i++) {\n\t\t\t\tlist.add(paddingElement);\n\t\t\t}\n\t\t\tlist.add(element);\n\t\t}\n\t\treturn list;\n\t}\n\n\t/**\n\t * 截取集合的部分\n\t *\n\t * @param <T>   集合元素类型\n\t * @param list  被截取的数组\n\t * @param start 开始位置（包含）\n\t * @param end   结束位置（不包含）\n\t * @return 截取后的数组，当开始位置超过最大时，返回空的List","sourceCodeStart":433,"sourceCodeEnd":469,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java#L433-L469","documentation":"Thrown by ListUtil.setOrPadding() when the target index exceeds the specified indexLimit. This method sets an element at a given index, padding with paddingElement if the index is beyond the current list size. The indexLimit parameter (when > 0) acts as a safety cap to prevent unbounded list growth. If index > indexLimit, a ValidateException is thrown.","triggerScenarios":"Calling ListUtil.setOrPadding(list, index, element, paddingElement, indexLimit) where index >= list.size() AND indexLimit > 0 AND index > indexLimit. For example, setOrPadding(list, 1000, x, null, 100) throws because 1000 > 100. If indexLimit is 0 or negative, the limit check is skipped entirely.","commonSituations":"User-supplied index values that are unreasonably large. Configuring an indexLimit that is too small for the expected indices. Off-by-one errors in index calculation. This guard was added for issue #3286 as a safety check against unbounded padding.","solutions":["Validate the index is within the expected range before calling setOrPadding().","Increase the indexLimit to accommodate the expected maximum index.","Pass indexLimit <= 0 to disable the limit check entirely (use with caution).","Catch ValidateException and report the index as out of allowed range."],"exampleFix":"// before\nListUtil.setOrPadding(list, 1000, value, null, 100); // throws — 1000 > 100\n\n// after\nint maxIndex = 1000;\nif (index > maxIndex) throw new IllegalArgumentException(\"Index out of range: \" + index);\nListUtil.setOrPadding(list, index, value, null, maxIndex);","handlingStrategy":"validation","validationCode":"if (indexLimit > 0 && index > indexLimit) {\n    throw new IllegalArgumentException(\"Index \" + index + \" exceeds limit \" + indexLimit);\n}","typeGuard":null,"tryCatchPattern":"try {\n    ListUtil.setOrPadding(list, index, element, padding, indexLimit);\n} catch (ValidateException e) {\n    // index exceeds the configured limit\n    throw new IllegalArgumentException(\"Index out of allowed range\", e);\n}","preventionTips":["Validate index against the limit before calling setOrPadding().","Set indexLimit large enough for expected indices, or pass <= 0 to disable.","Clamp user-supplied indices to a safe maximum before use."],"tags":["list","padding","index-out-of-bounds","validation"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}