Yalantis/uCrop · error · CImgArgumentException

"[" cimg_appname "_math_parser] CImg<%s>: Function 'copy()':

Error message

"[" cimg_appname "_math_parser] CImg<%s>: Function 'copy()': Out-of-bounds variable pointer (length: %ld, increment: %ld, offset start: %ld, offset end: %ld, offset max: %u)."

What it means

The math parser 'copy()' function copies a block of values into the parser's variable memory (mp.mem). Before copying it computes the end offset off + (siz-1)*inc and verifies the whole destination range lies inside mp.mem; otherwise it throws CImgArgumentException describing length, increment, start and end offsets and the maximum valid offset.

Source

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

              abs_val = cimg::abs(val);
              if (abs_val>abs_valmaxabs) { valmaxabs = val; abs_valmaxabs = abs_val; }
            }
          } else {
            val = _mp_arg(i);
            abs_val = cimg::abs(val);
            if (abs_val>abs_valmaxabs) { valmaxabs = val; abs_valmaxabs = abs_val; }
          }
        }
        return valmaxabs;
      }

      static double* _mp_memcopy_double(_cimg_math_parser& mp, const unsigned int ind, const ulongT *const p_ref,
                                        const longT siz, const long inc) {
        const longT
          off = *p_ref?p_ref[1] + (longT)mp.mem[(longT)p_ref[2]] + 1:ind,
          eoff = off + (siz - 1)*inc;
        if (off<0 || eoff>=mp.mem.width())
          throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'copy()': "
                                      "Out-of-bounds variable pointer "
                                      "(length: %ld, increment: %ld, offset start: %ld, "
                                      "offset end: %ld, offset max: %u).",
                                      mp.imgin.pixel_type(),siz,inc,off,eoff,mp.mem._width - 1);
        return &mp.mem[off];
      }

      static float* _mp_memcopy_float(_cimg_math_parser& mp, const ulongT *const p_ref,
                                      const longT siz, const long inc, const bool is_out) {
        const unsigned ind = (unsigned int)p_ref[1];
        const CImg<T> &img = is_out?
          (ind==~0U?mp.imgout:mp.imglist[cimg::mod((int)mp.mem[ind],mp.imglist.width())]):
          (ind==~0U?mp.imgin:mp.imglist[cimg::mod((int)mp.mem[ind],mp.imglist.width())]);
        const bool is_relative = (bool)p_ref[2];
        int ox, oy, oz, oc;
        longT off = 0;
        if (is_relative) {
          ox = (int)mp.mem[_cimg_mp_slot_x];

View on GitHub (pinned to f788b534b4)

Solutions

  1. Ensure the destination variable is declared with at least (siz-1)*inc + 1 elements before calling copy().
  2. Reduce the 'siz' length argument or the increment to fit within the target variable's allocated size.
  3. Check that the destination offset expression (p_ref[1] + mp.mem[...] + 1) points to the intended variable and not past the end of mem.
  4. Catch CImgArgumentException and log the length/increment/offsets to diagnose which argument is wrong.

Example fix

// before: V = vector(4); copy(V, 8, 1, src)
// after:  V = vector(8); copy(V, 8, 1, src)
Defensive patterns

Strategy: validation

Validate before calling

if (siz < 0 || inc <= 0 || off < 0 || off + (siz - 1) * inc >= memWidth)
  throw std::runtime_error("copy() would overflow variable memory");

Try / catch

try { img.evaluate(expr); } catch (const CImgArgumentException& e) { log("copy() out of bounds: " << e.what()); }

Prevention

When it happens

Trigger: An expression like `copy(dest,siz,inc,src...)` where the destination variable pointer offset plus (length-1)*increment exceeds the variable memory width, e.g. copying N elements into a small vector variable, or an increment too large for the target variable size.

Common situations: Resizing input images/vectors without updating hard-coded copy lengths; using vector variables declared with fewer elements than the copy length; negative or oversized increments in strided copies.

Related errors


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