{"record":{"id":"40cf7b603b17880d","repo":"Yalantis/uCrop","slug":"load-cimg-invalid-specified-size-u-u-u-u","errorCode":null,"errorMessage":"load_cimg(): Invalid specified size (%u,%u,%u,%u) of image %u in file '%s'.","messagePattern":"load_cimg\\(\\): Invalid specified size \\(%u,%u,%u,%u\\) of image %u in file '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":66745,"sourceCode":"}\n#else\n#define _cimgz_load_cimg_case(Tss) \\\n   throw CImgIOException(_cimglist_instance \\\n                         \"load_cimg(): Unable to load compressed data from file '%s' unless zlib is enabled.\", \\\n                         cimglist_instance, \\\n                         filename?filename:\"(FILE*)\");\n#endif\n\n#define _cimg_load_cimg_case(Ts1,Ts2,Ts3,Tss) \\\n      if (!loaded && ((Ts1 && !cimg::strcasecmp(Ts1,str_pixeltype)) || \\\n                      (Ts2 && !cimg::strcasecmp(Ts2,str_pixeltype)) || \\\n                      (Ts3 && !cimg::strcasecmp(Ts3,str_pixeltype)))) { \\\n        const bool is_bool = cimg::type<Tss>::string()==cimg::type<bool>::string(); \\\n        for (unsigned int l = 0; l<N; ++l) { \\\n          j = 0; while ((i=std::fgetc(nfile))!='\\n' && i>=0 && j<255) tmp[j++] = (char)i; tmp[j] = 0; \\\n          W = H = D = C = 0; csiz = 0; \\\n          if ((err = cimg_sscanf(tmp,\"%u %u %u %u #\" cimg_fuint64,&W,&H,&D,&C,&csiz))<4) \\\n            throw CImgIOException(_cimglist_instance \\\n                                  \"load_cimg(): Invalid specified size (%u,%u,%u,%u) of image %u in file '%s'.\", \\\n                                  cimglist_instance, \\\n                                  W,H,D,C,l,filename?filename:(\"(FILE*)\")); \\\n          if (W*H*D*C>0) { \\\n            CImg<T> &img = _data[l]; \\\n            if (err==5) _cimgz_load_cimg_case(Tss) \\\n            else { \\\n              img.assign(W,H,D,C); \\\n              T *ptrd = img._data; \\\n              if (is_bool) { \\\n                CImg<ucharT> raw; \\\n                for (ulongT to_read = img.size(); to_read; ) { \\\n                  raw.assign((unsigned int)std::min(to_read,cimg_iobuffer)); \\\n                  cimg::fread(raw._data,raw._width,nfile); \\\n                  CImg<T>(ptrd,std::min(8*raw._width,(unsigned int)(img.end() - ptrd)),1,1,1,true).\\\n                    _uchar2bool(raw,raw._width,false); \\\n                  to_read-=raw._width; \\\n                } \\","sourceCodeStart":66727,"sourceCodeEnd":66763,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L66727-L66763","documentation":"While reading a multi-image .cimg file, load_cimg() parses each image's header line '%u %u %u %u #<csiz>' and throws if fewer than 4 unsigned dimensions can be extracted (CImg.h:66745). The file is corrupt, truncated, or not a valid .cimg stream.","triggerScenarios":"Calling CImgList::load_cimg(filename) on a file whose per-image header line contains non-numeric or missing dimension fields (sscanf returns <4), e.g. a truncated download or a file corrupted mid-transfer.","commonSituations":"Interrupted FTP/HTTP transfer leaving a partial .cimg; text-mode transfer on Windows corrupting binary data; hand-edited or concatenated .cimg files; using a plain binary file that happens to be passed as .cimg.","solutions":["Regenerate or re-download the .cimg file and verify its integrity (size vs. header-declared csiz).","Open the file in a hex editor and check the header lines match 'W H D C #csiz'.","Confirm the transfer used binary mode and no line-ending mangling.","Validate the header yourself before loading and fall back to re-export from the source."],"exampleFix":"// before\nCImgList<unsigned char> imgs;\nimgs.load_cimg(partialFilePath); // throws on truncated header\n\n// after\nstd::FILE* f = std::fopen(partialFilePath, \"rb\");\nchar line[256];\nunsigned W, H, D, C; unsigned long long csiz;\nbool ok = std::fgets(line, sizeof line, f) &&\n          std::sscanf(line, \"%u %u %u %u #%llu\", &W, &H, &D, &C, &csiz) >= 4;\nstd::fclose(f);\nif (ok) imgs.load_cimg(partialFilePath);\nelse reExportCimgFile(partialFilePath);","handlingStrategy":"validation","validationCode":"bool validCimgHeader(const char* path) {\n  std::FILE* f = std::fopen(path, \"rb\"); if (!f) return false;\n  char line[256] = {0};\n  bool ok = std::fgets(line, sizeof line, f) != nullptr;\n  unsigned N, W, H, D, C; unsigned long long csiz; char pt[64], en[64];\n  if (ok && std::sscanf(line, \"%u %63s %63s\", &N, pt, en) == 3) {\n    ok = true;\n    for (unsigned l = 0; l < N && ok; ++l) {\n      if (!std::fgets(line, sizeof line, f)) { ok = false; break; }\n      ok = std::sscanf(line, \"%u %u %u %u #%llu\", &W,&H,&D,&C,&csiz) >= 4;\n    }\n  } else ok = false;\n  std::fclose(f);\n  return ok;\n}","typeGuard":null,"tryCatchPattern":"try {\n  imgs.load_cimg(path);\n} catch (cimg_library::CImgIOException& e) {\n  logError(\"corrupt .cimg (bad size header): %s\", e.what());\n  reFetchOrReExport(path);\n}","preventionTips":["Verify file size matches dimensions declared in headers after download","Use checksums for .cimg dataset transfers","Always transfer binary data in binary mode","Regenerate rather than hand-edit .cimg headers"],"tags":["cimg","cpp","file-corruption","binary-format","parsing"],"backgroundTag":"invalid-argument-format","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"}