{"record":{"id":"5d683305fa96328c","repo":"Yalantis/uCrop","slug":"cimg-appname-math-parser-cimg-s-s-unbal","errorCode":null,"errorMessage":"[\" cimg_appname \"_math_parser] CImg<%s>::%s: Unbalanced parentheses/brackets, in expression '%s'.","messagePattern":"\\[\" cimg_appname \"_math_parser\\] CImg<(.+?)>::(.+?): Unbalanced parentheses/brackets, in expression '(.+?)'\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":25023,"sourceCode":"          }\n          *(pd++) = (unsigned int)(mode>=1 || is_escaped?_level + (mode==1):\n                                   *ps=='(' || *ps=='['?_level++:\n                                   *ps==')' || *ps==']'?--_level:\n                                   _level);\n          mode = next_mode;\n          is_escaped = next_is_escaped;\n          next_is_escaped = false;\n        }\n        if (mode) {\n          cimg::strellipsize(_expr,64);\n          throw CImgArgumentException(\"[\" cimg_appname \"_math_parser] \"\n                                      \"CImg<%s>::%s: Unterminated string literal, in expression '%s'.\",\n                                      pixel_type(),_cimg_mp_calling_function,\n                                      _expr._data);\n        }\n        if (_level) {\n          cimg::strellipsize(_expr,64);\n          throw CImgArgumentException(\"[\" cimg_appname \"_math_parser] \"\n                                      \"CImg<%s>::%s: Unbalanced parentheses/brackets, in expression '%s'.\",\n                                      pixel_type(),_cimg_mp_calling_function,\n                                      _expr._data);\n        }\n        return res;\n      }\n\n      // Find and return index of current image 'imgin' within image list 'imglist'.\n      unsigned int get_mem_img_index() {\n        if (mem_img_index==~0U) {\n          if (&imgout>=imglist.data() && &imgout<imglist.end())\n            mem_img_index = const_scalar((double)(&imgout - imglist.data()));\n          else {\n            unsigned int pos = ~0U;\n            cimglist_for(imglist,l)\n              if (imgout._data==imglist[l]._data && imgout.is_sameXYZC(imglist[l])) { pos = l; break; }\n            if (pos!=~0U) mem_img_index = const_scalar((double)pos);\n          }","sourceCodeStart":25005,"sourceCodeEnd":25041,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L25005-L25041","documentation":"After tokenizing the math expression, the CImg parser tracks nesting depth (_level). If the level is non-zero at the end of parsing, some '(' or '[' was never closed (or a ')' / ']' was closed out of order), so it throws CImgArgumentException for unbalanced parentheses/brackets. This is a structural validation pass that always runs before the compiled expression is returned.","triggerScenarios":"Calling a CImg function with an expression string such as img.fill(\"sin(x*(cos(y)\") where an opening '(' or '[' lacks its closing partner, or brackets are closed in the wrong order like \"(x]\")\". Also occurs when a builder or template generates expressions with conditionally dropped closing brackets.","commonSituations":"Hand-written formulas with deeply nested function calls; code-generated expressions where a branch omits a closing paren; editing expressions by hand and deleting one side of a bracket pair; macro/template substitution producing broken formulas.","solutions":["Count and match every '(' with ')' and every '[' with ']' in the expression string","Log the full expression (the error truncates it to 64 chars) and check the tail for dangling open brackets","If generating expressions in code, build them with a small helper that tracks nesting depth and asserts balance before calling CImg","Validate the expression with a paren-balance check before passing it to the CImg API"],"exampleFix":"// before\nimg.fill(\"sin(x*(cos(y)\"); // unbalanced: missing one ')'\n// after\nimg.fill(\"sin(x*(cos(y)))\");","handlingStrategy":"validation","validationCode":"// C++: verify parentheses/bracket balance before calling CImg\nbool brackets_balanced(const std::string& e) {\n  int d = 0;\n  for (char c : e) {\n    if (c == '(' || c == '[') ++d;\n    else if (c == ')' || c == ']') --d;\n    if (d < 0) return false;\n  }\n  return d == 0;\n}\nif (!brackets_balanced(expr)) throw std::invalid_argument(\"unbalanced parentheses in expression\");","typeGuard":"bool has_balanced_brackets(const std::string& e); // see validationCode","tryCatchPattern":"try {\n  img.fill(expr);\n} catch (const cimg_library::CImgArgumentException& e) {\n  std::cerr << \"Unbalanced brackets in: \" << expr << std::endl;\n}","preventionTips":["Build nested expressions with a builder that tracks depth","Keep indentation in generated expression strings aligned to nesting","Run a paren-balance unit test over every expression constant in the codebase"],"tags":["cimg","math-parser","expression-parsing","syntax-error"],"backgroundTag":"invalid-argument-format","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"}