{"record":{"id":"a5c1d25465acfb92","repo":"Yalantis/uCrop","slug":"load-bmp-invalid-file-size-d-specified-in-file","errorCode":null,"errorMessage":"load_bmp(): Invalid file_size %d specified in filename '%s' (expected %lu).","messagePattern":"load_bmp\\(\\): Invalid file_size (.+?) specified in filename '(.+?)' \\(expected %lu\\)\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":56802,"sourceCode":"        throw CImgIOException(_cimg_instance\n                              \"load_bmp(): Invalid BMP file '%s'.\",\n                              cimg_instance,\n                              filename?filename:\"(FILE*)\");\n      }\n\n      // Read header and pixel buffer.\n      int\n        file_size = header[0x02] + (header[0x03]<<8) + (header[0x04]<<16) + (header[0x05]<<24),\n        offset = header[0x0A] + (header[0x0B]<<8) + (header[0x0C]<<16) + (header[0x0D]<<24),\n        header_size = header[0x0E] + (header[0x0F]<<8) + (header[0x10]<<16) + (header[0x11]<<24),\n        dx = header[0x12] + (header[0x13]<<8) + (header[0x14]<<16) + (header[0x15]<<24),\n        dy = header[0x16] + (header[0x17]<<8) + (header[0x18]<<16) + (header[0x19]<<24),\n        compression = header[0x1E] + (header[0x1F]<<8) + (header[0x20]<<16) + (header[0x21]<<24),\n        nb_colors = header[0x2E] + (header[0x2F]<<8) + (header[0x30]<<16) + (header[0x31]<<24),\n        bpp = header[0x1C] + (header[0x1D]<<8);\n\n      if ((ulongT)file_size!=fsiz)\n        throw CImgIOException(_cimg_instance\n                              \"load_bmp(): Invalid file_size %d specified in filename '%s' (expected %lu).\",\n                              cimg_instance,\n                              file_size,filename?filename:\"(FILE*)\",fsiz);\n\n      if (header_size<0 || header_size>=file_size)\n        throw CImgIOException(_cimg_instance\n                              \"load_bmp(): Invalid header size %d specified in filename '%s'.\",\n                              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","sourceCodeStart":56784,"sourceCodeEnd":56820,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L56784-L56820","documentation":"The BMP header's file_size field (bytes 0x02-0x05) does not match the actual size of the file on disk (fsiz), so load_bmp() throws CImgIOException. Many BMP writers put 0 or a stale value in this field even though the pixel data may be fine.","triggerScenarios":"Loading a BMP whose header claims a size different from the real file length: headers written incorrectly by third-party tools, files modified after writing (appended/truncated), or files where the size field is 0 (some writers omit it).","commonSituations":"Images produced by embedded devices or custom encoders that set file_size=0; truncated or concatenated downloads; editing tools that rewrite pixel data without updating the header.","solutions":["Open the BMP in an editor/image tool and re-save so the header file_size is correct.","Patch bytes 0x02-0x05 to the actual file size before loading.","Use a more tolerant loader (img.load() may route to a different decoder, or decode via platform APIs).","Check for truncation: compare real file size with expected size from the source of the file."],"exampleFix":"// before\nimg.load_bmp(path); // header file_size=0\n// after\nstd::fstream f(path, std::ios::binary|std::ios::in|std::ios::out);\nf.seekp(2); uint32_t sz = fileSize(path); f.write((char*)&sz,4); f.close();\nimg.load_bmp(path);","handlingStrategy":"try-catch","validationCode":"bool bmpFileSizeFieldSane(const char* p){ std::ifstream f(p,std::ios::binary); unsigned char h[6]={0}; f.read((char*)h,6); uint32_t declared=h[2]|(h[3]<<8)|(h[4]<<16)|((uint32_t)h[5]<<24); std::ifstream e(p,std::ios::binary|std::ios::ate); uint32_t actual=(uint32_t)e.tellg(); return declared==actual; }","typeGuard":null,"tryCatchPattern":"try { img.load_bmp(path); } catch (cimg_library::CImgIOException& e) { /* header file_size mismatch: patch or transcode */ }","preventionTips":["Re-save BMPs from unknown encoders before loading","Reject or repair files where the size field is 0","Check downloads for truncation before loading"],"tags":["cimg","bmp","header-validation","file-corruption"],"backgroundTag":"invalid-header-value","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"}