{"record":{"id":"b86079dfff5cdb0e","repo":"Yalantis/uCrop","slug":"cimg-appname-math-parser-cimg-s-s-s-s-b86079","errorCode":null,"errorMessage":"[\" cimg_appname \"_math_parser] CImg<%s>::%s: %s%s Specified index '%s' is NaN.","messagePattern":"\\[\" cimg_appname \"_math_parser\\] CImg<(.+?)>::(.+?): (.+?)(.+?) Specified index '(.+?)' is NaN\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":25197,"sourceCode":"                             char *const ss, char *const se, const char saved_char) {\n        if (arg!=~0U && !is_const_scalar(arg)) {\n          char *s0;\n          _cimg_mp_strerr;\n          throw CImgArgumentException(\"[\" cimg_appname \"_math_parser] \"\n                                      \"CImg<%s>::%s: %s%s Specified image index is not a constant, \"\n                                      \"in expression '%s'.\",\n                                      pixel_type(),_cimg_mp_calling_function,s_op,*s_op?\":\":\"\",s0);\n        }\n      }\n\n      // Check that specified constant is not nan.\n      void check_notnan_index(const unsigned int arg, const char *const s_arg,\n                              char *const ss, char *const se, const char saved_char) {\n        if (arg!=~0U &&\n            (arg==_cimg_mp_slot_nan || (is_const_scalar(arg) && cimg::type<double>::is_nan(mem[arg])))) {\n          char *s0;\n          _cimg_mp_strerr;\n          throw CImgArgumentException(\"[\" cimg_appname \"_math_parser] \"\n                                      \"CImg<%s>::%s: %s%s Specified index '%s' is NaN.\",\n                                      pixel_type(),_cimg_mp_calling_function,s_op,*s_op?\":\":\"\",s_arg);\n        }\n      }\n\n      // Check a matrix is square.\n      void check_matrix_square(const unsigned int arg, const unsigned int n_arg,\n                               char *const ss, char *const se, const char saved_char) {\n        _cimg_mp_check_type(arg,n_arg,2,0);\n        const unsigned int\n          siz = size(arg),\n          n = (unsigned int)cimg::round(std::sqrt((float)siz));\n        if (n*n!=siz) {\n          const char *s_arg;\n          if (*s_op!='F') s_arg = !n_arg?\"\":n_arg==1?\"Left-hand\":\"Right-hand\";\n          else s_arg = !n_arg?\"\":n_arg==1?\"First\":n_arg==2?\"Second\":n_arg==3?\"Third\":\"One\";\n          char *s0;\n          _cimg_mp_strerr;","sourceCodeStart":25179,"sourceCodeEnd":25215,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L25179-L25215","documentation":"CImg's math expression parser throws CImgArgumentException when an index argument passed to a math-expression function evaluates to NaN. The check_notnan_index helper runs during parsing to reject NaN indices early instead of causing undefined memory access. It fires when the compiled opcode slot is the NaN marker or the constant scalar memory cell holds a double NaN.","triggerScenarios":"Calling a math-parser function (e.g. inside a fill() or MP expression) with an index argument computed as NaN, such as 'da_at(img,nan)' or an index derived from a 0/0 expression.","commonSituations":"Division by zero inside expressions producing NaN indices; reading index values from uninitialized or invalid image data; typos like 'x/y' where y can be 0 in user-provided expressions.","solutions":["Validate the index expression before use and guard against NaN with a ternary, e.g. 'isnan(i)?0:i'.","Check upstream arithmetic for 0/0 or inf-inf that yields NaN.","Use a constant or validated index instead of a computed one.","Wrap the fill()/expression call in a try/catch on CImgArgumentException to surface a friendly message."],"exampleFix":"// before\nimg.fill(\"da_at(A,x/y)\"); // y may be 0 -> NaN index\n// after\nimg.fill(\"da_at(A,y==0?0:x/y)\");","handlingStrategy":"validation","validationCode":"// Reject NaN index before evaluating\nconst double idx = computeIndex();\nif (std::isnan(idx)) throw std::invalid_argument(\"math-parser index is NaN\");\nimg.fill((std::string(\"da_at(A,\") + safeExpr + \")\").c_str());","typeGuard":"bool isValidIndex(double v) { return !std::isnan(v) && !std::isinf(v) && v >= 0; }","tryCatchPattern":"try { img.fill(expr); } catch (const cimg_library::CImgArgumentException& e) { /* handle NaN index */ }","preventionTips":["Guard divisions used to compute indices","Initialize all expression variables","Validate indices are finite before use"],"tags":["cimg","math-parser","nan","argument-validation"],"backgroundTag":"invalid-argument-value","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"}