{"record":{"id":"43da8c7ab5aa1d53","repo":"Yalantis/uCrop","slug":"cimg-appname-math-parser-cimg-s-s-s-inval-43da8c","errorCode":null,"errorMessage":"[cimg_appname_math_parser] CImg<%s>::%s: %s: Invalid type '%s' for variable '%s' (expected 'scalar'), in expression '%s'.","messagePattern":"\\[cimg_appname_math_parser\\] CImg<(.+?)>::(.+?): (.+?): Invalid type '(.+?)' for variable '(.+?)' \\(expected 'scalar'\\), in expression '(.+?)'\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":21794,"sourceCode":"              if (s1<se1) { // Version with 3 arguments\n                variable_name.assign(s0,(unsigned int)(s1 + 1 - s0)).back() = 0;\n                cimg::strpare(variable_name,false,true);\n                if (!cimg::is_varname(variable_name)) { // Invalid variable name\n                  cimg::strellipsize(variable_name,64);\n                  _cimg_mp_strerr;\n                  throw CImgArgumentException(\"[\" cimg_appname \"_math_parser] \"\n                                              \"CImg<%s>::%s: %s: Invalid loop variable name '%s', \"\n                                              \"in expression '%s'.\",\n                                              pixel_type(),_cimg_mp_calling_function,s_op,\n                                              variable_name._data,s0);\n                }\n                get_variable_pos(variable_name,arg2,arg3);\n                arg2 = arg3!=~0U?reserved_label[arg3]:arg2!=~0U?variable_pos[arg2]:~0U; // Variable slot\n                if (arg2!=~0U && (!is_scalar(arg2) ||\n                                  is_const_scalar(arg2))) { // Variable is not a vector or is a constant->error\n                  cimg::strellipsize(variable_name,64);\n                  _cimg_mp_strerr;\n                  throw CImgArgumentException(\"[\" cimg_appname \"_math_parser] \"\n                                              \"CImg<%s>::%s: %s: Invalid type '%s' for variable '%s' \"\n                                              \"(expected 'scalar'), in expression '%s'.\",\n                                              pixel_type(),_cimg_mp_calling_function,s_op,\n                                              s_type(arg2)._data,variable_name._data,s0);\n                } else if (arg2==~0U) { // Variable does not exist -> create it\n                  arg2 = scalar();\n                  if (arg3!=~0U) reserved_label[arg3] = arg2;\n                  else {\n                    if (variable_def._width>=variable_pos._width) variable_pos.resize(-200,1,1,1,0);\n                    variable_pos[variable_def._width] = arg2;\n                    variable_name.move_to(variable_def);\n                  }\n                  memtype[arg2] = -1;\n                }\n                arg3 = compile(++s1,se1,depth1,0,block_flags);\n                _cimg_mp_check_type(arg3,3,1,0);\n              } else { // Version with 2 arguments\n                arg2 = ~0U;","sourceCodeStart":21776,"sourceCodeEnd":21812,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L21776-L21812","documentation":"For 3-argument loops, the named loop variable must be an existing, non-constant scalar variable slot. After resolving the variable position, CImg checks is_scalar(slot) && !is_const_scalar(slot); if the variable is a vector type or was declared constant, it throws this error reporting the actual type via s_type(arg2).","triggerScenarios":"Using a vector variable as a loop variable (`for(v..., ...)` where v=vector(...)), or a variable captured as a constant (e.g. a constant literal binding or a const-folded slot), in `for`/`while`/`repeat` expressions compiled by fill()/evaluate().","commonSituations":"Reusing an existing vector name for the loop counter; declaring the variable with a constant expression that the parser marked const; shadowing a built-in constant name (pi, e, w, h, d, s) as loop variable.","solutions":["Use a plain scalar variable as the loop variable (declare with `x = 0` before the loop, not vector())","Rename the loop variable so it doesn't shadow a vector or reserved constant (pi, e, w, h, d, s...)","Initialize the variable with a non-constant expression so it gets a mutable scalar slot","Use the 2-argument loop form with an implicit counter variable"],"exampleFix":"// before\nimg.fill(\"v = vector(3); for(v, 0, 5, ...)\" ); // v is a vector\n// after\nimg.fill(\"i = 0; for(i, 0, 5, ...)\");","handlingStrategy":"validation","validationCode":"// Before a 3-arg loop, ensure the counter variable is declared as a plain mutable scalar:\n// \"i = 0; for(i, 0, n, ...)\" — never reuse a vector() variable as the counter.\nboolean loopVarIsScalarDecl(String expr, String var) {\n  return expr.matches(\"(?s).*\\\\b\" + var + \"\\\\s*=\\\\s*[-+0-9.].*\\\\bfor\\\\s*\\\\(\\\\s*\" + var + \"\\\\b.*\");\n}","typeGuard":null,"tryCatchPattern":"try { img.fill(expr); } catch (CImgArgumentException e) { if (e.getMessage().contains(\"Invalid type\") && e.getMessage().contains(\"expected 'scalar'\")) { throw new IllegalArgumentException(\"Loop variable must be a mutable scalar, not a vector or constant\"); } throw e; }","preventionTips":["Declare loop counters with a scalar initializer (e.g. i = 0) before the loop","Never use vector variables or reserved constants (pi, e, w, h, d, s) as loop counters","Keep loop variable names unique to avoid shadowing vectors"],"tags":["cimg","math-parser","type-mismatch","loop-variable"],"backgroundTag":"type-mismatch","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"}