Yalantis/uCrop · error · CImgArgumentException

[" cimg_appname "_math_parser] CImg<%s>::%s: %s%s Specified

Error message

[" cimg_appname "_math_parser] CImg<%s>::%s: %s%s Specified index '%s' is NaN.

What it means

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.

Source

Thrown at ucrop/src/main/jni/CImg.h:25197

                             char *const ss, char *const se, const char saved_char) {
        if (arg!=~0U && !is_const_scalar(arg)) {
          char *s0;
          _cimg_mp_strerr;
          throw CImgArgumentException("[" cimg_appname "_math_parser] "
                                      "CImg<%s>::%s: %s%s Specified image index is not a constant, "
                                      "in expression '%s'.",
                                      pixel_type(),_cimg_mp_calling_function,s_op,*s_op?":":"",s0);
        }
      }

      // Check that specified constant is not nan.
      void check_notnan_index(const unsigned int arg, const char *const s_arg,
                              char *const ss, char *const se, const char saved_char) {
        if (arg!=~0U &&
            (arg==_cimg_mp_slot_nan || (is_const_scalar(arg) && cimg::type<double>::is_nan(mem[arg])))) {
          char *s0;
          _cimg_mp_strerr;
          throw CImgArgumentException("[" cimg_appname "_math_parser] "
                                      "CImg<%s>::%s: %s%s Specified index '%s' is NaN.",
                                      pixel_type(),_cimg_mp_calling_function,s_op,*s_op?":":"",s_arg);
        }
      }

      // Check a matrix is square.
      void check_matrix_square(const unsigned int arg, const unsigned int n_arg,
                               char *const ss, char *const se, const char saved_char) {
        _cimg_mp_check_type(arg,n_arg,2,0);
        const unsigned int
          siz = size(arg),
          n = (unsigned int)cimg::round(std::sqrt((float)siz));
        if (n*n!=siz) {
          const char *s_arg;
          if (*s_op!='F') s_arg = !n_arg?"":n_arg==1?"Left-hand":"Right-hand";
          else s_arg = !n_arg?"":n_arg==1?"First":n_arg==2?"Second":n_arg==3?"Third":"One";
          char *s0;
          _cimg_mp_strerr;

View on GitHub (pinned to f788b534b4)

Solutions

  1. Validate the index expression before use and guard against NaN with a ternary, e.g. 'isnan(i)?0:i'.
  2. Check upstream arithmetic for 0/0 or inf-inf that yields NaN.
  3. Use a constant or validated index instead of a computed one.
  4. Wrap the fill()/expression call in a try/catch on CImgArgumentException to surface a friendly message.

Example fix

// before
img.fill("da_at(A,x/y)"); // y may be 0 -> NaN index
// after
img.fill("da_at(A,y==0?0:x/y)");
Defensive patterns

Strategy: validation

Validate before calling

// Reject NaN index before evaluating
const double idx = computeIndex();
if (std::isnan(idx)) throw std::invalid_argument("math-parser index is NaN");
img.fill((std::string("da_at(A,") + safeExpr + ")").c_str());

Type guard

bool isValidIndex(double v) { return !std::isnan(v) && !std::isinf(v) && v >= 0; }

Try / catch

try { img.fill(expr); } catch (const cimg_library::CImgArgumentException& e) { /* handle NaN index */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/b86079dfff5cdb0e. Report an issue: GitHub.