{"record":{"id":"994e6dd1c4528ee0","repo":"Yalantis/uCrop","slug":"load-png-invalid-png-file-s","errorCode":null,"errorMessage":"load_png(): Invalid PNG file '%s'.","messagePattern":"load_png\\(\\): Invalid PNG file '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":57386,"sourceCode":"        throw CImgIOException(_cimg_instance\n                              \"load_png(): Unable to load data from '(FILE*)' unless libpng is enabled.\",\n                              cimg_instance);\n\n      else return load_other(filename);\n#else\n      // Open file and check for PNG validity.\n#if defined __GNUC__\n      const char *volatile nfilename = filename; // Use 'volatile' to avoid (wrong) g++ warning\n      std::FILE *volatile nfile = file?file:cimg::fopen(nfilename,\"rb\");\n#else\n      const char *nfilename = filename;\n      std::FILE *nfile = file?file:cimg::fopen(nfilename,\"rb\");\n#endif\n      unsigned char pngCheck[8] = {};\n      cimg::fread(pngCheck,8,(std::FILE*)nfile);\n      if (png_sig_cmp(pngCheck,0,8)) {\n        if (!file) cimg::fclose(nfile);\n        throw CImgIOException(_cimg_instance\n                              \"load_png(): Invalid PNG file '%s'.\",\n                              cimg_instance,\n                              nfilename?nfilename:\"(FILE*)\");\n      }\n\n      // Setup PNG structures for read.\n      png_voidp user_error_ptr = 0;\n      png_error_ptr user_error_fn = 0, user_warning_fn = 0;\n      png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,user_error_ptr,user_error_fn,user_warning_fn);\n      if (!png_ptr) {\n        if (!file) cimg::fclose(nfile);\n        throw CImgIOException(_cimg_instance\n                              \"load_png(): Failed to initialize 'png_ptr' structure for file '%s'.\",\n                              cimg_instance,\n                              nfilename?nfilename:\"(FILE*)\");\n      }\n      png_infop info_ptr = png_create_info_struct(png_ptr);\n      if (!info_ptr) {","sourceCodeStart":57368,"sourceCodeEnd":57404,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L57368-L57404","documentation":"After opening the file, CImg reads the first 8 bytes and calls libpng's png_sig_cmp(); if the bytes do not match the PNG signature, the file is not a PNG and CImgIOException is thrown with the filename (or '(FILE*)').","triggerScenarios":"load_png() pointing at a JPEG/GIF/BMP/WebP file, a text/HTML error page saved with a .png extension, a truncated or zero-byte file, or an SVG named .png.","commonSituations":"Wrong extension after renaming files, a download that saved an error page, a URL fetch that returned JSON/HTML instead of image bytes, empty placeholder files in test fixtures.","solutions":["Verify the file really is a PNG (file photo.png, or check the 8-byte header \\x89PNG\\r\\n\\x1a\\n yourself).","Convert/re-export the image to PNG (e.g. magick input.jpg output.png).","If the source is a URL, check the response was a real image (status 200, correct content-type) before saving.","Check the file is not empty or truncated (size > 8 bytes)."],"exampleFix":"// before\nimg.load_png(path); // path may hold a JPEG\n// after\nunsigned char sig[8] = {};\nstd::FILE *f = std::fopen(path, \"rb\");\nif (f) { std::fread(sig,1,8,f); std::fclose(f); }\nif (sig[0]==0x89 && sig[1]=='P') img.load_png(path);\nelse img.load(path); // let CImg sniff the real format","handlingStrategy":"validation","validationCode":"bool isPng(const char *p) {\n  unsigned char s[8] = {};\n  std::FILE *f = std::fopen(p, \"rb\");\n  if (!f) return false;\n  size_t n = std::fread(s,1,8,f); std::fclose(f);\n  return n == 8 && std::memcmp(s, \"\\x89PNG\\r\\n\\x1a\\n\", 8) == 0;\n}","typeGuard":"bool looksLikePng(const unsigned char *hdr, size_t n) {\n  return n >= 8 && std::memcmp(hdr, \"\\x89PNG\\r\\n\\x1a\\n\", 8) == 0;\n}","tryCatchPattern":"try { img.load_png(path); }\ncatch (CImgIOException &e) { std::fprintf(stderr, \"Not a PNG: %s\\n\", path); img.load(path); }","preventionTips":["Check the magic bytes before calling a format-specific loader","Never trust file extensions; sniff content","Validate downloaded images (HTTP status + content-type) before saving"],"tags":["cimg","png","format-validation","corrupt-file"],"backgroundTag":"file-not-found","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"}