{"record":{"id":"8c3d920e93b66896","repo":"Yalantis/uCrop","slug":"cimg-ftype-specified-filename-is-null","errorCode":null,"errorMessage":"cimg::ftype(): Specified filename is (null).","messagePattern":"cimg::ftype\\(\\): Specified filename is \\(null\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":69973,"sourceCode":"      }\n      closedir(dir);\n#endif\n\n      // Sort resulting list by lexicographic order.\n      if (res._width>=2) std::qsort(res._data,res._width,sizeof(CImg<char>),_sort_files);\n\n      return res;\n    }\n\n    //! Try to guess format from an image 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       \\return C-string containing the guessed file format, or \\c 0 if nothing has been guessed.\n    **/\n    inline const char *ftype(std::FILE *const file, const char *const filename) {\n      if (!file && !filename)\n        throw CImgArgumentException(\"cimg::ftype(): Specified filename is (null).\");\n      static const char\n        *const _bmp = \"bmp\",\n        *const _cr2 = \"cr2\",\n        *const _dcm = \"dcm\",\n        *const _gif = \"gif\",\n        *const _inr = \"inr\",\n        *const _jpg = \"jpg\",\n        *const _off = \"off\",\n        *const _pan = \"pan\",\n        *const _pfm = \"pfm\",\n        *const _png = \"png\",\n        *const _pnm = \"pnm\",\n        *const _tif = \"tif\",\n        *const _webp = \"webp\",\n        *const _jxl = \"jxl\";\n\n      const char *f_type = 0;\n      CImg<char> header;","sourceCodeStart":69955,"sourceCodeEnd":69991,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L69955-L69991","documentation":"cimg::ftype() guesses the image file format from a FILE* or a filename. The library throws this CImgArgumentException when both the file handle and the filename pointer are null, since with neither input there is nothing whose format can be guessed. It is an argument-validation guard at the very top of the function.","triggerScenarios":"Calling cimg::ftype(NULL, NULL), or indirectly calling a CImg load/constructor path (e.g. CImg<T>(filename) or load_other) that passes through ftype after the filename failed to resolve (e.g. a null string from an unset variable).","commonSituations":"Android NDK/JNI code (uCrop bundles CImg) passing a null path from Java because the app never set the image URI; a std::string converted with .c_str() from an empty/failed lookup; refactorings that drop the filename argument before the call.","solutions":["Ensure a non-null filename or FILE* is passed to ftype (or to the CImg load call that reaches it).","Validate the input path in your own code before invoking CImg load functions; reject empty strings early.","If the path comes from Java via JNI, check for null/empty jstring before converting to a C string.","Catch CImgArgumentException around the load call and surface a user-facing 'invalid file path' message instead of crashing."],"exampleFix":"// before\nCImg<unsigned char> img(uriPath.c_str()); // uriPath may be empty -> c_str() yields \"\", unresolved path leads to null\n// after\nif (uriPath.empty()) throw std::invalid_argument(\"image path is empty\");\nCImg<unsigned char> img(uriPath.c_str());","handlingStrategy":"validation","validationCode":"bool canGuessFormat(const char *file_or_name) { return file_or_name != nullptr && *file_or_name != '\\0'; }\nif (!canGuessFormat(path)) { /* fail fast: no file to inspect */ }","typeGuard":"bool hasValidPath(const char *p) { return p != nullptr && *p != '\\0'; }","tryCatchPattern":"try { img.load(path); } catch (const CImgArgumentException &e) { fprintf(stderr, \"invalid file argument: %s\", e.what()); }","preventionTips":["Never pass the result of an empty std::string's c_str() assuming it is usable — check emptiness first","Validate JNI jstrings for null before GetStringUTFChars conversion","Validate the image path at the boundary (UI/config layer) before calling CImg","Wrap CImg load calls in a helper that centralizes null/empty checks"],"tags":["null-argument","cimg","argument-validation"],"backgroundTag":"null-argument","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}