{"record":{"id":"27b1c8f3b21b2786","repo":"Yalantis/uCrop","slug":"cimg-s-safe-size-specified-size-u-u-u-u","errorCode":null,"errorMessage":"CImg<%s>::safe_size(): Specified size (%u,%u,%u,%u) exceeds maximum allowed buffer size of %lu ","messagePattern":"CImg<(.+?)>::safe_size\\(\\): Specified size \\(%u,%u,%u,%u\\) exceeds maximum allowed buffer size of %lu ","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":12850,"sourceCode":"    typedef typename cimg::last<T,int>::type intT;\n    typedef typename cimg::last<T,cimg_ulong>::type ulongT;\n    typedef typename cimg::last<T,cimg_long>::type longT;\n    typedef typename cimg::last<T,cimg_uint64>::type uint64T;\n    typedef typename cimg::last<T,cimg_int64>::type int64T;\n    typedef typename cimg::last<T,float>::type floatT;\n    typedef typename cimg::last<T,double>::type doubleT;\n\n    // Return 'dx*dy*dz*dc' as a 'size_t' and check no overflow occurs.\n    static size_t safe_size(const unsigned int dx, const unsigned int dy,\n                            const unsigned int dz, const unsigned int dc) {\n      if (!(dx && dy && dz && dc)) return 0;\n      size_t siz = (size_t)dx, osiz = siz;\n      if ((dy==1 || (siz*=dy)>osiz) &&\n          ((osiz = siz), dz==1 || (siz*=dz)>osiz) &&\n          ((osiz = siz), dc==1 || (siz*=dc)>osiz) &&\n          ((osiz = siz), sizeof(T)==1 || (siz*sizeof(T))>osiz)) {\n        if (siz>cimg_max_buf_size){\n          throw CImgArgumentException(\"CImg<%s>::safe_size(): Specified size (%u,%u,%u,%u) exceeds maximum \"\n                                      \"allowed buffer size of %lu \",\n                                      pixel_type(),dx,dy,dz,dc,cimg_max_buf_size);\n        }\n        return siz;\n      }\n      throw CImgArgumentException(\"CImg<%s>::safe_size(): Specified size (%u,%u,%u,%u) overflows 'size_t'.\",\n                                  pixel_type(),dx,dy,dz,dc);\n    }\n\n    //@}\n    //---------------------------\n    //\n    //! \\name Plugins\n    //@{\n    //---------------------------\n#ifdef cimg_plugin\n#include cimg_plugin\n#endif","sourceCodeStart":12832,"sourceCodeEnd":12868,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L12832-L12868","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Reduce the requested image size (use a crop, downsample, or process tiles).","Validate width/height/depth/spectrum against realistic limits before constructing the image.","Raise the cimg_max_buf_size configuration if your platform genuinely has the memory (and is 64-bit).","Sanity-check dimensions decoded from image files before calling any CImg constructor with them."],"exampleFix":"// before\nCImg<float> img(hdr.width, hdr.height); // hdr from untrusted file, huge values -> throws\n// after\nif ((unsigned long long)hdr.width * hdr.height > MAX_PIXELS) return reject(hdr);\nCImg<float> img(hdr.width, hdr.height);","handlingStrategy":"validation","validationCode":"unsigned long long n = (unsigned long long)dx*dy*dz*dc;\nif (n > cimg_max_buf_size) return fail_too_large(n);","typeGuard":null,"tryCatchPattern":"try { CImg<T> img(dx,dy,dz,dc); }\ncatch (cimg_library::CImgArgumentException &e) { fprintf(stderr, \"%s\", e.what()); /* reject or tile */ }","preventionTips":["Validate all dimension inputs (especially from file headers) against app-level maximums.","Process large data in tiles or ROI crops.","Verify cimg_max_buf_size fits your platform's realistic memory budget."],"tags":["cimg","buffer-size","memory","argument-validation"],"backgroundTag":"value-out-of-range","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"}