{"record":{"id":"b4ea0c516a0699c3","repo":"apache/druid","slug":"index-s-0-b4ea0c","errorCode":null,"errorMessage":"Index[%s] < 0","messagePattern":"Index\\[(.+?)\\] < 0","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/segment/data/Indexed.java","lineNumber":124,"sourceCode":"   */\n  default boolean isSorted()\n  {\n    return false;\n  }\n\n  /**\n   * Checks  if {@code index} is between 0 and {@code size}. Similar to Preconditions.checkElementIndex() except this\n   * method throws {@link IAE} with custom error message.\n   * <p>\n   * Used here to get existing behavior(same error message and exception) of V1 {@link GenericIndexed}.\n   *\n   * @param index identifying an element of an {@link Indexed}\n   * @param size size of the {@link Indexed}\n   */\n  static void checkIndex(int index, int size)\n  {\n    if (index < 0) {\n      throw new IAE(\"Index[%s] < 0\", index);\n    }\n    if (index >= size) {\n      throw new IAE(\"Index[%d] >= size[%d]\", index, size);\n    }\n  }\n}\n","sourceCodeStart":106,"sourceCodeEnd":131,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/segment/data/Indexed.java#L106-L131","documentation":"Indexed.checkIndex(int index, int size) is the shared static bounds validator for Indexed implementations: negative indexes throw IAE 'Index[i] < 0'. Like the GenericIndexed variant, it usually indicates an unhandled lookup miss (-1) or index arithmetic underflow.","triggerScenarios":"Any Indexed implementation's get()/accessor delegating to Indexed.checkIndex with a negative index, commonly from indexOf()-style misses returning -1.","commonSituations":"Treating -1 from a failed lookup as valid, decrementing index 0 to -1, or passing external numeric input straight into get().","solutions":["Validate lookup results before get(): if (idx < 0) handle-not-found;","Bound-check externally supplied indexes against size() before access.","Refactor call sites to use Optional/null-returning lookup helpers instead of sentinel -1."],"exampleFix":"// before\nObject v = indexed.lookupOrdered(k);\nreturn indexed.get(v);\n// after\nint idx = indexed.indexOf(k);\nreturn idx < 0 ? null : indexed.get(idx);","handlingStrategy":"type-guard","validationCode":"if (index < 0 || index >= size) return null;","typeGuard":"static <T> T safeGet(Indexed<T> idx, int i) { return (i >= 0 && i < idx.size()) ? idx.get(i) : null; }","tryCatchPattern":"try { return indexed.get(index); } catch (IAE e) { return null; }","preventionTips":["Convert sentinel -1 lookup results to null/Optional before indexing","Sanitize numeric inputs before passing them to Indexed.get","Centralize Indexed access behind a bounds-checked helper"],"tags":["java","bounds-check","index"],"backgroundTag":"index-out-of-bounds","analyzedSha":"9b90983fd291f26935af934383ce360473179e4d","analyzedAt":"2026-09-07T13:32:30.957Z","contentChangedAt":"2026-09-07T13:32:30.957Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}