{"record":{"id":"ccf860884f56b0fe","repo":"Yalantis/uCrop","slug":"load-cimg-invalid-specified-coordinates-u-u-ccf860","errorCode":null,"errorMessage":"load_cimg(): Invalid specified coordinates [%u](%u,%u,%u,%u) -> [%u](%u,%u,%u,%u) because file '%s' contains only %u images.","messagePattern":"load_cimg\\(\\): Invalid specified coordinates \\[%u\\]\\(%u,%u,%u,%u\\) -> \\[%u\\]\\(%u,%u,%u,%u\\) because file '(.+?)' contains only %u images\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":66988,"sourceCode":"      CImg<charT> tmp(256), str_pixeltype(256), str_endian(256);\n      *tmp = *str_pixeltype = *str_endian = 0;\n      unsigned int j, N, W, H, D, C;\n      int i, err;\n      j = 0; while ((i=std::fgetc(nfile))!='\\n' && i!=EOF && j<256) tmp[j++] = (char)i; tmp[j] = 0;\n      err = cimg_sscanf(tmp,\"%u%*c%255[A-Za-z123468_]%*c%255[sA-Za-z_ ]\",\n                        &N,str_pixeltype._data,str_endian._data);\n      if (err<2) {\n        if (!file) cimg::fclose(nfile);\n        throw CImgIOException(_cimglist_instance\n                              \"load_cimg(): CImg header not found in file '%s'.\",\n                              cimglist_instance,\n                              filename?filename:\"(FILE*)\");\n      }\n      if (!cimg::strncasecmp(\"little\",str_endian,6)) endian = false;\n      else if (!cimg::strncasecmp(\"big\",str_endian,3)) endian = true;\n      nn1 = n1==~0U?N - 1:n1;\n      if (nn1>=N)\n        throw CImgArgumentException(_cimglist_instance\n                                    \"load_cimg(): Invalid specified coordinates [%u](%u,%u,%u,%u) -> [%u](%u,%u,%u,%u) \"\n                                    \"because file '%s' contains only %u images.\",\n                                    cimglist_instance,\n                                    n0,x0,y0,z0,c0,n1,x1,y1,z1,c1,filename?filename:\"(FILE*)\",N);\n      assign(1 + nn1 - n0);\n      _cimg_load_cimg_case2(\"bool\",0,0,cimg_uint8);\n      _cimg_load_cimg_case2(\"uint8\",\"unsigned char\",\"uchar\",cimg_uint8);\n      _cimg_load_cimg_case2(\"int8\",0,0,cimg_int8);\n      _cimg_load_cimg_case2(\"char\",0,0,char);\n      _cimg_load_cimg_case2(\"uint16\",\"unsigned_short\",\"ushort\",cimg_uint16);\n      _cimg_load_cimg_case2(\"int16\",\"short\",0,cimg_int16);\n      _cimg_load_cimg_case2(\"uint32\",\"unsigned_int\",\"uint\",cimg_uint32);\n      _cimg_load_cimg_case2(\"int32\",\"int\",0,cimg_int32);\n      _cimg_load_cimg_case2(\"unsigned_long\",\"ulong\",0,cimg_ulong);\n      _cimg_load_cimg_case2(\"long\",0,0,cimg_long);\n      _cimg_load_cimg_case2(\"uint64\",\"unsigned_int64\",0,cimg_uint64);\n      _cimg_load_cimg_case2(\"int64\",0,0,cimg_int64);\n      _cimg_load_cimg_case2(\"float32\",\"float\",0,cimg_float32);","sourceCodeStart":66970,"sourceCodeEnd":67006,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L66970-L67006","documentation":"CImgList::load_cimg() reads a sequence of images from a .cimg raw file. Before reading it verifies the requested end index: the caller asked for images up to index nn1 but the file header says the file only contains N images. CImg throws CImgArgumentException to stop the load before reading out of bounds.","triggerScenarios":"Calling load_cimg(filename, n0,x0,y0,z0,c0, n1,x1,y1,z1,c1) where n1 (default ~0U meaning 'last image') resolves to nn1 >= N, the actual number of images stored in the file per its header.","commonSituations":"Hardcoded image-index ranges that assume a larger .cimg file; the file was truncated, regenerated with fewer images, or written by a different version; passing explicit n1 values copied from another dataset; off-by-one when N images means valid indices 0..N-1.","solutions":["Check the number of images in the .cimg file (its header stores N) and clamp n1 to at most N-1","Use the default n1 = ~0U to load all images instead of a hardcoded upper bound","Regenerate or re-export the .cimg file so it actually contains the images you request","Verify you are loading the intended file (path/logged N) - a wrong or truncated file is common"],"exampleFix":"// before\nimgs.load_cimg(\"seq.cimg\", 0,0,0,0,0, 99,0,0,0,0); // assumes 100 images\n// after\nconst unsigned int n1 = std::min(99u, countImagesInCimg(\"seq.cimg\") - 1);\nimgs.load_cimg(\"seq.cimg\", 0,0,0,0,0, n1,0,0,0,0);","handlingStrategy":"validation","validationCode":"unsigned int nImages = readCimgHeaderImageCount(path); // parse N from header\nif (n1 != ~0U && n1 >= nImages) n1 = nImages - 1;","typeGuard":null,"tryCatchPattern":"try { imgs.load_cimg(path, ...); }\ncatch (CImgArgumentException& e) { fprintf(stderr, \"cimg range exceeds file: %s\\n\", e.what()); imgs.assign(); }","preventionTips":["Use n1 = ~0U (load all) unless you know the count","Log the file's image count N when loading .cimg files","Never assume a file contains a fixed number of images"],"tags":["cimg","image-loading","argument-validation","index-out-of-range"],"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-14T11:17:12.474Z"}