Yalantis/uCrop · error · CImgArgumentException

load_graphicsmagick_external(): Specified filename is (null)

Error message

load_graphicsmagick_external(): Specified filename is (null) or does not exist.

What it means

load_graphicsmagick_external() first validates that the filename argument is non-null and points to an existing regular file via cimg::is_file(); if not, it throws CImgArgumentException before any external 'gm' command is run. This is an argument precondition, not a decode failure.

Source

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

        cimg_forX(*this,x) {
          *(ptr_r++) = (T)*(ptrs++);
          *(ptr_g++) = (T)*(ptrs++);
          *(ptr_b++) = (T)*(ptrs++);
          if (ptr_a) *(ptr_a++) = (T)*(ptrs++);
        }
      }
      WebPFreeDecBuffer(&config.output);
      return *this;
#endif
    }

    //! Load image using GraphicsMagick's external tool 'gm'.
    /**
       \param filename Filename, as a C-string.
    **/
    CImg<T>& load_graphicsmagick_external(const char *const filename) {
      if (!filename || !cimg::is_file(filename))
        throw CImgArgumentException(_cimg_instance
                                    "load_graphicsmagick_external(): Specified filename is (null) or does not exist.",
                                    cimg_instance);
      CImg<charT> command(1024), filename_tmp(256);
      const CImg<charT> s_filename = CImg<charT>::string(filename)._system_strescape();
#if cimg_OS==1
      if (cimg::posix_searchpath("gm")) {
        cimg_snprintf(command,command._width,"%s convert \"%s\" %s:-",
                      cimg::graphicsmagick_path(),
                      s_filename.data(),
#ifdef cimg_use_png
                      "png"
#else
                      "pnm"
#endif
                      );
        std::FILE *file = popen(command,"r");
        if (file) {
          const unsigned int omode = cimg::exception_mode();

View on GitHub (pinned to f788b534b4)

Solutions

  1. Check the path with a filesystem stat (or cimg::is_file) before calling and handle the missing-file case
  2. Pass an absolute path resolved against a known base directory
  3. Fix the code that produces the filename (config, env, user input) so it yields an existing file
  4. If the image is not on disk, write it to a temp file first, then call load_graphicsmagick_external() on it

Example fix

// before
img.load_graphicsmagick_external(fname); // fname may be NULL / missing
// after
if (fname && cimg::is_file(fname)) {
  img.load_graphicsmagick_external(fname);
} else {
  throw std::invalid_argument("input file does not exist");
}
Defensive patterns

Strategy: validation

Validate before calling

if (!filename || !cimg::is_file(filename)) {
  throw std::invalid_argument(std::string("input file missing: ") + (filename ? filename : "(null)"));
}
img.load_graphicsmagick_external(filename);

Type guard

bool loadable_file(const char* f) { return f != nullptr && cimg::is_file(f); }

Try / catch

try {
  img.load_graphicsmagick_external(filename);
} catch (CImgArgumentException& e) {
  log_error("bad path for gm loader: ", e.what());
}

Prevention

When it happens

Trigger: Calling load_graphicsmagick_external(NULL) or with a path that does not exist, is a directory, or is a dangling symlink.

Common situations: Path built from a config value that was never resolved; working-directory assumptions breaking after deployment; passing a URL or in-memory buffer name instead of a real filesystem path; typo'd relative paths.

Related errors


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