{"record":{"id":"e41067a93d1048f9","repo":"Yalantis/uCrop","slug":"save-minc2-specified-filename-is-null","errorCode":null,"errorMessage":"save_minc2(): Specified filename is (null).","messagePattern":"save_minc2\\(\\): Specified filename is \\(null\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":62858,"sourceCode":"      _cimg_save_tiff(\"float64\",cimg_float32); // 'float64' as 'float32'\n      const char *const filename = TIFFFileName(tif);\n      throw CImgInstanceException(_cimg_instance\n                                  \"save_tiff(): Unsupported pixel type '%s' for file '%s'.\",\n                                  cimg_instance,\n                                  pixel_type(),filename?filename:\"(FILE*)\");\n      return *this;\n    }\n#endif\n\n    //! Save image as a MINC2 file.\n    /**\n       \\param filename Filename, as a C-string.\n       \\param imitate_file If non-zero, reference filename, as a C-string, to borrow header from.\n    **/\n    const CImg<T>& save_minc2(const char *const filename,\n                              const char *const imitate_file=0) const {\n      if (!filename)\n        throw CImgArgumentException(_cimg_instance\n                                   \"save_minc2(): Specified filename is (null).\",\n                                   cimg_instance);\n      if (is_empty()) { cimg::fempty(0,filename); return *this; }\n\n#ifndef cimg_use_minc2\n     cimg::unused(imitate_file);\n     return save_other(filename);\n#else\n     minc::minc_1_writer wtr;\n     if (imitate_file)\n       wtr.open(filename, imitate_file);\n     else {\n       minc::minc_info di;\n       if (width()) di.push_back(minc::dim_info(width(),width()*0.5,-1,minc::dim_info::DIM_X));\n       if (height()) di.push_back(minc::dim_info(height(),height()*0.5,-1,minc::dim_info::DIM_Y));\n       if (depth()) di.push_back(minc::dim_info(depth(),depth()*0.5,-1,minc::dim_info::DIM_Z));\n       if (spectrum()) di.push_back(minc::dim_info(spectrum(),spectrum()*0.5,-1,minc::dim_info::DIM_TIME));\n       wtr.open(filename,di,1,NC_FLOAT,0);","sourceCodeStart":62840,"sourceCodeEnd":62876,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L62840-L62876","documentation":"save_minc2() validates its filename argument before doing any work and throws CImgArgumentException when a null C-string is passed. The library formats the message with '(null)' because printf of a null pointer prints that literal. This is a defensive precondition check, not a file-system failure.","triggerScenarios":"Passing NULL/0 as the filename to CImg::save_minc2(filename), typically from an uninitialized char* variable or a failed path-computation function whose null return was not checked. Also hit when calling the internal save(filename) dispatcher with a null string.","commonSituations":"Filename built by getenv() or a config lookup that returned NULL; results of basename()/string parsing assigned to a pointer that stayed null; minc2 toolchain missing so the caller passes a placeholder null.","solutions":["Check the filename for null/empty before calling save_minc2 and surface a clear error to the user.","Fix the upstream function that was supposed to produce the filename (env var, config key, CLI argument) so it returns a valid path.","If you only have a FILE*, use the save_minc2(std::FILE*) overload with a real stream instead.","Guard the call site with a small validation wrapper that throws a domain-specific error carrying the missing config/argument name."],"exampleFix":"// before\nconst char *out = getenv(\"MINC_OUT\");\nimg.save_minc2(out); // throws if MINC_OUT unset\n\n// after\nconst char *out = getenv(\"MINC_OUT\");\nif (!out || !*out) throw std::runtime_error(\"MINC_OUT not set\");\nimg.save_minc2(out);","handlingStrategy":"validation","validationCode":"if (!filename || !*filename)\n  throw std::invalid_argument(\"save_minc2: filename is null or empty\");","typeGuard":"bool valid_path(const char* p) { return p != nullptr && *p != '\\0'; }","tryCatchPattern":"try {\n  img.save_minc2(path.c_str());\n} catch (const CImgArgumentException& e) {\n  std::fprintf(stderr, \"MINC2 output path missing: %s\\n\", e.what());\n}","preventionTips":["Use std::string instead of raw char* for paths","Never forward getenv() results without a null/empty check","Validate all output paths centrally before calling any save_*"],"tags":["cimg","minc2","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"}