{"record":{"id":"1d0f6bab06dc68c7","repo":"google/ExoPlayer","slug":"invalid-index-index-size-is-size","errorCode":null,"errorMessage":"Invalid index {index}, size is {size}","messagePattern":"Invalid index (.+?), size is (.+?)","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"library/common/src/main/java/com/google/android/exoplayer2/util/LongArray.java","lineNumber":69,"sourceCode":"   */\n  public void add(long value) {\n    if (size == values.length) {\n      values = Arrays.copyOf(values, size * 2);\n    }\n    values[size++] = value;\n  }\n\n  /**\n   * Returns the value at a specified index.\n   *\n   * @param index The index.\n   * @return The corresponding value.\n   * @throws IndexOutOfBoundsException If the index is less than zero, or greater than or equal to\n   *     {@link #size()}.\n   */\n  public long get(int index) {\n    if (index < 0 || index >= size) {\n      throw new IndexOutOfBoundsException(\"Invalid index \" + index + \", size is \" + size);\n    }\n    return values[index];\n  }\n\n  /** Returns the current size of the array. */\n  public int size() {\n    return size;\n  }\n\n  /**\n   * Copies the current values into a newly allocated primitive array.\n   *\n   * @return The primitive array containing the copied values.\n   */\n  public long[] toArray() {\n    return Arrays.copyOf(values, size);\n  }\n}","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/google/ExoPlayer/blob/dd430f7053a1a3958deea3ead6a0565150c06bfc/library/common/src/main/java/com/google/android/exoplayer2/util/LongArray.java#L51-L87","documentation":"LongArray is ExoPlayer's auto-growing primitive long[] wrapper; get(index) throws IndexOutOfBoundsException when index is negative or >= size(). The message reports both the requested index and the actual size so off-by-one or stale-index bugs are immediately visible. It behaves exactly like standard collections: a get can only succeed for 0 <= index < size().","triggerScenarios":"Calling get(i) with i == size() (classic off-by-one in a <= loop), using an index captured before the array was cleared/rebuilt (e.g. across a seek or stream reset), or a negative index from arithmetic like index - 1 when index == 0. Internal users include subtitle/chunk scheduling caches and seek maps that store timestamps by index.","commonSituations":"Custom MediaSource/Metadata parsers built on LongArray that compute positions from 'lastIndex + 1'; iterating while concurrently modifying (another thread clears), and porting code from arrays (where length was constant) to LongArray whose size shrinks after clear().","solutions":["Use size() in loop bounds: for (int i = 0; i < arr.size(); i++) — never <=, never a cached length","Re-derive or re-validate the index right before get(): if (index >= 0 && index < arr.size()) arr.get(index);","Recompute stored indexes after any operation that can clear or trim the LongArray (seek, reset, format change)","Harden parsers: bounds-check against both size() and the underlying data's declared count before indexing"],"exampleFix":"// before\nfor (int i = 0; i <= chunkStarts.size(); i++) {\n  long ts = chunkStarts.get(i); // throws at i == size\n}\n// after\nfor (int i = 0; i < chunkStarts.size(); i++) {\n  long ts = chunkStarts.get(i);\n}","handlingStrategy":"validation","validationCode":"if (index < 0 || index >= longArray.size()) {\n  // skip or clamp instead of calling get\n}","typeGuard":null,"tryCatchPattern":"Not recommended: IndexOutOfBoundsException from LongArray.get signals a logic bug; bounds-check before the call.","preventionTips":["Always bound loops with size(), re-read at call time","Invalidate cached indexes whenever the array is cleared or rebuilt","Prefer binary-search helpers that return negative 'not found' and check the sign before get"],"tags":["collections","index-out-of-bounds","parsing"],"backgroundTag":null,"analyzedSha":"dd430f7053a1a3958deea3ead6a0565150c06bfc","analyzedAt":"2026-08-14T12:22:02.982Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}