Yalantis/uCrop · error · CImgArgumentException

[cimg_appname_math_parser] CImg<%s>::%s: Out-of-bounds refer

Error message

[cimg_appname_math_parser] CImg<%s>::%s: Out-of-bounds reference '%s[%d]' (vector '%s' has dimension %u), in expression '%s'.

What it means

When indexing a vector with a compile-time constant index (e.g. 'v[5]'), the parser checks the index against the vector's dimension. If the constant index is negative or >= the vector size, it throws this out-of-bounds reference error.

Source

Thrown at ucrop/src/main/jni/CImg.h:19906

              arg3 = compile(s1,s0,depth1,0,block_flags); // Length
              arg4 = s0<se1?compile(++s0,se1,depth1,0,block_flags):1; // Step
              _cimg_mp_check_const_scalar(arg3,2,3);
              arg3 = (unsigned int)mem[arg3];
              pos = vector(arg3);
              CImg<ulongT>::vector((ulongT)mp_vector_crop,pos,arg1,p1,arg2,arg3,arg4).move_to(code);
              return_comp = true;
              _cimg_mp_return(pos);
            }

            // One argument -> vector value reference.
            arg2 = compile(++s0,se1,depth1,0,block_flags);
            if (is_const_scalar(arg2)) { // Constant index
              nb = (int)mem[arg2];
              if (nb>=0 && nb<(int)size(arg1)) _cimg_mp_return(arg1 + 1 + nb);
              variable_name.assign(ss,(unsigned int)(s0 - ss)).back() = 0;
              cimg::strellipsize(variable_name,64);
              _cimg_mp_strerr;
              throw CImgArgumentException("[" cimg_appname "_math_parser] "
                                          "CImg<%s>::%s: Out-of-bounds reference '%s[%d]' "
                                          "(vector '%s' has dimension %u), "
                                          "in expression '%s'.",
                                          pixel_type(),_cimg_mp_calling_function,
                                          variable_name._data,nb,
                                          variable_name._data,size(arg1),s0);
            }
            if (p_ref) {
              *p_ref = 1;
              p_ref[1] = arg1;
              p_ref[2] = arg2;
              if (is_comp_scalar(arg2)) memtype[arg2] = -1; // Prevent from being used in further optimization
            }
            pos = scalar3(mp_vector_off,arg1,size(arg1),arg2);
            memtype[pos] = -1; // Prevent from being used in further optimization
            _cimg_mp_return(pos);
          }
        }

View on GitHub (pinned to f788b534b4)

Solutions

  1. Use an index in the range [0, size(v)-1]
  2. Verify the vector's declared dimension matches the indices used
  3. Use a runtime-computed index clamped to bounds if dynamic access is needed

Example fix

// before
v = vector(4); x = v[4];
// after
v = vector(4); x = v[3];
Defensive patterns

Strategy: validation

Validate before calling

function checkConstVectorIndex(expr, vectorSizes) {
  for (const m of expr.matchAll(/([A-Za-z_]\w*)\s*\[\s*(-?\d+)\s*\]/g)) {
    const size = vectorSizes.get(m[1]);
    const idx = parseInt(m[2], 10);
    if (size !== undefined && (idx < 0 || idx >= size))
      throw new Error(`${m[1]}[${idx}] out of bounds (dim ${size})`);
  }
}

Try / catch

try {
  img.evaluate(expr);
} catch (e) {
  if (/Out-of-bounds reference/.test(String(e))) {
    // clamp index or resize the vector
  } else throw e;
}

Prevention

When it happens

Trigger: Accessing 'v[k]' where k is a constant literal and k<0 or k>=size(v); vector dimension mismatch after changing vector allocations.

Common situations: Off-by-one indexing (1-based assumptions in a 0-based parser), shrinking a vector's size while older index expressions remain, hard-coded indices.

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/3f77cb167a2d6acb. Report an issue: GitHub.