Yalantis/uCrop · error · CImgArgumentException

save_minc2(): Specified filename is (null).

Error message

save_minc2(): Specified filename is (null).

What it means

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.

Source

Thrown at ucrop/src/main/jni/CImg.h:62858

      _cimg_save_tiff("float64",cimg_float32); // 'float64' as 'float32'
      const char *const filename = TIFFFileName(tif);
      throw CImgInstanceException(_cimg_instance
                                  "save_tiff(): Unsupported pixel type '%s' for file '%s'.",
                                  cimg_instance,
                                  pixel_type(),filename?filename:"(FILE*)");
      return *this;
    }
#endif

    //! Save image as a MINC2 file.
    /**
       \param filename Filename, as a C-string.
       \param imitate_file If non-zero, reference filename, as a C-string, to borrow header from.
    **/
    const CImg<T>& save_minc2(const char *const filename,
                              const char *const imitate_file=0) const {
      if (!filename)
        throw CImgArgumentException(_cimg_instance
                                   "save_minc2(): Specified filename is (null).",
                                   cimg_instance);
      if (is_empty()) { cimg::fempty(0,filename); return *this; }

#ifndef cimg_use_minc2
     cimg::unused(imitate_file);
     return save_other(filename);
#else
     minc::minc_1_writer wtr;
     if (imitate_file)
       wtr.open(filename, imitate_file);
     else {
       minc::minc_info di;
       if (width()) di.push_back(minc::dim_info(width(),width()*0.5,-1,minc::dim_info::DIM_X));
       if (height()) di.push_back(minc::dim_info(height(),height()*0.5,-1,minc::dim_info::DIM_Y));
       if (depth()) di.push_back(minc::dim_info(depth(),depth()*0.5,-1,minc::dim_info::DIM_Z));
       if (spectrum()) di.push_back(minc::dim_info(spectrum(),spectrum()*0.5,-1,minc::dim_info::DIM_TIME));
       wtr.open(filename,di,1,NC_FLOAT,0);

View on GitHub (pinned to f788b534b4)

Solutions

  1. Check the filename for null/empty before calling save_minc2 and surface a clear error to the user.
  2. Fix the upstream function that was supposed to produce the filename (env var, config key, CLI argument) so it returns a valid path.
  3. If you only have a FILE*, use the save_minc2(std::FILE*) overload with a real stream instead.
  4. Guard the call site with a small validation wrapper that throws a domain-specific error carrying the missing config/argument name.

Example fix

// before
const char *out = getenv("MINC_OUT");
img.save_minc2(out); // throws if MINC_OUT unset

// after
const char *out = getenv("MINC_OUT");
if (!out || !*out) throw std::runtime_error("MINC_OUT not set");
img.save_minc2(out);
Defensive patterns

Strategy: validation

Validate before calling

if (!filename || !*filename)
  throw std::invalid_argument("save_minc2: filename is null or empty");

Type guard

bool valid_path(const char* p) { return p != nullptr && *p != '\0'; }

Try / catch

try {
  img.save_minc2(path.c_str());
} catch (const CImgArgumentException& e) {
  std::fprintf(stderr, "MINC2 output path missing: %s\n", e.what());
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/e41067a93d1048f9. Report an issue: GitHub.