{"record":{"id":"e79ffe12bd7087d2","repo":"Yalantis/uCrop","slug":"load-bmp-file-size-lu-for-filename-s-does-n","errorCode":null,"errorMessage":"load_bmp(): File size %lu for filename '%s' does not match encoded image dimensions (%d,%d).","messagePattern":"load_bmp\\(\\): File size %lu for filename '(.+?)' does not match encoded image dimensions \\((.+?),(.+?)\\)\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":56828,"sourceCode":"                              cimg_instance,\n                              header_size,filename?filename:\"(FILE*)\");\n\n      if (offset<0 || offset>=file_size)\n        throw CImgIOException(_cimg_instance\n                              \"load_bmp(): Invalid offset %d specified in filename '%s'.\",\n                              cimg_instance,\n                              offset,filename?filename:\"(FILE*)\");\n\n      if (header_size>40) cimg::fseek(nfile,header_size - 40,SEEK_CUR);\n      const int\n        dx_bytes = (bpp==1)?(dx/8 + (dx%8?1:0)):((bpp==4)?(dx/2 + (dx%2)):(int)((longT)dx*bpp/8)),\n        align_bytes = (4 - dx_bytes%4)%4;\n      const ulongT\n        cimg_iobuffer = (ulongT)24*1024*1024,\n        buf_size = (ulongT)cimg::abs(dy)*(dx_bytes + align_bytes);\n\n      if (buf_size>=fsiz)\n        throw CImgIOException(_cimg_instance\n                              \"load_bmp(): File size %lu for filename '%s' does not match \"\n                              \"encoded image dimensions (%d,%d).\",\n                              cimg_instance,\n                              (long)fsiz,filename?filename:\"(FILE*)\",dx,dy);\n\n      CImg<intT> colormap;\n      if (bpp<16) { if (!nb_colors) nb_colors = 1<<bpp; } else nb_colors = 0;\n      if (nb_colors) { colormap.assign(nb_colors); cimg::fread(colormap._data,nb_colors,nfile); }\n\n      const int xoffset = offset - 14 - header_size - 4*nb_colors;\n      if (xoffset<0 || xoffset>=file_size)\n        throw CImgIOException(_cimg_instance\n                              \"load_bmp(): Malformed header in filename '%s'.\",\n                              cimg_instance,\n                              filename?filename:\"(FILE*)\");\n      cimg::fseek(nfile,xoffset,SEEK_CUR);\n\n      CImg<ucharT> buffer;","sourceCodeStart":56810,"sourceCodeEnd":56846,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L56810-L56846","documentation":"CImg's load_bmp() computes the buffer size implied by the header's width/height and rejects the file when this buffer is not smaller than the actual file size, i.e. the declared dimensions are inconsistent with the file's length. This guards against corrupted or fabricated headers.","triggerScenarios":"Loading a .bmp whose BITMAPINFOHEADER width/height imply more pixel bytes than the file actually contains (e.g. dy*dx*(bytes/row+align) >= file size); truncated download or a hand-edited header.","commonSituations":"Incomplete downloads, files modified by scripts, BMPs truncated by asset packaging tools, malicious/fuzzed inputs to uCrop's native loader.","solutions":["Re-obtain or re-export the BMP from a trustworthy source","Check the actual file size against the header dimensions with a tool like `identify` before loading","Convert the image to PNG/JPEG and load that format instead","Catch CImgIOException and fall back to another decoder"],"exampleFix":"// before\nimg.load_bmp(\"truncated.bmp\");\n// after\ntry { img.load_bmp(\"truncated.bmp\"); }\ncatch (CImgIOException&) { img.load(\"truncated.png\"); } // re-export a complete file","handlingStrategy":"validation","validationCode":"unsigned dx = readS32LE(path,18), dy = readS32LE(path,22);\nlong fsiz = getFileSize(path);\nif ((long)dx*dy*4 + 54 > fsiz) throw std::runtime_error(\"bmp size mismatch\");","typeGuard":"bool bmpDimensionsMatchSize(long fsiz, int dx, int dy) { return (long)dy*((dx*4+3)/4*4)+54 < fsiz; }","tryCatchPattern":"try { img.load_bmp(path); }\ncatch (CImgIOException& e) { fprintf(stderr,\"%s\\n\",e.what()); img.assign(); }","preventionTips":["Compare header dimensions with actual file size before load","Re-download truncated files","Avoid hand-editing binary headers","Use checksums to detect corruption"],"tags":["bmp","image-loading","corrupt-file","cimg"],"backgroundTag":"schema-validation-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"}