{"record":{"id":"6d0a6f6de696707b","repo":"stanfordnlp/CoreNLP","slug":"out-of-bounds-d-d","errorCode":null,"errorMessage":"Out of bounds: %d >= %d","messagePattern":"Out of bounds: (.+?) >= (.+?)","errorType":"exception","errorClass":"ArrayIndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/concurrent/ConcurrentHashIndex.java","lineNumber":69,"sourceCode":"   */\n  public ConcurrentHashIndex(int initialCapacity) {\n    item2Index = new ConcurrentHashMap<>(initialCapacity);\n    indexSize = 0;\n    lock = new ReentrantLock();\n    Object[] arr = new Object[initialCapacity];\n    index2Item = new AtomicReference<>(arr);\n  }\n\n  @SuppressWarnings(\"unchecked\")\n  @Override\n  public E get(int i) {\n    Object[] arr = index2Item.get();\n    if (i < indexSize) {\n      // arr.length guaranteed to be == to size() given the\n      // implementation of indexOf below.\n      return (E) arr[i];\n    }\n    throw new ArrayIndexOutOfBoundsException(String.format(\"Out of bounds: %d >= %d\", i, indexSize));\n  }\n\n  @Override\n  public int indexOf(E o) {\n    Integer id = item2Index.get(o);\n    return id == null ? UNKNOWN_ID : id;\n  }\n\n  @Override\n  public int addToIndex(E o) {\n    Integer index = item2Index.get(o);\n    if (index != null) {\n      return index;\n    }\n\n    lock.lock();\n    try {\n      // Recheck state","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/concurrent/ConcurrentHashIndex.java#L51-L87","documentation":"ConcurrentHashIndex.get(int i) throws ArrayIndexOutOfBoundsException when the requested index i is not smaller than the index's current size (indexSize). The index maps items to dense integer IDs; asking for an ID that was never assigned (or belongs to a stale snapshot) is out of bounds. The message reports the requested index and the current bound.","triggerScenarios":"Calling get(i) with an id returned by indexOf() as UNKNOWN (-1), an id saved to disk for a different index (deserialized via saveToWriter/loadFromReader into a new index), or an id >= size() of the live index.","commonSituations":"Persisting a model with index ids and loading it against a rebuilt/shorter index; using -1 (UNKNOWN_ID from indexOf) as a lookup key; concurrent modification where indexSize shrank or the caller held an old id.","solutions":["Check 0 <= i && i < index.size() before calling get(i)","Treat indexOf() == Integer-UNKNOWN (-1) as 'item absent' and skip the get()","Load the index from the same saved snapshot that produced the ids (saveToWriter/loadFromReader pair), not a rebuilt one","Re-derive ids by calling indexOf(item) instead of caching ids across runs"],"exampleFix":"// before\nE item = index.get(savedId);\n// after\nint id = index.indexOf(item);\nE item2 = (id != -1) ? index.get(id) : null;","handlingStrategy":"validation","validationCode":"if (id < 0 || id >= index.size()) {\n  throw new IllegalArgumentException(\"id \" + id + \" not present in index of size \" + index.size());\n}\nE item = index.get(id);","typeGuard":"// Java has no runtime type guard; use a bounds-check helper\nstatic <E> E safeGet(ConcurrentHashIndex<E> idx, int i) {\n  return (i >= 0 && i < idx.size()) ? idx.get(i) : null;\n}","tryCatchPattern":"try {\n  E item = index.get(id);\n} catch (ArrayIndexOutOfBoundsException e) {\n  log.warn(\"Stale/unknown id {} against index of size {}\", id, index.size());\n  item = null; // fall back to re-indexing the item\n}","preventionTips":["Never use indexOf()'s -1 (UNKNOWN_ID) as a get() argument","Persist and restore the index itself (saveToWriter/loadFromReader) whenever you persist ids","Re-derive ids with indexOf() instead of caching them across rebuilds","Log index.size() alongside any id in diagnostics"],"tags":["index","out-of-bounds","concurrency"],"backgroundTag":"index-out-of-bounds","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}