{"record":{"id":"9b1c439e6d33f361","repo":"Yalantis/uCrop","slug":"save-bmp-specified-filename-is-null","errorCode":null,"errorMessage":"save_bmp(): Specified filename is (null).","messagePattern":"save_bmp\\(\\): Specified filename is \\(null\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":61491,"sourceCode":"      return *this;\n    }\n\n    //! Save image as a BMP file.\n    /**\n      \\param filename Filename, as a C-string.\n    **/\n    const CImg<T>& save_bmp(const char *const filename) const {\n      return _save_bmp(0,filename);\n    }\n\n    //! Save image as a BMP file \\overloading.\n    const CImg<T>& save_bmp(std::FILE *const file) const {\n      return _save_bmp(file,0);\n    }\n\n    const CImg<T>& _save_bmp(std::FILE *const file, const char *const filename) const {\n      if (!file && !filename)\n        throw CImgArgumentException(_cimg_instance\n                                    \"save_bmp(): Specified filename is (null).\",\n                                    cimg_instance);\n      if (is_empty()) { cimg::fempty(file,filename); return *this; }\n      if (_depth>1)\n        cimg::warn(_cimg_instance\n                   \"save_bmp(): Instance is volumetric, only the first slice will be saved in file '%s'.\",\n                   cimg_instance,\n                   filename?filename:\"(FILE*)\");\n      if (_spectrum>3)\n        cimg::warn(_cimg_instance\n                   \"save_bmp(): Instance is multispectral, only the three first channels will be saved in file '%s'.\",\n                   cimg_instance,\n                   filename?filename:\"(FILE*)\");\n\n      std::FILE *const nfile = file?file:cimg::fopen(filename,\"wb\");\n      CImg<ucharT> header(54,1,1,1,0);\n      unsigned char align_buf[4] = {};\n      const unsigned int","sourceCodeStart":61473,"sourceCodeEnd":61509,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L61473-L61509","documentation":"save_bmp() writes a Windows BMP file; _save_bmp() throws CImgArgumentException when both the FILE* and filename are null — no output destination was specified. BMP needs either an open stream or a path to open itself.","triggerScenarios":"img.save_bmp((std::FILE*)NULL) or img.save_bmp((const char*)NULL); forwarding a NULL FILE* from failed fopen into the stream overload.","commonSituations":"Screenshot/image export with a destination path from user input that was empty and converted to NULL; fopen permission errors.","solutions":["Pass a valid filename, e.g., img.save_bmp(\"shot.bmp\").","Check fopen returned non-NULL before using the FILE* overload.","Null-check both arguments at the call site.","Write to stdout with the \"-\" filename if streaming output is intended."],"exampleFix":"// before\nimg.save_bmp(file); // file==NULL when fopen failed\n// after\nif (file) img.save_bmp(file); else img.save_bmp(\"output.bmp\");","handlingStrategy":"type-guard","validationCode":"if (!file && (!filename || !*filename)) { /* abort or default to output.bmp */ }","typeGuard":"bool hasDestination(std::FILE* f, const char* n){ return f!=nullptr || (n!=nullptr && *n!='\\0'); }","tryCatchPattern":"try { img.save_bmp(f); } catch (CImgArgumentException& e) { img.save_bmp(\"output.bmp\"); }","preventionTips":["Check fopen result before the stream overload","Never convert empty user paths to NULL; validate earlier","Use \"-\" filename for stdout streaming instead of NULL"],"tags":["cimg","save","null-pointer","bmp"],"backgroundTag":"null-argument","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}