{"record":{"id":"b6304f529ef2d843","repo":"Yalantis/uCrop","slug":"cimg-fwrite-invalid-writing-request-of-u-s-s","errorCode":null,"errorMessage":"cimg::fwrite(): Invalid writing request of %u %s%s from buffer %p to file %p.","messagePattern":"cimg::fwrite\\(\\): Invalid writing request of %u (.+?)(.+?) from buffer %p to file %p\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":7923,"sourceCode":"      } 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    /**\n       \\param ptr Pointer to memory buffer containing the binary data to write on file.\n       \\param nmemb Number of elements to write.\n       \\param[out] stream File to write data on.\n       \\return Number of written elements.\n       \\note Similar to <tt>std::fwrite</tt> but may display warning messages if all elements could not be written.\n    **/\n    template<typename T>\n    inline size_t fwrite(const T *ptr, const size_t nmemb, std::FILE *stream) {\n      if (!ptr || !stream)\n        throw CImgArgumentException(\"cimg::fwrite(): Invalid writing request of %u %s%s from buffer %p to file %p.\",\n                                    nmemb,cimg::type<T>::string(),nmemb>1?\"s\":\"\",ptr,stream);\n      if (!nmemb) return 0;\n      const size_t wlimitT = 63*1024*1024, wlimit = wlimitT/sizeof(T);\n      size_t to_write = nmemb, al_write = 0, l_to_write = 0, l_al_write = 0;\n      do {\n        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    /**","sourceCodeStart":7905,"sourceCodeEnd":7941,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L7905-L7941","documentation":"cimg::fwrite() mirrors cimg::fread(): it throws CImgArgumentException if the source buffer or the FILE* stream is NULL. The element count, type, buffer and stream pointers are embedded in the message. Writing 0 elements is permitted and returns 0.","triggerScenarios":"cimg::fwrite(ptr, nmemb, stream) with a NULL ptr (data array never allocated or already freed) or NULL stream (save target never opened successfully).","commonSituations":"Saving an image whose _data is NULL (never assigned / default-constructed with no size) to a FILE*; passing a stream from a failed cimg::fopen; double-free leaving a dangling/zeroed pointer in JNI code.","solutions":["Ensure the image/buffer is initialized and non-empty before writing","Confirm cimg::fopen succeeded and the stream is valid before fwrite","Check that the buffer was not deleted/freed earlier in the code path"],"exampleFix":"// before\nif (img._data) cimg::fwrite(img._data, img.size(), f); // f may be NULL\n// after\nif (img._data && f) cimg::fwrite(img._data, img.size(), f);","handlingStrategy":"type-guard","validationCode":"if (!f || !data || count == 0) { /* skip or raise app-level error */ }","typeGuard":"template<typename T> bool writable(const T *p, std::FILE *s){ return p && s; }","tryCatchPattern":"try { n = cimg::fwrite(data, count, f); } catch (const cimg_library::CImgArgumentException &e) { log_error(\"bad fwrite request: %s\", e.what()); }","preventionTips":["Check img.is_empty() / _data != NULL before saving","Open the target stream first and check it before any write","Avoid storing raw pointers to data owned by temporary CImg objects"],"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"}