{"record":{"id":"7707f2d7f96ee8e4","repo":"Yalantis/uCrop","slug":"save-cpp-specified-filename-is-null","errorCode":null,"errorMessage":"save_cpp(): Specified filename is (null).","messagePattern":"save_cpp\\(\\): Specified filename is \\(null\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":61414,"sourceCode":"      return *this;\n    }\n\n    //! Save image as a .cpp source file.\n    /**\n      \\param filename Filename, as a C-string.\n    **/\n    const CImg<T>& save_cpp(const char *const filename) const {\n      return _save_cpp(0,filename);\n    }\n\n    //! Save image as a .cpp source file \\overloading.\n    const CImg<T>& save_cpp(std::FILE *const file) const {\n      return _save_cpp(file,0);\n    }\n\n    const CImg<T>& _save_cpp(std::FILE *const file, const char *const filename) const {\n      if (!file && !filename)\n        throw CImgArgumentException(_cimg_instance\n                                    \"save_cpp(): Specified filename is (null).\",\n                                    cimg_instance);\n      std::FILE *const nfile = file?file:cimg::fopen(filename,\"w\");\n      CImg<charT> varname(1024); *varname = 0;\n      if (filename) cimg_sscanf(cimg::basename(filename),\"%1023[a-zA-Z0-9_]\",varname._data);\n      if (!*varname) cimg_snprintf(varname,varname._width,\"unnamed\");\n      std::fprintf(nfile,\n                   \"/* Define image '%s' of size %ux%ux%ux%u and type '%s' */\\n\"\n                   \"%s data_%s[] = { %s\\n  \",\n                   varname._data,_width,_height,_depth,_spectrum,pixel_type(),pixel_type(),varname._data,\n                   is_empty()?\"};\":\"\");\n      if (!is_empty()) for (ulongT off = 0, siz = size() - 1; off<=siz; ++off) {\n        std::fprintf(nfile,cimg::type<T>::format(),cimg::type<T>::format((*this)[off]));\n        if (off==siz) std::fprintf(nfile,\" };\\n\");\n        else if (!((off + 1)%16)) std::fprintf(nfile,\",\\n  \");\n        else std::fprintf(nfile,\", \");\n      }\n      if (!file) cimg::fclose(nfile);","sourceCodeStart":61396,"sourceCodeEnd":61432,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L61396-L61432","documentation":"save_cpp() exports the image as C++ source; _save_cpp() throws CImgArgumentException when both the FILE* and filename are null, since it needs an output stream and also derives the variable name from the filename. Same shared-implementation pattern as the other _save_* helpers.","triggerScenarios":"Calling img.save_cpp((std::FILE*)NULL) or img.save_cpp((const char*)NULL); forwarding a NULL FILE* from a failed fopen without a filename fallback.","commonSituations":"Code-generation tooling with optional output paths, refactored calls that dropped the filename argument, fopen errors silently propagating NULL.","solutions":["Provide a valid filename so the variable name can be derived.","Open the FILE* successfully before the FILE*-overload call.","Null-check both arguments at the call site.","Default the output to a fixed name like \"image.cpp\" when unset."],"exampleFix":"// before\nimg.save_cpp(NULL); // no destination\n// after\nimg.save_cpp(\"image.cpp\");","handlingStrategy":"type-guard","validationCode":"if (!file && (!filename || !*filename)) { filename = \"unnamed.cpp\"; }","typeGuard":"bool hasDestination(std::FILE* f, const char* n){ return f!=nullptr || (n!=nullptr && *n!='\\0'); }","tryCatchPattern":"try { img.save_cpp(f); } catch (CImgArgumentException& e) { img.save_cpp(\"unnamed.cpp\"); }","preventionTips":["Always supply a filename so save_cpp can derive the variable name","Null-check fopen results before stream-overload calls","Default output names in code-generation pipelines"],"tags":["cimg","save","null-pointer","code-generation"],"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"}