{"record":{"id":"6d2c15e7212f210b","repo":"Yalantis/uCrop","slug":"save-inr-specified-filename-is-null","errorCode":null,"errorMessage":"save_inr(): Specified filename is (null).","messagePattern":"save_inr\\(\\): Specified filename is \\(null\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":63058,"sourceCode":"    }\n\n    //! Save image as an INRIMAGE-4 file.\n    /**\n      \\param filename Filename, as a C-string.\n      \\param voxel_size Pointer to 3 values specifying the voxel sizes along the X,Y and Z dimensions.\n    **/\n    const CImg<T>& save_inr(const char *const filename, const float *const voxel_size=0) const {\n      return _save_inr(0,filename,voxel_size);\n    }\n\n    //! Save image as an INRIMAGE-4 file \\overloading.\n    const CImg<T>& save_inr(std::FILE *const file, const float *const voxel_size=0) const {\n      return _save_inr(file,0,voxel_size);\n    }\n\n    const CImg<T>& _save_inr(std::FILE *const file, const char *const filename, const float *const voxel_size) const {\n      if (!file && !filename)\n        throw CImgArgumentException(_cimg_instance\n                                    \"save_inr(): Specified filename is (null).\",\n                                    cimg_instance);\n      if (is_empty()) { cimg::fempty(file,filename); return *this; }\n\n      int inrpixsize = -1;\n      const char *inrtype = \"unsigned fixed\\nPIXSIZE=8 bits\\nSCALE=2**0\";\n      if (!cimg::strcasecmp(pixel_type(),\"uint8\")) {\n        inrtype = \"unsigned fixed\\nPIXSIZE=8 bits\\nSCALE=2**0\"; inrpixsize = 1;\n      }\n      if (!cimg::strcasecmp(pixel_type(),\"int8\")) {\n        inrtype = \"fixed\\nPIXSIZE=8 bits\\nSCALE=2**0\"; inrpixsize = 1;\n      }\n      if (!cimg::strcasecmp(pixel_type(),\"uint16\")) {\n        inrtype = \"unsigned fixed\\nPIXSIZE=16 bits\\nSCALE=2**0\";inrpixsize = 2;\n      }\n      if (!cimg::strcasecmp(pixel_type(),\"int16\")) {\n        inrtype = \"fixed\\nPIXSIZE=16 bits\\nSCALE=2**0\"; inrpixsize = 2;\n      }","sourceCodeStart":63040,"sourceCodeEnd":63076,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L63040-L63076","documentation":"The INR (Inrimage) writer has two entry points: one taking an open FILE* and one taking a filename, both funnelling into _save_inr(). It throws CImgArgumentException when both the FILE* and the filename are null, because there is nowhere to write. This is a pure precondition check before the image is even inspected.","triggerScenarios":"Calling CImg::save_inr(filename) with a null filename; calling the FILE* overload with a null FILE* (e.g. fopen failed) and no filename fallback; generic save(0) dispatch hitting the inr branch.","commonSituations":"fopen() returned NULL and its result was forwarded to save_inr; uninitialized filename pointer from config/env; wrapper APIs that pass through optional path/stream arguments where both ended up unset.","solutions":["Ensure exactly one of the output targets is valid: check filename non-null before calling, or check the FILE* from fopen before passing it.","Fix the source of the null (fopen failure, missing config value, unset env var) and handle that failure at its origin.","If you have neither a path nor a stream by design, choose a save overload that takes a filename and construct one.","Add an app-level validation wrapper that rejects both-null inputs with a message naming where the path should come from."],"exampleFix":"// before\nstd::FILE *f = fopen(path, \"wb\"); // may be NULL\nimg.save_inr(f);\n\n// after\nstd::FILE *f = fopen(path, \"wb\");\nif (!f) throw std::runtime_error(std::string(\"cannot open \") + path);\nimg.save_inr(f);","handlingStrategy":"validation","validationCode":"if (!file && !(filename && *filename))\n  throw std::invalid_argument(\"save_inr: need a FILE* or a filename\");","typeGuard":"bool has_output_target(std::FILE* f, const char* p) { return f != nullptr || (p && *p); }","tryCatchPattern":"try {\n  img.save_inr(path.c_str());\n} catch (const CImgArgumentException& e) {\n  std::fprintf(stderr, \"INR save failed, no output target: %s\\n\", e.what());\n}","preventionTips":["Check fopen() result before passing a FILE* to save_inr","Ensure at least one of stream/path is set in wrapper APIs","Never pass both null stream and null filename"],"tags":["cimg","inr","null-argument","filename"],"backgroundTag":"null-argument","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}