Yalantis/uCrop · error · CImgArgumentException

"[" cimg_appname "_math_parser] CImg<

Error message

"[" cimg_appname "_math_parser] CImg<%s>: Function 'da_remove()': Invalid starting (%d) and ending (%d) positions (not ordered, in range -%d...%d)."

What it means

mp_da_remove() validates that the start/end positions of the removal range lie inside the array (0..siz-1 after negative-index normalization) and that start<=end. This error is thrown when the given range is out of bounds, reversed, or otherwise not an ordered valid range.

Solutions

  1. Clamp/validate start and end to 0..da_size(#ind)-1 before calling.
  2. Ensure start<=end (swap or sort the arguments if computed dynamically).
  3. Use the default sentinel (omit arguments) instead of hand-computed last-index values.
  4. Print da_size(#ind) in the expression to debug the actual array length.

Example fix

// before: start > end
 da_remove(#0,5,2)
// after
 da_remove(#0,2,5)
Defensive patterns

Strategy: validation

Validate before calling

// Clamp and order the removal range before calling
def clampRange(s, e, siz) { s = Math.max(0, Math.min(s, siz - 1)); e = Math.max(s, Math.min(e, siz - 1)); return [s, e]; }

Type guard

function isValidRange(s, e, siz) { return s >= 0 && e >= 0 && s <= e && s < siz && e < siz; }

Try / catch

try { evalMath(expr); } catch (e) { if (String(e).includes('Invalid starting') && String(e).includes('da_remove')) { [s,e] = clampRange(s,e,siz); retry(); } else throw e; }

Prevention

When it happens

Trigger: Calling da_remove(#ind,start,end) with start>end, with positions >= siz, or with negative values whose magnitude exceeds siz (e.g. siz=3 and start=-5).

Common situations: Off-by-one errors in computed ranges; assuming the array is larger than it is; passing unsorted user-supplied indices; using -1 expecting 'last element' when the default (~0U) sentinel was intended.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

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

        CImg<T> &img = mp.imglist[ind];
        int siz = img?(int)cimg::float2uint((float)img[img._height - 1]):0;
        if (img && (img._width!=1 || img._depth!=1 || siz<0 || siz>img.height() - 1))
          throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'da_remove()': "
                                      "Specified image #%u of size (%d,%d,%d,%d) cannot be used as dynamic array%s.",
                                      mp.imgout.pixel_type(),ind,
                                      img.width(),img.height(),img.depth(),img.spectrum(),
                                      img._width==1 && img._depth==1?"":" (contains invalid element counter)");
        if (!siz)
          throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'da_remove()': "
                                      "Dynamic array is empty.",
                                      mp.imgout.pixel_type());
        int
          start0 = mp.opcode[3]==~0U?siz - 1:_mp_arg(3),
          end0 = mp.opcode[4]==~0U?start0:_mp_arg(4),
          start = start0<0?start0 + siz:start0,
          end = end0<0?end0 + siz:end0;
        if (start<0 || start>=siz || end<0 || end>=siz || start>end)
          throw CImgArgumentException("[" cimg_appname "_math_parser] CImg<%s>: Function 'da_remove()': "
                                      "Invalid starting (%d) and ending (%d) positions "
                                      "(not ordered, in range -%d...%d).",
                                      mp.imgout.pixel_type(),start0,end0,siz,siz - 1);
        if (end<siz - 1) // Move remaining data in dynamic array
          cimg_forC(img,c) std::memmove(img.data(0,start,0,c),img.data(0,end + 1,0,c),(siz - 1 - end)*sizeof(T));
        siz-=end - start + 1;
        if (img.height()>32 && siz<img.height()/8) // Reduce size of dynamic array
          img.resize(1,std::max(2*siz + 1,32),1,-100,0);
        img[img._height - 1] = (T)cimg::uint2float(siz);
        return cimg::type<double>::nan();
      }

      static double mp_da_size(_cimg_math_parser& mp) {
        mp_check_list(mp,"da_size");
        const unsigned int ind = (unsigned int)cimg::mod((int)_mp_arg(2),mp.imglist.width());
        CImg<T> &img = mp.imglist[ind];
        const int siz = img?(int)cimg::float2uint((float)img[img._height - 1]):0;
        if (img && (img._width!=1 || img._depth!=1 || siz<0 || siz>img.height() - 1))

View on GitHub (pinned to f788b534b4)