{"record":{"id":"ca08033a1190082c","repo":"Yalantis/uCrop","slug":"load-bmp-invalid-offset-d-specified-in-filenam","errorCode":null,"errorMessage":"load_bmp(): Invalid offset %d specified in filename '%s'.","messagePattern":"load_bmp\\(\\): Invalid offset (.+?) specified in filename '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":56814,"sourceCode":"        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\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);","sourceCodeStart":56796,"sourceCodeEnd":56832,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L56796-L56832","documentation":"CImg's load_bmp() rejects a BMP whose header-declared pixel-data offset is negative or extends beyond the file size. The offset field of the BMP header tells where image bits start; a nonsensical value means the file is corrupt or not a valid BMP. CImg throws CImgIOException naming the offending file.","triggerScenarios":"Calling load_bmp()/CImg<T>::load_bmp on a truncated or corrupted .bmp file, or a file whose BITMAPFILEHEADER::bfOffBits exceeds the actual byte count, or passing a non-BMP file with a .bmp extension.","commonSituations":"Partially downloaded BMP, BMP produced by a buggy encoder, renamed file, data corrupted during resource bundling in an Android project (ucrop JNI).","solutions":["Verify the BMP file opens correctly in another tool (e.g. identify from ImageMagick); re-export or re-obtain the file","Re-export the image as BMP from a reliable encoder or convert to PNG and load that","Check that the asset was not truncated during copy/download (compare file size with source)","Validate header fields (bfOffBits <= file size) before handing the file to CImg"],"exampleFix":"// before\nimg.load_bmp(\"corrupt.bmp\");\n// after\nstd::FILE* f = std::fopen(\"corrupt.bmp\",\"rb\");\nunsigned char hdr[26];\nif (fread(hdr,1,26,f)!=26) { /* handle */ }\nunsigned off = hdr[10] | (hdr[11]<<8) | (hdr[12]<<16) | ((unsigned)hdr[13]<<24);\nfseek(f,0,SEEK_END); long size = ftell(f); fclose(f);\nif (off < (unsigned)size) img.load_bmp(\"corrupt.bmp\");","handlingStrategy":"validation","validationCode":"long fileSize = getFileSize(path);\nunsigned off = readU32LE(path, 10); // bfOffBits\nif (off == 0 || off >= (unsigned)fileSize) throw std::runtime_error(\"bad bmp offset\");","typeGuard":"bool isValidBmpOffset(long fileSize, unsigned offset) { return offset > 0 && offset < fileSize; }","tryCatchPattern":"try { img.load_bmp(path); }\ncatch (CImgIOException&) { img.assign(); /* fallback */ }","preventionTips":["Only load BMPs from trusted encoders","Validate header fields before decoding","Prefer PNG for internal storage","Check file integrity after download/transfer"],"tags":["bmp","image-loading","corrupt-file","cimg"],"backgroundTag":"invalid-argument-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"}