{"record":{"id":"2ad8b947e4b219a9","repo":"Yalantis/uCrop","slug":"load-cimg-specified-filename-is-null","errorCode":null,"errorMessage":"load_cimg(): Specified filename is (null).","messagePattern":"load_cimg\\(\\): Specified filename is \\(null\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":66782,"sourceCode":"              } else { \\\n                CImg<Tss> raw; \\\n                for (ulongT to_read = img.size(); to_read; ) { \\\n                  raw.assign((unsigned int)std::min(to_read,cimg_iobuffer)); \\\n                  cimg::fread(raw._data,raw._width,nfile); \\\n                  if (endian!=cimg::endianness()) cimg::invert_endianness(raw._data,raw.size()); \\\n                  const Tss *ptrs = raw._data; \\\n                  for (ulongT off = (ulongT)raw._width; off; --off) *(ptrd++) = (T)*(ptrs++); \\\n                  to_read-=raw._width; \\\n                } \\\n              } \\\n            } \\\n          } \\\n        } \\\n        loaded = true; \\\n      }\n\n      if (!filename && !file)\n        throw CImgArgumentException(_cimglist_instance\n                                    \"load_cimg(): Specified filename is (null).\",\n                                    cimglist_instance);\n\n      const ulongT cimg_iobuffer = (ulongT)24*1024*1024;\n      std::FILE *const nfile = file?file:cimg::fopen(filename,\"rb\");\n      bool loaded = false, endian = cimg::endianness();\n      CImg<charT> tmp(256), str_pixeltype(256), str_endian(256);\n      *tmp = *str_pixeltype = *str_endian = 0;\n      unsigned int j, N = 0, W, H, D, C;\n      cimg_uint64 csiz;\n      int i, err;\n      do {\n        j = 0; while ((i=std::fgetc(nfile))!='\\n' && i>=0 && j<255) tmp[j++] = (char)i; tmp[j] = 0;\n      } while (*tmp=='#' && i>=0);\n      err = cimg_sscanf(tmp,\"%u%*c%255[A-Za-z123468_]%*c%255[sA-Za-z_ ]\",\n                        &N,str_pixeltype._data,str_endian._data);\n      if (err<2) {\n        if (!file) cimg::fclose(nfile);","sourceCodeStart":66764,"sourceCodeEnd":66800,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L66764-L66800","documentation":"CImgList::load_cimg() requires either a filename or an open FILE*; if both are null it throws CImgArgumentException before doing any I/O (CImg.h:66782). It is a programming/API-misuse guard, not a file problem.","triggerScenarios":"Calling list.load_cimg(NULL) or load_cimg(std::string().c_str()) where the string is empty, or passing filename=NULL with file=NULL because an earlier path-lookup returned null.","commonSituations":"A config/env variable holding the dataset path is empty; a JNI layer passing a null jstring converted to nullptr; a failed path join producing an empty string.","solutions":["Check the path string is non-empty and pass it only when non-null.","If reading from memory, open the stream yourself: load_cimg(\"file.cimg\") or pass an std::FILE* from fopen.","Fix the upstream code that produced the null/empty path (missing env var, failed config lookup)."],"exampleFix":"// before\nconst char* path = getenv(\"CIMG_DATA\"); // may be NULL\nimgs.load_cimg(path);\n\n// after\nconst char* path = getenv(\"CIMG_DATA\");\nif (path && *path) imgs.load_cimg(path);\nelse throw std::invalid_argument(\"CIMG_DATA path not set\");","handlingStrategy":"validation","validationCode":"bool canLoadCimg(const char* path) { return path != nullptr && *path != '\\0'; }","typeGuard":null,"tryCatchPattern":"if (!canLoadCimg(path))\n  throw std::invalid_argument(\"load_cimg: filename must be non-empty\");\ntry {\n  imgs.load_cimg(path);\n} catch (cimg_library::CImgArgumentException& e) {\n  logError(\"null path: %s\", e.what());\n}","preventionTips":["Guard env/config lookups for null/empty before passing paths into CImg","In JNI, check jstring for null before GetStringUTFChars","Never pass c_str() of an empty std::string","Resolve and log the full path before calling load_cimg"],"tags":["cimg","cpp","null-argument","api-misuse"],"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"}