{"record":{"id":"f6ea1318b0a018bb","repo":"Yalantis/uCrop","slug":"load-jpeg-incomplete-data-in-file-s","errorCode":null,"errorMessage":"load_jpeg(): Incomplete data in file '%s'.","messagePattern":"load_jpeg\\(\\): Incomplete data in file '(.+?)'\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":57068,"sourceCode":"      if (cinfo.output_components!=1 && cinfo.output_components!=3 && cinfo.output_components!=4) {\n        if (!file) {\n          cimg::fclose(nfile);\n          return load_other(nfilename);\n        } else\n          throw CImgIOException(_cimg_instance\n                                \"load_jpeg(): Failed to load JPEG data from file '%s'.\",\n                                cimg_instance,nfilename?nfilename:\"(FILE*)\");\n      }\n      CImg<ucharT> buffer(cinfo.output_width*cinfo.output_components);\n      JSAMPROW row_pointer[1];\n      try { assign(cinfo.output_width,cinfo.output_height,1,cinfo.output_components); }\n      catch (...) { if (!file) cimg::fclose(nfile); throw; }\n      T *ptr_r = _data, *ptr_g = _data + 1UL*_width*_height, *ptr_b = _data + 2UL*_width*_height,\n        *ptr_a = _data + 3UL*_width*_height;\n      while (cinfo.output_scanline<cinfo.output_height) {\n        *row_pointer = buffer._data;\n        if (jpeg_read_scanlines(&cinfo,row_pointer,1)!=1) {\n          cimg::warn(_cimg_instance\n                     \"load_jpeg(): Incomplete data in file '%s'.\",\n                     cimg_instance,nfilename?nfilename:\"(FILE*)\");\n          break;\n        }\n        const unsigned char *ptrs = buffer._data;\n        switch (_spectrum) {\n        case 1 : {\n          cimg_forX(*this,x) *(ptr_r++) = (T)*(ptrs++);\n        } break;\n        case 3 : {\n          cimg_forX(*this,x) {\n            *(ptr_r++) = (T)*(ptrs++);\n            *(ptr_g++) = (T)*(ptrs++);\n            *(ptr_b++) = (T)*(ptrs++);\n          }\n        } break;\n        case 4 : {\n          cimg_forX(*this,x) {","sourceCodeStart":57050,"sourceCodeEnd":57086,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L57050-L57086","documentation":"CImg<T>::load_jpeg() decodes a JPEG via libjpeg; if jpeg_read_scanlines() fails to return a scanline partway through the image, it warns non-fatally and breaks, leaving the image with the scanlines decoded so far (rest uninitialized/zero). This indicates truncated or corrupt JPEG data after the header.","triggerScenarios":"Loading a JPEG file that ends prematurely (truncated download/copy), a corrupted bitstream where entropy-coded data is cut off, or a FILE* whose read fails mid-decode.","commonSituations":"Partially downloaded .jpg files, images recovered from damaged storage, interrupted uploads, concatenation/encoding bugs producing headers with dimensions larger than actual data.","solutions":["Verify the JPEG file is complete (re-download/re-copy; check end-of-image marker EOI 0xFFD9)","Validate the source file size against the dimensions declared in the SOF header before decoding","Treat images loaded after this warning as suspect: check returned dimensions/content before use, or rethrow","Use a tolerant/robust JPEG decoder or repair tool (e.g. jpegtran with -restart, gfxutil-style repair) for partially recoverable files"],"exampleFix":"// before\nCImg<unsigned char> img;\nimg.load_jpeg(\"truncated.jpg\"); // partially decoded, no exception\n// after\nCImg<unsigned char> img;\nimg.load_jpeg(\"truncated.jpg\");\nstd::FILE* f = std::fopen(\"truncated.jpg\", \"rb\");\nbool has_eoi = false; int c, prev = 0;\nwhile ((c = std::fgetc(f)) != EOF) { if (prev == 0xFF && c == 0xD9) has_eoi = true; prev = c; }\nstd::fclose(f);\nif (!has_eoi) throw std::runtime_error(\"JPEG truncated (no EOI marker)\");","handlingStrategy":"validation","validationCode":"bool jpeg_complete(const char* path) {\n  std::FILE* f = std::fopen(path, \"rb\"); if (!f) return false;\n  int c, prev = 0, ok = 0;\n  while ((c = std::fgetc(f)) != EOF) { if (prev == 0xFF && c == 0xD9) { ok = 1; break; } prev = c; }\n  std::fclose(f);\n  return ok;\n}\nif (!jpeg_complete(\"img.jpg\")) throw std::runtime_error(\"truncated JPEG\");","typeGuard":"bool has_jpeg_magic(const unsigned char* d, size_t n) {\n  return n >= 4 && d[0] == 0xFF && d[1] == 0xD8 &&\n         d[n-2] == 0xFF && d[n-3] == 0xD9;\n}","tryCatchPattern":null,"preventionTips":["Check for the JPEG EOI marker (FFD9) before trusting a loaded image","Re-download files whose size differs from the source","Validate declared dimensions vs file size before decoding","Treat images from untrusted sources as suspect; verify content after load"],"tags":["jpeg","image-loading","truncated-file","libjpeg","cimg"],"backgroundTag":"file-read-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"}