{"record":{"id":"3f77cb167a2d6acb","repo":"Yalantis/uCrop","slug":"cimg-appname-math-parser-cimg-s-s-out-of-bo","errorCode":null,"errorMessage":"[cimg_appname_math_parser] CImg<%s>::%s: Out-of-bounds reference '%s[%d]' (vector '%s' has dimension %u), in expression '%s'.","messagePattern":"\\[cimg_appname_math_parser\\] CImg<(.+?)>::(.+?): Out-of-bounds reference '(.+?)\\[(.+?)\\]' \\(vector '(.+?)' has dimension %u\\), in expression '(.+?)'\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":19906,"sourceCode":"              arg3 = compile(s1,s0,depth1,0,block_flags); // Length\n              arg4 = s0<se1?compile(++s0,se1,depth1,0,block_flags):1; // Step\n              _cimg_mp_check_const_scalar(arg3,2,3);\n              arg3 = (unsigned int)mem[arg3];\n              pos = vector(arg3);\n              CImg<ulongT>::vector((ulongT)mp_vector_crop,pos,arg1,p1,arg2,arg3,arg4).move_to(code);\n              return_comp = true;\n              _cimg_mp_return(pos);\n            }\n\n            // One argument -> vector value reference.\n            arg2 = compile(++s0,se1,depth1,0,block_flags);\n            if (is_const_scalar(arg2)) { // Constant index\n              nb = (int)mem[arg2];\n              if (nb>=0 && nb<(int)size(arg1)) _cimg_mp_return(arg1 + 1 + nb);\n              variable_name.assign(ss,(unsigned int)(s0 - ss)).back() = 0;\n              cimg::strellipsize(variable_name,64);\n              _cimg_mp_strerr;\n              throw CImgArgumentException(\"[\" cimg_appname \"_math_parser] \"\n                                          \"CImg<%s>::%s: Out-of-bounds reference '%s[%d]' \"\n                                          \"(vector '%s' has dimension %u), \"\n                                          \"in expression '%s'.\",\n                                          pixel_type(),_cimg_mp_calling_function,\n                                          variable_name._data,nb,\n                                          variable_name._data,size(arg1),s0);\n            }\n            if (p_ref) {\n              *p_ref = 1;\n              p_ref[1] = arg1;\n              p_ref[2] = arg2;\n              if (is_comp_scalar(arg2)) memtype[arg2] = -1; // Prevent from being used in further optimization\n            }\n            pos = scalar3(mp_vector_off,arg1,size(arg1),arg2);\n            memtype[pos] = -1; // Prevent from being used in further optimization\n            _cimg_mp_return(pos);\n          }\n        }","sourceCodeStart":19888,"sourceCodeEnd":19924,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L19888-L19924","documentation":"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.","triggerScenarios":"Accessing 'v[k]' where k is a constant literal and k<0 or k>=size(v); vector dimension mismatch after changing vector allocations.","commonSituations":"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.","solutions":["Use an index in the range [0, size(v)-1]","Verify the vector's declared dimension matches the indices used","Use a runtime-computed index clamped to bounds if dynamic access is needed"],"exampleFix":"// before\nv = vector(4); x = v[4];\n// after\nv = vector(4); x = v[3];","handlingStrategy":"validation","validationCode":"function checkConstVectorIndex(expr, vectorSizes) {\n  for (const m of expr.matchAll(/([A-Za-z_]\\w*)\\s*\\[\\s*(-?\\d+)\\s*\\]/g)) {\n    const size = vectorSizes.get(m[1]);\n    const idx = parseInt(m[2], 10);\n    if (size !== undefined && (idx < 0 || idx >= size))\n      throw new Error(`${m[1]}[${idx}] out of bounds (dim ${size})`);\n  }\n}\n","typeGuard":null,"tryCatchPattern":"try {\n  img.evaluate(expr);\n} catch (e) {\n  if (/Out-of-bounds reference/.test(String(e))) {\n    // clamp index or resize the vector\n  } else throw e;\n}\n","preventionTips":["Remember CImg math-parser indexing is 0-based","Update index expressions whenever vector sizes change","Clamp dynamic indices to [0, size-1]"],"tags":["cimg","math-parser","vector","out-of-bounds"],"backgroundTag":"index-out-of-bounds","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}