apache/dubbo · error · IndexOutOfBoundsException

Index: ${index}, Size: ${mSize}

Error message

Index: ${index}, Size: ${mSize}

What it means

Thrown by Stack.get(int index) when index is out of bounds. The guard checks two conditions: index >= mSize (too large) or index + mSize < 0 (excessively negative, since negative indices wrap from the end). The message reports the offending index and current size. Negative indices are supported as offsets from the top.

Source

Thrown at dubbo-common/src/main/java/org/apache/dubbo/common/utils/Stack.java:79

     *
     * @return the last element.
     */
    public E peek() {
        if (mSize == 0) {
            throw new EmptyStackException();
        }
        return mElements.get(mSize - 1);
    }

    /**
     * get.
     *
     * @param index index.
     * @return element.
     */
    public E get(int index) {
        if (index >= mSize || index + mSize < 0) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + mSize);
        }

        return index < 0 ? mElements.get(index + mSize) : mElements.get(index);
    }

    /**
     * set.
     *
     * @param index index.
     * @param value element.
     * @return old element.
     */
    public E set(int index, E value) {
        if (index >= mSize || index + mSize < 0) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + mSize);
        }

        return mElements.set(index < 0 ? index + mSize : index, value);

View on GitHub (pinned to 3a3043227f)

Solutions

  1. Check bounds before access: if (index >= 0 && index < stack.size()) { ... }.
  2. Fix off-by-one in loops: use < stack.size() not <= stack.size().
  3. For negative indices, ensure the absolute value does not exceed stack.size().

Example fix

// before
for (int i = 0; i <= stack.size(); i++) {
    E e = stack.get(i);
}

// after
for (int i = 0; i < stack.size(); i++) {
    E e = stack.get(i);
}
Defensive patterns

Strategy: validation

Validate before calling

if (index >= 0 && index < stack.size()) {
    E val = stack.get(index);
} else {
    // handle out-of-bounds
}

Prevention

When it happens

Trigger: Calling get(size) or get(size + n) (one-past-end or beyond); calling get(-n) where n > mSize (negative index more negative than -size); calling get on an empty stack (any non-negative index triggers since 0 >= 0).

Common situations: Off-by-one loop: for (int i = 0; i <= stack.size(); i++) instead of <; using get(0) on an empty stack; index computed from external input without clamping.

Related errors


AI-assisted analysis of apache/dubbo@3a3043227f (2026-08-14). Data as JSON: /api/errors/3e9e0a3a14c08b52. Report an issue: GitHub.