{"record":{"id":"da14eef10d99b45d","repo":"Yalantis/uCrop","slug":"load-pfm-specified-filename-is-null","errorCode":null,"errorMessage":"load_pfm(): Specified filename is (null).","messagePattern":"load_pfm\\(\\): Specified filename is \\(null\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":57771,"sourceCode":"\n    //! Load image from a PFM file \\newinstance.\n    static CImg<T> get_load_pfm(const char *const filename) {\n      return CImg<T>().load_pfm(filename);\n    }\n\n    //! Load image from a PFM file \\overloading.\n    CImg<T>& load_pfm(std::FILE *const file) {\n      return _load_pfm(file,0);\n    }\n\n    //! Load image from a PFM file \\newinstance.\n    static CImg<T> get_load_pfm(std::FILE *const file) {\n      return CImg<T>().load_pfm(file);\n    }\n\n    CImg<T>& _load_pfm(std::FILE *const file, const char *const filename) {\n      if (!file && !filename)\n        throw CImgArgumentException(_cimg_instance\n                                    \"load_pfm(): Specified filename is (null).\",\n                                    cimg_instance);\n\n      std::FILE *const nfile = file?file:cimg::fopen(filename,\"rb\");\n      char pfm_type;\n      CImg<charT> item(16384,1,1,1,0);\n      int W = 0, H = 0, err = 0;\n      double scale = 0;\n      while ((err=std::fscanf(nfile,\"%16383[^\\n]\",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);\n      if (cimg_sscanf(item,\" P%c\",&pfm_type)!=1) {\n        if (!file) cimg::fclose(nfile);\n        throw CImgIOException(_cimg_instance\n                              \"load_pfm(): PFM header not found in file '%s'.\",\n                              cimg_instance,\n                              filename?filename:\"(FILE*)\");\n      }\n      while ((err=std::fscanf(nfile,\" %16383[^\\n]\",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);\n      if ((err=cimg_sscanf(item,\" %d %d\",&W,&H))<2) {","sourceCodeStart":57753,"sourceCodeEnd":57789,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L57753-L57789","documentation":"This is an argument-contract check in CImg<T>::_load_pfm(): the loader requires either an open FILE* or a non-null filename. If both are null, there is nothing to read from, so it throws CImgArgumentException before touching the file system.","triggerScenarios":"Calling load_pfm(nullptr) or load_pfm(nullptr, nullptr); passing a filename variable that is NULL (e.g. from getenv or an uninitialized char*); programmatically constructing a CImg load chain where the source path was never set.","commonSituations":"Configuration lookups returning NULL for the image path; forgetting to default-initialize a filename buffer; calling the FILE*-overload without opening the file first.","solutions":["Pass a valid filename string to load_pfm, e.g. img.load_pfm(\"scene.pfm\").","Open the file yourself and pass the FILE*: FILE* f=fopen(path,\"rb\"); img.load_pfm(f).","Check why the filename is null (failed getenv/lookup) and provide a default or abort early with a clear error.","Guard the call: if (!path) { report; return; } before invoking the loader.","Catch CImgArgumentException in calling code that deals with optional paths."],"exampleFix":"// before\nconst char* path = getenv(\"HDR_FILE\"); // may be NULL\nimg.load_pfm(path);\n// after\nconst char* path = getenv(\"HDR_FILE\");\nif (path) {\n  img.load_pfm(path);\n} else {\n  std::fprintf(stderr, \"HDR_FILE not set\\n\");\n}","handlingStrategy":"type-guard","validationCode":"if (path == nullptr || path[0] == '\\0') {\n  throw std::invalid_argument(\"PFM path must be set before load_pfm\");\n}","typeGuard":"bool hasSource(std::FILE* f, const char* name) { return f != nullptr || name != nullptr; }","tryCatchPattern":"try {\n  img.load_pfm(path);\n} catch (cimg_library::CImgArgumentException& e) {\n  std::cerr << \"No PFM source given: \" << e.what() << \"\\n\";\n}","preventionTips":["Null-check env/config lookups before using the value as a filename","Prefer std::string over raw char* for paths","Always open the FILE* before using FILE*-overloads","Fail fast with a clear message when a required path is unset"],"tags":["cimg","pfm","null-argument","image-loading"],"backgroundTag":"null-argument","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"}