{"record":{"id":"657f36690a6ce78b","repo":"Yalantis/uCrop","slug":"cimg-temporary-path-failed-to-locate-path-for","errorCode":null,"errorMessage":"cimg::temporary_path(): Failed to locate path for writing temporary files.\n","messagePattern":"cimg::temporary_path\\(\\): Failed to locate path for writing temporary files\\.\n","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":69756,"sourceCode":"        _cimg_test_temporary_path(\"C:\\\\WINDOWS\\\\Temp\");\n        _cimg_test_temporary_path(\"C:\\\\Temp\");\n        _cimg_test_temporary_path(\"C:\");\n        _cimg_test_temporary_path(\"D:\\\\WINNT\\\\Temp\");\n        _cimg_test_temporary_path(\"D:\\\\WINDOWS\\\\Temp\");\n        _cimg_test_temporary_path(\"D:\\\\Temp\");\n        _cimg_test_temporary_path(\"D:\");\n#else\n        _cimg_test_temporary_path(\"/tmp\");\n        _cimg_test_temporary_path(\"/var/tmp\");\n#endif\n        if (!path_found) {\n          *s_path = 0;\n          std::strncpy(tmp,filename_tmp,tmp._width - 1);\n          if ((file=cimg::std_fopen(tmp,\"wb\"))!=0) { cimg::fclose(file); std::remove(tmp); path_found = true; }\n        }\n        if (!path_found) {\n          cimg::mutex(7,0);\n          throw CImgIOException(\"cimg::temporary_path(): Failed to locate path for writing temporary files.\\n\");\n        }\n      }\n      cimg::mutex(7,0);\n      return s_path;\n    }\n\n    //! Get/set path to the \\c wget binary.\n    /**\n       \\param user_path Specified path, or \\c 0 to get the path currently used.\n       \\param reinit_path Force path to be recalculated (may take some time).\n       \\return Path containing the \\c wget binary.\n    **/\n    inline const char *wget_path(const char *const user_path, const bool reinit_path) {\n      static CImg<char> s_path;\n      cimg::mutex(7);\n      if (reinit_path) s_path.assign();\n      if (user_path) {\n        if (!s_path) s_path.assign(1024);","sourceCodeStart":69738,"sourceCodeEnd":69774,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L69738-L69774","documentation":"cimg::temporary_path() probes candidate directories (environment variables like TMPDIR/TEMP/TMP, system temp paths, the current directory) by attempting to create and remove a scratch file. If no candidate is writable it releases its mutex and throws CImgIOException, because many CImg features (temp files for ffmpeg, ImageMagick, display) need a writable temp directory.","triggerScenarios":"Calling cimg::temporary_path() (or any feature needing temp files, e.g. save_ffmpeg_external, save_magick, X11 display helpers) when TMPDIR/TEMP/TMP are unset or point to nonexistent dirs, and the current directory is read-only.","commonSituations":"Sandboxed Android app with no writable CWD; hardened server with read-only working directory and no TMPDIR set; misconfigured TEMP pointing to a deleted folder; running under a no-write mount.","solutions":["Set the TMPDIR environment variable to an existing writable directory before running the process.","Explicitly configure CImg: call cimg::temporary_path(\"/data/local/tmp\") with a verified writable path.","Check directory permissions on the candidate temp locations and the CWD.","If sandboxed (Android/app container), create a dedicated cache dir and export it as TMPDIR at startup."],"exampleFix":"// before\nconst char* tmp = cimg::temporary_path(); // throws if none found\n// after\nif (!cimg::path_exists(\"/data/local/tmp\"))\n  throw std::runtime_error(\"no writable temp dir available\");\ncimg::temporary_path(\"/data/local/tmp\");\nconst char* tmp = cimg::temporary_path();","handlingStrategy":"fallback","validationCode":"// Probe a writable temp dir before calling CImg APIs that need one\nconst char* candidates[] = { getenv(\"TMPDIR\"), getenv(\"TEMP\"), getenv(\"TMP\"), \".\" };\nbool haveTemp = false;\nfor (const char* d : candidates)\n  if (d && cimg::path_exists(d)) { haveTemp = true; break; }\nif (!haveTemp) setenv(\"TMPDIR\", \"/data/local/tmp\", 1); // or fail early","typeGuard":null,"tryCatchPattern":"try {\n  const char* tmp = cimg::temporary_path();\n} catch (CImgIOException& e) {\n  cimg::warn(\"no writable temp dir; configure TMPDIR\");\n}","preventionTips":["Set TMPDIR/TEMP to a writable path at process startup.","On Android, use the app cache directory as TMPDIR.","Check CWD and temp mounts are writable in sandboxed/CI environments.","Call cimg::temporary_path(\"dir\") explicitly with a known-good directory."],"tags":["filesystem","temp-files","environment","permissions"],"backgroundTag":"permission-denied","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"}