{"record":{"id":"01c73b59207732d5","repo":"Yalantis/uCrop","slug":"cimg-fread-invalid-reading-request-of-u-s-s","errorCode":null,"errorMessage":"cimg::fread(): Invalid reading request of %u %s%s from file %p to buffer %p.","messagePattern":"cimg::fread\\(\\): Invalid reading request of %u (.+?)(.+?) from file %p to buffer %p\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":7895,"sourceCode":"      return p + 1;\n    }\n\n    // Generate a numbered version of a filename.\n    inline char* number_filename(const char *const filename, const int number,\n                                 const unsigned int digits, char *const str);\n\n    //! Read data from file.\n    /**\n       \\param[out] ptr Pointer to memory buffer that will contain the binary data read from file.\n       \\param nmemb Number of elements to read.\n       \\param stream File to read data from.\n       \\return Number of read elements.\n       \\note Same as <tt>std::fread()</tt> but may display warning message if all elements could not be read.\n    **/\n    template<typename T>\n    inline size_t fread(T *const ptr, const size_t nmemb, std::FILE *stream) {\n      if (!ptr || !stream)\n        throw CImgArgumentException(\"cimg::fread(): Invalid reading request of %u %s%s from file %p to buffer %p.\",\n                                    nmemb,cimg::type<T>::string(),nmemb>1?\"s\":\"\",stream,ptr);\n      if (!nmemb) return 0;\n      const size_t wlimitT = 63*1024*1024, wlimit = wlimitT/sizeof(T);\n      size_t to_read = nmemb, al_read = 0, l_to_read = 0, l_al_read = 0;\n      do {\n        l_to_read = (to_read*sizeof(T))<wlimitT?to_read:wlimit;\n        l_al_read = std::fread((void*)(ptr + al_read),sizeof(T),l_to_read,stream);\n        al_read+=l_al_read;\n        to_read-=l_al_read;\n      } while (l_to_read==l_al_read && to_read>0);\n      if (to_read>0)\n        warn(\"cimg::fread(): Only %lu/%lu elements could be read from file.\",\n             (unsigned long)al_read,(unsigned long)nmemb);\n      return al_read;\n    }\n\n    //! Write data to file.\n    /**","sourceCodeStart":7877,"sourceCodeEnd":7913,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L7877-L7913","documentation":"cimg::fread() validates that both the destination buffer and the FILE* stream are non-NULL before reading, and throws CImgArgumentException naming the element count, element type, stream pointer, and buffer pointer. It catches calls like fread(NULL, 100, fp) or fread(buf, 100, NULL). Note that nmemb==0 is explicitly allowed and returns 0.","triggerScenarios":"cimg::fread(ptr, nmemb, stream) with a NULL ptr (allocation failed or buffer freed early) or NULL stream (cimg::fopen failed earlier and its exception was ignored/caught without stopping the flow).","commonSituations":"Ignoring the throw from cimg::fopen and continuing to use the NULL handle; malloc/calloc returning NULL for huge sizes on Android NDK due to memory exhaustion; using a FILE* after fclose.","solutions":["Fix the earlier failure that left the FILE* or buffer NULL (check the return of cimg::fopen / allocation)","Check that the buffer allocation succeeded and is large enough for nmemb elements of sizeof(T)","Ensure the stream is opened before reading and not closed prematurely"],"exampleFix":"// before\nCImg<float> img; img.load(f); // f is NULL after failed open\n// after\nstd::FILE *f = cimg::fopen(path, \"rb\"); // throws if it fails\nif (f) { CImg<float> img; img.load(f); cimg::fclose(f); }","handlingStrategy":"type-guard","validationCode":"if (!f || !buf || nmemb == 0) { /* skip or raise app-level error */ }","typeGuard":"template<typename T> bool readable(T *p, std::FILE *s){ return p && s; }","tryCatchPattern":"try { n = cimg::fread(buf, nmemb, f); } catch (const cimg_library::CImgArgumentException &e) { log_error(\"bad fread request: %s\", e.what()); }","preventionTips":["Always check the FILE* returned by cimg::fopen before reading","Verify malloc/new results, especially for large buffers on mobile","Never use a FILE* after fclose; set it to NULL to catch reuse"],"tags":["cimg","null-pointer","file-io","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-14T05:17:10.506Z"}