{"record":{"id":"d919ca2619faf91e","repo":"Yalantis/uCrop","slug":"save-pnm-specified-filename-is-null","errorCode":null,"errorMessage":"save_pnm(): Specified filename is (null).","messagePattern":"save_pnm\\(\\): Specified filename is \\(null\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":62275,"sourceCode":"\n    //! Save image as a PNM file.\n    /**\n      \\param filename Filename, as a C-string.\n      \\param bytes_per_pixel Force the number of bytes per pixels for the saving.\n    **/\n    const CImg<T>& save_pnm(const char *const filename, const unsigned int bytes_per_pixel=0) const {\n      return _save_pnm(0,filename,bytes_per_pixel);\n    }\n\n    //! Save image as a PNM file \\overloading.\n    const CImg<T>& save_pnm(std::FILE *const file, const unsigned int bytes_per_pixel=0) const {\n      return _save_pnm(file,0,bytes_per_pixel);\n    }\n\n    const CImg<T>& _save_pnm(std::FILE *const file, const char *const filename,\n                             const unsigned int bytes_per_pixel=0) const {\n      if (!file && !filename)\n        throw CImgArgumentException(_cimg_instance\n                                    \"save_pnm(): Specified filename is (null).\",\n                                    cimg_instance);\n      if (is_empty()) { cimg::fempty(file,filename); return *this; }\n\n      double stmin, stmax = (double)max_min(stmin);\n      if (_depth>1)\n        cimg::warn(_cimg_instance\n                   \"save_pnm(): Instance is volumetric, only the first slice will be saved in file '%s'.\",\n                   cimg_instance,\n                   filename?filename:\"(FILE*)\");\n      if (_spectrum>3)\n        cimg::warn(_cimg_instance\n                   \"save_pnm(): Instance is multispectral, only the three first channels will be saved in file '%s'.\",\n                   cimg_instance,\n                   filename?filename:\"(FILE*)\");\n      if (stmin<0 || (bytes_per_pixel==1 && stmax>=256) || stmax>=65536)\n        cimg::warn(_cimg_instance\n                   \"save_pnm(): Instance has pixel values in [%g,%g], probable type overflow in file '%s'.\",","sourceCodeStart":62257,"sourceCodeEnd":62293,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L62257-L62293","documentation":"_save_pnm() requires at least one of an open FILE* or a filename string; when both are null it cannot know where to write, so it throws CImgArgumentException. This is a pure API-misuse guard at the top of the PNM/PPM/PGM save path.","triggerScenarios":"Calling save_pnm((std::FILE*)0) or a wrapper that passes a null filename and no file handle, e.g. build_save_pnm-style code forwarding an unset path variable.","commonSituations":"Path variable left uninitialized or emptied by config parsing; calling the FILE*-only overload with a stream that failed to open and was null; passing 0 instead of a filename in templated code.","solutions":["Pass a valid non-null filename string to save_pnm()","Open the file yourself and pass the FILE* to the FILE* overload","Validate the path argument before calling save_pnm()","Check that code deriving the filename (config, user input) never yields null"],"exampleFix":"// before\nimg.save_pnm((std::FILE*)NULL);\n// after\nimg.save_pnm(\"output.pnm\");\n// or\nstd::FILE *f = std::fopen(\"output.pnm\",\"wb\");\nif (f) img.save_pnm(f);","handlingStrategy":"validation","validationCode":"if (!file && (!filename || !*filename)) throw std::invalid_argument(\"save_pnm requires a filename or FILE*\");","typeGuard":"bool hasDestination(std::FILE *f, const char *name) { return f != NULL || (name != NULL && name[0] != '\\0'); }","tryCatchPattern":"try { img.save_pnm(path.c_str()); } catch (CImgArgumentException &e) { /* handle missing destination */ }","preventionTips":["Never call FILE*-only overloads with a possibly-null stream","Check fopen() results immediately","Validate path strings before passing to CImg"],"tags":["null-argument","image-io","api-misuse"],"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"}