{"record":{"id":"4a22dc109cc76750","repo":"Yalantis/uCrop","slug":"cimg-fopen-file-s-specified-mode-is-null","errorCode":null,"errorMessage":"cimg::fopen(): File '%s', specified mode is (null).","messagePattern":"cimg::fopen\\(\\): File '(.+?)', specified mode is \\(null\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":7553,"sourceCode":"    }\n\n    // Open a file (similar to std:: fopen(), but with wide character support on Windows).\n    inline std::FILE *std_fopen(const char *const path, const char *const mode);\n\n\n    //! Open a file.\n    /**\n       \\param path Path of the filename to open.\n       \\param mode C-string describing the opening mode.\n       \\return Opened file.\n       \\note Same as <tt>std::fopen()</tt> but throw a \\c CImgIOException when\n       the specified file cannot be opened, instead of returning \\c 0.\n    **/\n    inline std::FILE *fopen(const char *const path, const char *const mode) {\n      if (!path)\n        throw CImgArgumentException(\"cimg::fopen(): Specified file path is (null).\");\n      if (!mode)\n        throw CImgArgumentException(\"cimg::fopen(): File '%s', specified mode is (null).\",\n                                    path);\n      std::FILE *res = 0;\n      if (*path=='-' && (!path[1] || path[1]=='.')) {\n        res = (*mode=='r')?cimg::_stdin():cimg::_stdout();\n#if cimg_OS==2\n        if (*mode && mode[1]=='b') { // Force stdin/stdout to be in binary mode\n#ifdef __BORLANDC__\n          if (setmode(_fileno(res),0x8000)==-1) res = 0;\n#else\n          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    }","sourceCodeStart":7535,"sourceCodeEnd":7571,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L7535-L7571","documentation":"cimg::fopen() is CImg's thin wrapper over std fopen that validates its arguments before delegating to the OS. It throws CImgArgumentException when the caller passes a NULL mode string (e.g. fopen(path, NULL)); the path is still printed to help identify the call site. This is a pure caller-contract violation, not an I/O problem.","triggerScenarios":"Calling cimg::fopen(path, mode) with mode == NULL, typically when a mode string is built conditionally and ends up unset, or a function parameter is forwarded uninitialized.","commonSituations":"Passing a std::string::c_str() from an empty/unset variable used as mode; refactoring that removed a default \"r\"/\"rb\" argument; uninitialized const char* mode in JNI/Native code paths.","solutions":["Pass a valid mode string literal such as \"r\", \"rb\", \"w\", or \"wb\" as the second argument to cimg::fopen()","Check the caller that computes the mode string and give it a non-NULL default","If the mode may be absent, guard with `if (mode)` before calling, or throw your own descriptive error"],"exampleFix":"// before\nstd::FILE *f = cimg::fopen(path, mode); // mode may be NULL\n// after\nif (!mode) mode = \"rb\";\nstd::FILE *f = cimg::fopen(path, mode);","handlingStrategy":"validation","validationCode":"if (!mode || !*mode) mode = \"rb\"; // or throw app-level error before cimg::fopen","typeGuard":"bool validMode(const char *m){ return m && *m; }","tryCatchPattern":"try { f = cimg::fopen(path, mode); } catch (const cimg_library::CImgArgumentException &e) { log_error(\"bad fopen args: %s\", e.what()); }","preventionTips":["Always pass string-literal modes (\"r\",\"rb\",\"w\",\"wb\")","Never forward a possibly-NULL mode parameter; default it at the function boundary","Enable compiler warnings for uninitialized pointers"],"tags":["cimg","null-pointer","file-io","argument-validation"],"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"}