{"record":{"id":"6dcd714bea8120c9","repo":"Yalantis/uCrop","slug":"cimg-fempty-specified-filename-is-null","errorCode":null,"errorMessage":"cimg::fempty(): Specified filename is (null).","messagePattern":"cimg::fempty\\(\\): Specified filename is \\(null\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":7947,"sourceCode":"        l_to_write = (to_write*sizeof(T))<wlimitT?to_write:wlimit;\n        l_al_write = std::fwrite((void*)(ptr + al_write),sizeof(T),l_to_write,stream);\n        al_write+=l_al_write;\n        to_write-=l_al_write;\n      } while (l_to_write==l_al_write && to_write>0);\n      if (to_write>0)\n        warn(\"cimg::fwrite(): Only %lu/%lu elements could be written in file.\",\n             (unsigned long)al_write,(unsigned long)nmemb);\n      return al_write;\n    }\n\n    //! Create an empty file.\n    /**\n       \\param file Input file (can be \\c 0 if \\c filename is set).\n       \\param filename Filename, as a C-string (can be \\c 0 if \\c file is set).\n    **/\n    inline void fempty(std::FILE *const file, const char *const filename) {\n      if (!file && !filename)\n        throw CImgArgumentException(\"cimg::fempty(): Specified filename is (null).\");\n      std::FILE *const nfile = file?file:cimg::fopen(filename,\"wb\");\n      if (!file) cimg::fclose(nfile);\n    }\n\n    // Try to guess format from an image file.\n    inline const char *ftype(std::FILE *const file, const char *const filename);\n\n    // Get or set load from network mode (can be { 0=disabled | 1=enabled }).\n    inline bool& network_mode(const bool value, const bool is_set) {\n      static bool mode = true;\n      if (is_set) { cimg::mutex(0); mode = value; cimg::mutex(0,0); }\n      return mode;\n    }\n\n    inline bool& network_mode() {\n      return network_mode(false,false);\n    }\n","sourceCodeStart":7929,"sourceCodeEnd":7965,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L7929-L7965","documentation":"cimg::fempty() truncates/creates a file; it accepts either an open FILE* or a filename, but at least one must be non-NULL. It throws CImgArgumentException when both file and filename are NULL, because there is nothing to empty.","triggerScenarios":"Calling cimg::fempty(NULL, NULL) — e.g. both the stream variable and the filename string are unset, or a helper forwards two optional parameters that are both missing.","commonSituations":"Refactored helper signatures where the filename argument was dropped; passing a std::string filename's c_str() from an empty optional wrapper; forgetting to open the file before calling fempty.","solutions":["Pass either a valid open std::FILE* or a non-NULL filename C-string","If the filename may be absent, provide the already-open stream instead (or vice versa)","Validate the input before calling and raise a clearer application-level error"],"exampleFix":"// before\ncimg::fempty(file, filename); // both may be NULL\n// after\nif (file) cimg::fempty(file, 0);\nelse if (filename) cimg::fempty(0, filename);\nelse throw std::runtime_error(\"fempty needs a file or a filename\");","handlingStrategy":"validation","validationCode":"if (!file && (!filename || !*filename)) throw std::invalid_argument(\"fempty requires a file or filename\");","typeGuard":"bool emptyable(std::FILE *f, const char *name){ return f || (name && *name); }","tryCatchPattern":"try { cimg::fempty(file, filename); } catch (const cimg_library::CImgArgumentException &e) { log_error(\"fempty misuse: %s\", e.what()); }","preventionTips":["Pass exactly one of file/filename; pass 0 for the other, never both NULL","Keep filename lifetime valid — c_str() from a destroyed std::string is UB","Centralize file-truncation in one helper that validates inputs once"],"tags":["cimg","null-pointer","argument-validation","file-io"],"backgroundTag":"missing-required-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"}