{"record":{"id":"9dd73b23c51babc3","repo":"Yalantis/uCrop","slug":"save-imagemagick-external-specified-filename-is","errorCode":null,"errorMessage":"save_imagemagick_external(): Specified filename is (null).","messagePattern":"save_imagemagick_external\\(\\): Specified filename is \\(null\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":63771,"sourceCode":"                              \"save_graphicsmagick_external(): Failed to save file '%s' with external command 'gm'.\",\n                              cimg_instance,\n                              filename);\n\n      std::remove(filename_tmp);\n      return *this;\n    }\n\n    //! Save image using ImageMagick's external binary.\n    /**\n       \\param filename Filename, as a C-string.\n       \\param quality Image quality (expressed in percent), when the file format supports it.\n       \\note This method uses \\c convert, an external executable binary provided by\n       <a href=\"http://www.imagemagick.org\">ImageMagick</a>.\n       It must be installed for the method to succeed.\n    **/\n    const CImg<T>& save_imagemagick_external(const char *const filename, const unsigned int quality=100) const {\n      if (!filename)\n        throw CImgArgumentException(_cimg_instance\n                                    \"save_imagemagick_external(): Specified filename is (null).\",\n                                    cimg_instance);\n      if (is_empty()) { cimg::fempty(0,filename); return *this; }\n      if (_depth>1)\n        cimg::warn(_cimg_instance\n                   \"save_other(): File '%s', saving a volumetric image with an external call to \"\n                   \"ImageMagick only writes the first image slice.\",\n                   cimg_instance,filename);\n#ifdef cimg_use_png\n#define _cimg_sie_extension1 \"png\"\n#define _cimg_sie_extension2 \"png\"\n#else\n#define _cimg_sie_extension1 \"pgm\"\n#define _cimg_sie_extension2 \"ppm\"\n#endif\n      CImg<charT> command(1024), filename_tmp(256);\n      do {\n        cimg_snprintf(filename_tmp,filename_tmp._width,\"%s%c%s.%s\",cimg::temporary_path(),","sourceCodeStart":63753,"sourceCodeEnd":63789,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L63753-L63789","documentation":"CImg::save_imagemagick_external() validates its arguments before doing any work. Passing a NULL filename pointer is a programming error, so CImg throws CImgArgumentException immediately. No file system or external command is involved.","triggerScenarios":"Calling save_imagemagick_external(nullptr) or passing a char* variable that was never initialized / was set to NULL by a failed lookup (e.g. result of a function returning NULL on error).","commonSituations":"Constructing the filename dynamically and failing to check for NULL before the save call; using a path returned from an environment/config lookup that returned null.","solutions":["Check the filename pointer for NULL before calling save_imagemagick_external().","Fix the upstream code that produced a NULL path (failed allocation, missing config, failed lookup).","Provide a default output path when the computed one is unavailable.","Use std::string and pass .c_str() only after confirming non-empty content."],"exampleFix":"// before\nconst char* path = getenv(\"OUT\");\nimg.save_imagemagick_external(path); // path may be NULL\n// after\nconst char* path = getenv(\"OUT\");\nif (path) img.save_imagemagick_external(path);\nelse img.save_imagemagick_external(\"default.jpg\");","handlingStrategy":"type-guard","validationCode":"if (!filename || !*filename) { /* use default or error out before calling */ }","typeGuard":"bool validPath(const char* p) { return p != nullptr && *p != '\\0'; }","tryCatchPattern":"try {\n  img.save_imagemagick_external(filename);\n} catch (CImgArgumentException& e) {\n  std::fprintf(stderr, \"bad filename: %s\", e.what());\n}","preventionTips":["Always null-check pointers derived from getenv(), config lookups, or parsing before saving.","Use std::string for path handling and only call c_str() on a confirmed value.","Adopt a save wrapper that validates arguments once instead of at each call site."],"tags":["null-pointer","argument-validation","imagemagick"],"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"}