{"record":{"id":"accb094458a7eb22","repo":"Yalantis/uCrop","slug":"cimg-fclose-specified-file-is-null","errorCode":null,"errorMessage":"cimg::fclose(): Specified file is (null).","messagePattern":"cimg::fclose\\(\\): Specified file is \\(null\\)\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":7581,"sourceCode":"          if (_setmode(_fileno(res),0x8000)==-1) res = 0;\n#endif\n        }\n#endif\n      } else res = cimg::std_fopen(path,mode);\n      if (!res) throw CImgIOException(\"cimg::fopen(): Failed to open file '%s' with mode '%s'.\",\n                                      path,mode);\n      return res;\n    }\n\n    //! Close a file.\n    /**\n       \\param file File to close.\n       \\return \\c 0 if file has been closed properly, something else otherwise.\n       \\note Same as <tt>std::fclose()</tt> but display a warning message if\n       the file has not been closed properly.\n    **/\n    inline int fclose(std::FILE *file) {\n      if (!file) { warn(\"cimg::fclose(): Specified file is (null).\"); return 0; }\n      if (file==cimg::_stdin(false) || file==cimg::_stdout(false)) return 0;\n      const int errn = std::fclose(file);\n      if (errn!=0) warn(\"cimg::fclose(): Error code %d returned during file closing.\",\n                        errn);\n      return errn;\n    }\n\n    //! Version of 'fseek()' that supports >=64bits offsets everywhere (for Windows).\n    inline int fseek(FILE *stream, cimg_long offset, int origin) {\n#if defined(WIN64) || defined(_WIN64) || defined(__WIN64__)\n      return _fseeki64(stream,(__int64)offset,origin);\n#else\n      return std::fseek(stream,offset,origin);\n#endif\n    }\n\n    //! Version of 'ftell()' that supports >=64bits offsets everywhere (for Windows).\n    inline cimg_long ftell(FILE *stream) {","sourceCodeStart":7563,"sourceCodeEnd":7599,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L7563-L7599","documentation":"Warning from CImg's cimg::fclose(std::FILE*) helper, a wrapper over std::fclose() that reports problems instead of failing silently. If the passed FILE* is null, it warns \"Specified file is (null)\" and returns 0 without attempting any close. This indicates a bookkeeping bug in the caller: code saved/propagated a null FILE* and still tried to close it.","triggerScenarios":"Calling cimg::fclose(NULL), or fclose on a FILE* variable that was never assigned because the preceding cimg::fopen/std::fopen failed and its null return was not checked; also happens when a load/save path kept a null handle from a skipped branch.","commonSituations":"Unchecked fopen failure (bad path, missing file, no permissions) followed by cleanup code that unconditionally calls cimg::fclose; double-close logic where the first close nulled the pointer but a second path still calls with a stale null; conditional resource handling in custom I/O code built on CImg's file helpers.","solutions":["Check the FILE* for null before calling cimg::fclose, and only close handles that are non-null.","Check the return value of cimg::fopen/std::fopen immediately and handle failure before proceeding to use or close the file.","Restructure cleanup so the close only runs when the file was actually opened (single ownership of the handle).","If this fires inside library code, verify the filename passed to the load/save call exists and is readable so fopen succeeds.","Silence-check logging: since it returns 0, callers that treat the result as success will pass — audit call sites rather than the return value."],"exampleFix":"// before\nstd::FILE *f = cimg::fopen(path,\"r\");\nprocess(f);\ncimg::fclose(f); // warns if fopen failed\n// after\nstd::FILE *f = cimg::fopen(path,\"r\");\nif (f) { process(f); cimg::fclose(f); }\nelse cimg::warn(\"Could not open %s\", path);\n","handlingStrategy":"type-guard","validationCode":"std::FILE *f = cimg::fopen(path, \"r\");\nif (!f) { /* handle open failure: report path, errno */ }\n","typeGuard":"bool is_open(std::FILE *f) { return f != nullptr; }\n// usage: if (is_open(f)) cimg::fclose(f);","tryCatchPattern":"// C API style: guard the handle before closing\nif (file) {\n  cimg::fclose(file);\n  file = nullptr; // prevent double close\n} else {\n  // handle/report the never-opened case\n}","preventionTips":["Always check the FILE* returned by fopen/cimg::fopen before any use or close.","Null the pointer after closing so cleanup cannot double-close or close null.","Structure code so fclose runs only when the corresponding fopen succeeded (RAII wrapper or single-exit guard).","Investigate any 'Specified file is (null)' warning as a caller bug, not a library fault: trace why the handle was null."],"tags":["cimg","file-io","null-pointer","resource-management"],"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"}