{"record":{"id":"b22df7d8405d4333","repo":"Yalantis/uCrop","slug":"insert-invalid-insertion-request-of-specified-i","errorCode":null,"errorMessage":"insert(): Invalid insertion request of specified image (%u,%u,%u,%u,%p) at position %u.","messagePattern":"insert\\(\\): Invalid insertion request of specified image \\(%u,%u,%u,%u,%p\\) at position %u\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":65754,"sourceCode":"\n    //@}\n    //---------------------------\n    //\n    //! \\name List Manipulation\n    //@{\n    //---------------------------\n\n    //! Insert a copy of the image \\c img into the current image list, at position \\c pos.\n    /**\n        \\param img Image to insert a copy to the list.\n        \\param pos Index of the insertion.\n        \\param is_shared Tells if the inserted image is a shared copy of \\c img or not.\n    **/\n    template<typename t>\n    CImgList<T>& insert(const CImg<t>& img, const unsigned int pos=~0U, const bool is_shared=false) {\n      const unsigned int npos = pos==~0U?_width:pos;\n      if (npos>_width)\n        throw CImgArgumentException(_cimglist_instance\n                                    \"insert(): Invalid insertion request of specified image (%u,%u,%u,%u,%p) \"\n                                    \"at position %u.\",\n                                    cimglist_instance,\n                                    img._width,img._height,img._depth,img._spectrum,img._data,npos);\n      if (is_shared)\n        throw CImgArgumentException(_cimglist_instance\n                                    \"insert(): Invalid insertion request of specified shared image \"\n                                    \"CImg<%s>(%u,%u,%u,%u,%p) at position %u (pixel types are different).\",\n                                    cimglist_instance,\n                                    img.pixel_type(),img._width,img._height,img._depth,img._spectrum,img._data,npos);\n\n      CImg<T> *const new_data = (++_width>_allocated_width)?new CImg<T>[_allocated_width?(_allocated_width<<=1):\n                                                                        (_allocated_width=16)]:0;\n      if (!_data) { // Insert new element into empty list\n        _data = new_data;\n        *_data = img;\n      } else {\n        if (new_data) { // Insert with re-allocation","sourceCodeStart":65736,"sourceCodeEnd":65772,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L65736-L65772","documentation":"CImgList<T>::insert(img, pos, is_shared) validates the insertion position: the normalized position npos (= list width when pos==~0U, i.e. \"append\") must satisfy npos <= _width. Passing a pos greater than the current list size throws CImgArgumentException \"insert(): Invalid insertion request ... at position %u\" (ucrop/src/main/jni/CImg.h:65754), printing the image's dimensions and pointer for diagnosis.","triggerScenarios":"Calling insert(img, pos) with pos > list.size() (e.g. inserting at index 5 into a 3-element list); computing a position from an off-by-one loop or from a stale size captured before removals; passing a negative int that wraps to a huge unsigned value.","commonSituations":"Building lists by inserting at computed indices after prior inserts/removes changed the size; porting code that used signed indices; loop counters exceeding the list length; GUI code inserting at a selection index that no longer exists after deletion.","solutions":["Clamp the position: use pos = std::min(pos, (unsigned)list.size()) or pass the default ~0U to append.","Recompute list.size() immediately before insert() rather than caching it across mutations.","Reject negative/oversized user input before converting to the unsigned pos parameter.","Use push_back-equivalent behavior (insert with default pos) when order does not matter."],"exampleFix":"// before\nconst unsigned int pos = computedIndex; // may exceed size\nlist.insert(img, pos);\n\n// after\nunsigned int pos = computedIndex;\nif (pos > list.size()) pos = list.size(); // clamp; ~0U appends\nlist.insert(img, pos);","handlingStrategy":"validation","validationCode":"unsigned int npos = (pos == ~0U) ? list.size() : pos; if (pos != ~0U && pos > list.size()) { pos = list.size(); /* clamp or report error */ }","typeGuard":"static inline bool validInsertPos(const CImgList<T>& l, unsigned int pos) { return pos == ~0U || pos <= l.size(); }","tryCatchPattern":"try { list.insert(img, pos); } catch (CImgArgumentException& e) { /* clamp pos to list.size() and retry, or report */ }","preventionTips":["Clamp any computed insertion index to list.size() before calling insert().","Re-read list.size() right before insertion instead of caching stale sizes.","Pass the default pos (~0U) when you just want to append.","Beware signed-to-unsigned conversion: validate negative indices before casting to unsigned int."],"tags":["cimg","jni","insert","position-out-of-range"],"backgroundTag":"value-out-of-range","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"}