Yalantis/uCrop · error · CImgArgumentException
[cimg_appname_math_parser] CImg<%s>::%s: %s: Specified varia
Error message
[cimg_appname_math_parser] CImg<%s>::%s: %s: Specified variable size %g is larger than %d.
What it means
When a math-parser function (e.g. dynamic vector allocation like `vector(size)`) computes the requested size at runtime, the resulting value must fit in an int. If mem[arg2] (the evaluated size, stored as double) exceeds cimg::type<int>::max(), CImg throws this error instead of truncating or overflowing the allocation.
Source
Thrown at ucrop/src/main/jni/CImg.h:21996
if (!std::strncmp(ss,"get(",4)) { // Get value from external variable
_cimg_mp_op("Function 'get()'");
s1 = ss4; while (s1<se1 && (*s1!=',' || level[s1 - expr._data]!=clevel1)) ++s1;
arg1 = compile(ss4,s1,depth1,0,block_flags);
arg2 = arg3 = 0;
if (s1<se1) {
s2 = ++s1; while (s2<se1 && (*s2!=',' || level[s2 - expr._data]!=clevel1)) ++s2;
arg2 = compile(s1,s2,depth1,0,block_flags);
arg3 = s2<se1?compile(++s2,se1,depth1,0,block_flags):0;
}
_cimg_mp_check_type(arg1,1,2,0);
_cimg_mp_check_const_scalar(arg2,2,2);
_cimg_mp_check_type(arg3,3,1,0);
p1 = size(arg1);
const int siz_max = cimg::type<int>::max();
if (mem[arg2]>=siz_max) {
_cimg_mp_strerr;
throw CImgArgumentException("[" cimg_appname "_math_parser] "
"CImg<%s>::%s: %s: Specified variable size %g is larger than %d.",
pixel_type(),_cimg_mp_calling_function,s_op,
mem[arg2],siz_max);
}
arg2 = (unsigned int)mem[arg2];
if (arg2) pos = vector(arg2); else pos = scalar();
CImg<ulongT>::vector((ulongT)mp_get,pos,arg1,p1,arg2,arg3).move_to(code);
return_comp = true;
_cimg_mp_return(pos);
}
#endif
break;
case 'h' :
if (*ss1=='(') { // Image height
_cimg_mp_op("Function 'h()'");
if (*ss2=='#') { // Index specified
p1 = compile(ss3,se1,depth1,0,block_flags);View on GitHub (pinned to f788b534b4)
Solutions
- Clamp or reduce the requested size expression so it is < 2147483647
- Verify units: the argument is the number of elements, not bytes
- Add a runtime guard in the expression (e.g. `min(size, 1e9)`) before allocating
- Rework the algorithm: a >2G-element per-item buffer usually indicates a logic bug
Example fix
// before
img.fill("n = w*h*d; v = vector(n)"); // may exceed int max
// after
img.fill("n = min(w*h*d,2e9); v = vector(n)"); Defensive patterns
Strategy: validation
Validate before calling
long n = (long) width * height * depth; // or whatever drives the size
if (n <= 0 || n >= Integer.MAX_VALUE) throw new IllegalArgumentException("Requested vector size out of int range: " + n); Try / catch
try { img.fill(expr); } catch (CImgArgumentException e) { if (e.getMessage().contains("Specified variable size")) { /* clamp size and retry, or fail fast */ } throw e; } Prevention
- Clamp computed sizes with min() in the expression
- Remember sizes are element counts, not bytes
- Sanity-check sizes derived from image dimensions before allocating
When it happens
Trigger: Calling `vector(n)` / `array(n)` style allocation where the size expression evaluates to a value >= 2^31-1, e.g. `vector(2^31)` or a size computed from image dimensions with an off-by-scale multiplication; NaN/negative values may also produce out-of-range comparisons leading here.
Common situations: Computing vector sizes from large image dimensions without clamping; unit confusion (bytes vs elements) inflating the requested size; unbounded loop-generated sizes in generated expressions.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- CImg(): Failed to allocate memory (%s) for image (%u,%u,%u,%
- [cimg_appname_math_parser] CImg<%s>::%s: %s: Too much specif
- [cimg_appname_math_parser] CImg<%s>::%s: %s: %s name specifi
- [cimg_appname_math_parser] CImg<%s>::%s: %s: Invalid assignm
- [cimg_appname_math_parser] CImg<%s>::%s: %s: Invalid %slvalu
AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08).
Data as JSON: /api/errors/678c8857ea55ac67.
Report an issue: GitHub.