{"record":{"id":"8b6f13e77b4f87ed","repo":"Yalantis/uCrop","slug":"save-cimg-specified-filename-is-null","errorCode":null,"errorMessage":"save_cimg(): Specified filename is (null).","messagePattern":"save_cimg\\(\\): Specified filename is \\(null\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":68148,"sourceCode":"          }\n        }\n      }\n      if (!file) cimg::fclose(nfile);\n      return *this;\n    }\n\n    //! Save list into a .cimg file.\n    /**\n       \\param filename Filename to write data to.\n       \\param is_compressed Tells if data compression must be enabled.\n    **/\n    const CImgList<T>& save_cimg(const char *const filename, const bool is_compressed=false) const {\n      return _save_cimg(0,filename,is_compressed);\n    }\n\n    const CImgList<T>& _save_cimg(std::FILE *const file, const char *const filename, const bool is_compressed) const {\n      if (!file && !filename)\n        throw CImgArgumentException(_cimglist_instance\n                                    \"save_cimg(): Specified filename is (null).\",\n                                    cimglist_instance);\n#ifndef cimg_use_zlib\n      if (is_compressed)\n        cimg::warn(_cimglist_instance\n                   \"save_cimg(): Unable to save compressed data in file '%s' unless zlib is enabled, \"\n                   \"saving them uncompressed.\",\n                   cimglist_instance,\n                   filename?filename:\"(FILE*)\");\n#endif\n      const char *const ptype = pixel_type(), *const etype = cimg::endianness()?\"big\":\"little\";\n      std::FILE *const nfile = file?file:cimg::fopen(filename,\"wb\");\n      const bool is_bool = ptype==cimg::type<bool>::string();\n      std::fprintf(nfile,\"%u %s %s_endian\\n\",_width,ptype,etype);\n\n      cimglist_for(*this,l) {\n        const CImg<T>& img = _data[l];\n        std::fprintf(nfile,\"%u %u %u %u\",img._width,img._height,img._depth,img._spectrum);","sourceCodeStart":68130,"sourceCodeEnd":68166,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L68130-L68166","documentation":"CImgList::save_cimg delegates to _save_cimg, which requires either an open std::FILE* or a non-null filename. When both are null there is no output target, so a CImgArgumentException is thrown immediately. It protects the library from dereferencing a null path later during fopen.","triggerScenarios":"Calling save_cimg(nullptr) or save_cimg with a char* variable that is null at runtime (e.g. an unset filename from parsed options).","commonSituations":"Command-line parsing leaving the output filename unset; a function parameter forwarded straight through without a null check; refactoring from the FILE*-based overload to the filename overload and passing 0.","solutions":["Pass a valid non-null filename string, e.g. list.save_cimg(\"out.cimg\").","Check the filename for null/empty before calling and substitute a default output path.","Open a std::FILE* yourself and use the _save_cimg/file overload if you need stream control."],"exampleFix":"// before\nconst char *out = getenv(\"CIMG_OUT\"); // may be NULL\nlist.save_cimg(out);\n// after\nconst char *out = getenv(\"CIMG_OUT\");\nif (!out) out = \"out.cimg\";\nlist.save_cimg(out);","handlingStrategy":"validation","validationCode":"if (!filename || !*filename) { filename = \"out.cimg\"; }\nlist.save_cimg(filename);","typeGuard":"bool hasPath(const char *p){ return p && *p; }","tryCatchPattern":"try { list.save_cimg(filename); } catch (CImgArgumentException &e) { std::fprintf(stderr, \"no output file: %s\", e.what()); }","preventionTips":["Always initialize output filename variables","Null-check paths forwarded from CLI/config parsing","Prefer std::string with a default value over raw char*"],"tags":["argument-validation","null-pointer","image-io"],"backgroundTag":"null-argument","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}