{"record":{"id":"1187e5bd05630f36","repo":"Yalantis/uCrop","slug":"load-bmp-malformed-header-in-filename-s","errorCode":null,"errorMessage":"load_bmp(): Malformed header in filename '%s'.","messagePattern":"load_bmp\\(\\): Malformed header in filename '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":56840,"sourceCode":"        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;\n      if (buf_size<cimg_iobuffer) {\n        buffer.assign(buf_size,1,1,1,0);\n        cimg::fread(buffer._data,buf_size,nfile);\n      } else buffer.assign(dx_bytes + align_bytes);\n      unsigned char *ptrs = buffer;\n\n      // Decompress buffer (if necessary).\n      if (compression==1 || compression==2) {\n        if (file)\n          throw CImgIOException(_cimg_instance\n                                \"load_bmp(): Unable to load compressed data from '(*FILE)' inputs.\",\n                                cimg_instance);","sourceCodeStart":56822,"sourceCodeEnd":56858,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L56822-L56858","documentation":"After reading the colormap, load_bmp() derives the remaining pixel offset (xoffset = offset - 14 - header_size - 4*nb_colors) and rejects the file when this value is negative or exceeds the file size. It indicates internal header fields (offset, header_size, palette size) that are mutually inconsistent.","triggerScenarios":"Loading a .bmp whose bfOffBits does not equal 14 + biSize + palette bytes; a header_size > 40 with mismatched offset; a palette-size field inconsistent with the actual offset.","commonSituations":"Non-standard BMP variants (OS/2 vs Windows headers), files written by buggy encoders, corrupted downloads, files with padding bytes the encoder accounted for differently.","solutions":["Re-export the BMP with a standard encoder (e.g. ImageMagick: convert in.png out.bmp)","Use PNG instead of BMP to avoid legacy header quirks","Inspect the header with a hex editor to confirm bfOffBits and biSize consistency","Validate the header fields yourself before calling load_bmp"],"exampleFix":"// before\nimg.load_bmp(\"odd_header.bmp\");\n// after\n// re-export with standard encoder first, then:\nimg.load_bmp(\"odd_header_fixed.bmp\"); // convert odd_header.bmp fixed.bmp","handlingStrategy":"validation","validationCode":"unsigned offset = readU32LE(path,10);\nunsigned headerSize = readU32LE(path,14);\nif (offset < 14 + headerSize) throw std::runtime_error(\"inconsistent bmp header\");","typeGuard":"bool hasConsistentBmpHeader(unsigned offset, unsigned headerSize, unsigned nbColors) { return offset >= 14u + headerSize + 4u*nbColors; }","tryCatchPattern":"try { img.load_bmp(path); }\ncatch (CImgIOException&) { img.load(convertToPng(path)); }","preventionTips":["Re-export BMPs with modern encoders","Use PNG to avoid legacy header variants","Inspect unusual files in a hex editor","Reject BMPs with header_size or offset anomalies at ingestion"],"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"}