{"record":{"id":"0ee3eb67cbab5ba6","repo":"Yalantis/uCrop","slug":"save-cimg-invalid-size-u-u-u-u-of-image","errorCode":null,"errorMessage":"save_cimg(): Invalid size (%u,%u,%u,%u) of image[%u], for file '%s'.","messagePattern":"save_cimg\\(\\): Invalid size \\(%u,%u,%u,%u\\) of image\\[%u\\], for file '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":68242,"sourceCode":"       \\param is_compressed Tells if data compression must be enabled.\n    **/\n    const CImgList<T>& save_cimg(std::FILE *file, const bool is_compressed=false) const {\n      return _save_cimg(file,0,is_compressed);\n    }\n\n    const CImgList<T>& _save_cimg(std::FILE *const file, const char *const filename,\n                                 const unsigned int n0,\n                                 const unsigned int x0, const unsigned int y0,\n                                 const unsigned int z0, const unsigned int c0) const {\n#define _cimg_save_cimg_case(Ts1,Ts2,Ts3,Tss) \\\n      if (!saved && ((Ts1 && !cimg::strcasecmp(Ts1,str_pixeltype)) || \\\n                     (Ts2 && !cimg::strcasecmp(Ts2,str_pixeltype)) || \\\n                     (Ts3 && !cimg::strcasecmp(Ts3,str_pixeltype)))) { \\\n        for (unsigned int l = 0; l<lmax; ++l) { \\\n          j = 0; while ((i=std::fgetc(nfile))!='\\n') tmp[j++]=(char)i; tmp[j] = 0; \\\n          W = H = D = C = 0; \\\n          if (cimg_sscanf(tmp,\"%u %u %u %u\",&W,&H,&D,&C)!=4) \\\n            throw CImgIOException(_cimglist_instance \\\n                                  \"save_cimg(): Invalid size (%u,%u,%u,%u) of image[%u], for file '%s'.\", \\\n                                  cimglist_instance, \\\n                                  W,H,D,C,l,filename?filename:\"(FILE*)\"); \\\n          if (W*H*D*C>0) { \\\n            if (l<n0 || x0>=W || y0>=H || z0>=D || c0>=D) cimg::fseek(nfile,W*H*D*C*sizeof(Tss),SEEK_CUR); \\\n            else { \\\n              const CImg<T>& img = (*this)[l - n0]; \\\n              const T *ptrs = img._data; \\\n              const unsigned int \\\n                x1 = x0 + img._width - 1, \\\n                y1 = y0 + img._height - 1, \\\n                z1 = z0 + img._depth - 1, \\\n                c1 = c0 + img._spectrum - 1, \\\n                nx1 = x1>=W?W - 1:x1, \\\n                ny1 = y1>=H?H - 1:y1, \\\n                nz1 = z1>=D?D - 1:z1, \\\n                nc1 = c1>=C?C - 1:c1; \\\n              CImg<Tss> raw(1 + nx1 - x0); \\","sourceCodeStart":68224,"sourceCodeEnd":68260,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L68224-L68260","documentation":"Inside the _cimg_save_cimg_case macro of CImgList::save_cimg, each image's 4D size is read back from the target file and parsed with sscanf; if the line does not yield exactly four unsigned values (W,H,D,C), the header/data in the file is malformed. The library throws CImgIOException reporting the (possibly zero) parsed size, the image index and the file. This means the .cimg file being updated/overwritten does not have the expected per-image dimension line.","triggerScenarios":"Calling save_cimg on an existing .cimg file whose per-image header line 'W H D C' is corrupted, truncated, or in the wrong format; the file was hand-edited or produced by an incompatible writer.","commonSituations":"Partially written or truncated .cimg files after a crash; files manually edited; version/format mismatch where the header layout changed.","solutions":["Regenerate or re-save the target .cimg file from a valid source instead of updating it in place.","Validate the file's header lines (each image needs 'W H D C') before calling save_cimg in update mode.","Open a fresh file: use save_cimg(filename) on a new path rather than rb+ update of a damaged file."],"exampleFix":"// before\nlist.save_cimg(\"corrupted.cimg\"); // update mode on damaged file\n// after\nstd::remove(\"corrupted.cimg\");\nlist.save_cimg(\"corrupted.cimg\"); // fresh write, no in-place update of bad header","handlingStrategy":"validation","validationCode":"// sniff header line before updating an existing .cimg file\nstd::FILE *f = std::fopen(path, \"rb\");\nint ok = 0; unsigned W,H,D,C;\nchar line[256];\nif (f && std::fgets(line, sizeof line, f)) ok = (std::sscanf(line, \"%u %u %u %u\", &W,&H,&D,&C) == 4);\nif (f) std::fclose(f);\nif (!ok) std::remove(path); // regenerate instead of in-place update","typeGuard":null,"tryCatchPattern":"try { list.save_cimg(path); } catch (CImgIOException &e) { std::remove(path); list.save_cimg(path); }","preventionTips":["Never hand-edit .cimg files","Close files properly to avoid truncated writes","Regenerate rather than update damaged .cimg files"],"tags":["file-io","corrupt-file","image-io"],"backgroundTag":"file-read-failed","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"}