{"record":{"id":"f2ed50c880ba5394","repo":"Yalantis/uCrop","slug":"load-bmp-invalid-header-size-d-specified-in-fi","errorCode":null,"errorMessage":"load_bmp(): Invalid header size %d specified in filename '%s'.","messagePattern":"load_bmp\\(\\): Invalid header size (.+?) specified in filename '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":56808,"sourceCode":"      // 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\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","sourceCodeStart":56790,"sourceCodeEnd":56826,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L56790-L56826","documentation":"The BMP's header_size field (bytes 0x0E-0x11) is negative or not smaller than the file size, which is impossible for a valid BMP, so load_bmp() throws CImgIOException. It is a sanity check on the DIB header size before reading dimensions and pixel offset.","triggerScenarios":"Loading a BMP with a corrupted or maliciously/erroneously written header_size — e.g. wrong endianness when written, header bytes overwritten, or a non-BMP file that passed the 'BM' magic check by chance.","commonSituations":"Files produced by buggy custom BMP encoders; files that were partially overwritten; hand-crafted or fuzzed inputs in image-processing pipelines.","solutions":["Re-save the BMP with a standard tool so the DIB header size is 40 (BITMAPINFOHEADER) or 124 (BITMAPV5).","Validate bytes 0x0E-0x11 encode a sane header size (>=12 and < file_size) before loading.","Use a tolerant platform decoder and re-encode to a clean BMP.","Treat the file as corrupt: re-export from the original source."],"exampleFix":"// before\nimg.load_bmp(path); // header_size garbage\n// after\nif (!bmpHeaderSizeSane(path)) reencodeToBmp(path, cleanPath);\nimg.load_bmp(cleanPath);","handlingStrategy":"try-catch","validationCode":"bool bmpHeaderSizeSane(const char* p){ std::ifstream f(p,std::ios::binary); unsigned char h[0x12]={0}; f.read((char*)h,0x12); int32_t hs=h[0xE]|(h[0xF]<<8)|(h[0x10]<<16)|((int32_t)h[0x11]<<24); std::ifstream e(p,std::ios::binary|std::ios::ate); int64_t sz=e.tellg(); return hs>=12 && hs<sz; }","typeGuard":null,"tryCatchPattern":"try { img.load_bmp(path); } catch (cimg_library::CImgIOException& e) { /* corrupt DIB header: transcode via tolerant decoder */ }","preventionTips":["Validate header_size (bytes 0x0E-0x11) is 12..124 and < file size before load","Treat BMPs from custom encoders as suspect and re-encode","Use platform decoders for untrusted image inputs"],"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-14T11:17:12.474Z"}