Yalantis/uCrop · error · CImgArgumentException

CImg<%s>::safe_size(): Specified size (%u,%u,%u,%u) exceeds

Error message

CImg<%s>::safe_size(): Specified size (%u,%u,%u,%u) exceeds maximum allowed buffer size of %lu 

What it means

CImg::safe_size() throws CImgArgumentException when the requested image dimensions (dx,dy,dz,dc) multiply out to more pixels than cimg_max_buf_size, the library's configured maximum allocation. safe_size() is called by every CImg constructor/assign before allocating, so this error stops absurdly large allocations up front instead of letting operator new fail or the process be OOM-killed.

Source

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

    typedef typename cimg::last<T,int>::type intT;
    typedef typename cimg::last<T,cimg_ulong>::type ulongT;
    typedef typename cimg::last<T,cimg_long>::type longT;
    typedef typename cimg::last<T,cimg_uint64>::type uint64T;
    typedef typename cimg::last<T,cimg_int64>::type int64T;
    typedef typename cimg::last<T,float>::type floatT;
    typedef typename cimg::last<T,double>::type doubleT;

    // Return 'dx*dy*dz*dc' as a 'size_t' and check no overflow occurs.
    static size_t safe_size(const unsigned int dx, const unsigned int dy,
                            const unsigned int dz, const unsigned int dc) {
      if (!(dx && dy && dz && dc)) return 0;
      size_t siz = (size_t)dx, osiz = siz;
      if ((dy==1 || (siz*=dy)>osiz) &&
          ((osiz = siz), dz==1 || (siz*=dz)>osiz) &&
          ((osiz = siz), dc==1 || (siz*=dc)>osiz) &&
          ((osiz = siz), sizeof(T)==1 || (siz*sizeof(T))>osiz)) {
        if (siz>cimg_max_buf_size){
          throw CImgArgumentException("CImg<%s>::safe_size(): Specified size (%u,%u,%u,%u) exceeds maximum "
                                      "allowed buffer size of %lu ",
                                      pixel_type(),dx,dy,dz,dc,cimg_max_buf_size);
        }
        return siz;
      }
      throw CImgArgumentException("CImg<%s>::safe_size(): Specified size (%u,%u,%u,%u) overflows 'size_t'.",
                                  pixel_type(),dx,dy,dz,dc);
    }

    //@}
    //---------------------------
    //
    //! \name Plugins
    //@{
    //---------------------------
#ifdef cimg_plugin
#include cimg_plugin
#endif

View on GitHub (pinned to f788b534b4)

Solutions

  1. Reduce the requested image size (use a crop, downsample, or process tiles).
  2. Validate width/height/depth/spectrum against realistic limits before constructing the image.
  3. Raise the cimg_max_buf_size configuration if your platform genuinely has the memory (and is 64-bit).
  4. Sanity-check dimensions decoded from image files before calling any CImg constructor with them.

Example fix

// before
CImg<float> img(hdr.width, hdr.height); // hdr from untrusted file, huge values -> throws
// after
if ((unsigned long long)hdr.width * hdr.height > MAX_PIXELS) return reject(hdr);
CImg<float> img(hdr.width, hdr.height);
Defensive patterns

Strategy: validation

Validate before calling

unsigned long long n = (unsigned long long)dx*dy*dz*dc;
if (n > cimg_max_buf_size) return fail_too_large(n);

Try / catch

try { CImg<T> img(dx,dy,dz,dc); }
catch (cimg_library::CImgArgumentException &e) { fprintf(stderr, "%s", e.what()); /* reject or tile */ }

Prevention

When it happens

Trigger: Constructing/assigning an image whose dx*dy*dz*dc exceeds cimg_max_buf_size, e.g. CImg<T>(50000,50000,100,4); passing unvalidated user-supplied width/height values; a bad image header read from a file advertising huge dimensions.

Common situations: Parsing untrusted image headers (a malicious or corrupt file claims gigapixel sizes); integer/logic bugs that feed 0 or garbage into width/height; running a 32-bit build with a small cimg_max_buf_size.

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/27b1c8f3b21b2786. Report an issue: GitHub.