{"record":{"id":"c1474b6c407f03cd","repo":"Yalantis/uCrop","slug":"save-specified-filename-is-null","errorCode":null,"errorMessage":"save(): Specified filename is (null).","messagePattern":"save\\(\\): Specified filename is \\(null\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":61253,"sourceCode":"        }\n      }\n      disp._normalization = old_normalization;\n      return *this;\n    }\n\n    //! Save image as a file.\n    /**\n       \\param filename Filename, as a C-string.\n       \\param number When positive, represents an index added to the filename. Otherwise, no number is added.\n       \\param digits Number of digits used for adding the number to the filename.\n       \\note\n       - The used file format is defined by the file extension in the filename \\p filename.\n       - Parameter \\p number can be used to add a 6-digit number to the filename before saving.\n\n    **/\n    const CImg<T>& save(const char *const filename, const int number=-1, const unsigned int digits=6) const {\n      if (!filename)\n        throw CImgArgumentException(_cimg_instance\n                                    \"save(): Specified filename is (null).\",\n                                    cimg_instance);\n      // Do not test for empty instances, since .cimg format is able to manage empty instances.\n      const bool is_stdout = *filename=='-' && (!filename[1] || filename[1]=='.');\n      const char *const ext = cimg::split_filename(filename);\n      CImg<charT> nfilename(1024);\n      const char *const fn = is_stdout?filename:(number>=0)?cimg::number_filename(filename,number,digits,nfilename):\n        filename;\n\n#ifdef cimg_save_plugin\n      cimg_save_plugin(fn);\n#endif\n#ifdef cimg_save_plugin1\n      cimg_save_plugin1(fn);\n#endif\n#ifdef cimg_save_plugin2\n      cimg_save_plugin2(fn);\n#endif","sourceCodeStart":61235,"sourceCodeEnd":61271,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L61235-L61271","documentation":"CImg<T>::save() requires a non-null filename string; passing NULL throws CImgArgumentException before any file I/O. The format is chosen by file extension, so a filename is mandatory (stdout saving uses the special \"-\" name, not NULL).","triggerScenarios":"Calling img.save(NULL) directly, or passing a filename variable that was never initialized / returned NULL from a failed lookup into save().","commonSituations":"Filename built from a failed getenv() or config read returning NULL, C-string from a function that returns NULL on error, wrapping code forwarding optional path arguments unchecked.","solutions":["Check the filename pointer for NULL before calling save().","Use an empty-string/stdout convention (\"-\") instead of NULL when output destination is optional.","Fix the upstream producer of the filename (config/env/API) that returned NULL.","In C++, prefer std::string and pass .c_str() only after validation."],"exampleFix":"// before\nconst char* name = getenv(\"OUT\"); img.save(name); // NULL if unset\n// after\nconst char* name = getenv(\"OUT\");\nif (name) img.save(name); else img.save(\"out.png\");","handlingStrategy":"type-guard","validationCode":"if (!filename || !*filename) { /* substitute default or abort */ }","typeGuard":"bool validPath(const char* p){ return p!=nullptr && *p!='\\0'; }","tryCatchPattern":"try { img.save(name); } catch (CImgArgumentException& e) { fprintf(stderr,\"Save failed: %s\\n\", e.what()); }","preventionTips":["Never pass raw getenv()/config results unchecked into save()","Use std::string internally and only .c_str() after validation","Adopt a default output filename when the configured one is missing"],"tags":["cimg","save","null-pointer","argument-error"],"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"}