{"record":{"id":"b27f4cd1bca85d12","repo":"Yalantis/uCrop","slug":"remove-invalid-remove-request-at-positions-u","errorCode":null,"errorMessage":"remove(): Invalid remove request at positions %u->%u.","messagePattern":"remove\\(\\): Invalid remove request at positions %u->%u\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":65952,"sourceCode":"    //! Insert n copies of the list \\c list at position \\c pos of the current list \\newinstance.\n    template<typename t>\n    CImgList<T> get_insert(const unsigned int n, const CImgList<t>& list, const unsigned int pos=~0U,\n                           const bool is_shared=false) const {\n      return (+*this).insert(n,list,pos,is_shared);\n    }\n\n    //! Remove all images between from indexes.\n    /**\n      \\param pos1 Starting index of the removal.\n      \\param pos2 Ending index of the removal.\n    **/\n    CImgList<T>& remove(const unsigned int pos1, const unsigned int pos2) {\n      const unsigned int\n        npos1 = pos1<pos2?pos1:pos2,\n        tpos2 = pos1<pos2?pos2:pos1,\n        npos2 = tpos2<_width?tpos2:_width - 1;\n      if (npos1>=_width)\n        throw CImgArgumentException(_cimglist_instance\n                                    \"remove(): Invalid remove request at positions %u->%u.\",\n                                    cimglist_instance,\n                                    npos1,tpos2);\n      else {\n        if (tpos2>=_width)\n          throw CImgArgumentException(_cimglist_instance\n                                      \"remove(): Invalid remove request at positions %u->%u.\",\n                                      cimglist_instance,\n                                      npos1,tpos2);\n\n        for (unsigned int k = npos1; k<=npos2; ++k) _data[k].assign();\n        const unsigned int nb = 1 + npos2 - npos1;\n        if (!(_width-=nb)) return assign();\n        if (_width>(_allocated_width>>4) || _allocated_width<=16) { // Removing items without reallocation\n          if (npos1!=_width)\n            std::memmove((void*)(_data + npos1),(void*)(_data + npos2 + 1),sizeof(CImg<T>)*(_width - npos1));\n          std::memset((void*)(_data + _width),0,sizeof(CImg<T>)*nb);\n        } else { // Removing items with reallocation","sourceCodeStart":65934,"sourceCodeEnd":65970,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L65934-L65970","documentation":"CImgList::remove(pos1,pos2) throws when the normalized start position npos1 is greater than or equal to the list size _width, meaning the entire requested removal range lies past the end of the list. Nothing can be removed, so the library raises CImgArgumentException with the normalized position range.","triggerScenarios":"list.remove(pos1,pos2) where min(pos1,pos2) >= list.size(), e.g. removing indices 4->6 from a 4-element list, or calling remove on an empty list.","commonSituations":"Reusing cached index ranges after the list shrank; removing a batch twice (second call is out of range); calling on an empty list built from a failed load.","solutions":["Check list.size() and skip the call when pos1 >= size() before removing","Clamp positions: pos1 = min(pos1, size-1), pos2 = min(pos2, size-1)","Verify the range wasn't already removed (guard against double removal)"],"exampleFix":"// before\nlist.remove(4, 6); // throws when list.size() <= 4\n// after\nif (list.size() > 4) list.remove(4, std::min(6u, list.size() - 1));","handlingStrategy":"validation","validationCode":"if (list.is_empty() || std::min(pos1,pos2) >= list.size()) return; // skip out-of-range removal","typeGuard":null,"tryCatchPattern":"try { list.remove(pos1, pos2); } catch (const CImgArgumentException& e) { /* log and skip */ }","preventionTips":["Check size() before batch removal","Avoid double removal of the same range","Use size()-1 as the maximum valid index"],"tags":["cimg","index-out-of-range","list-remove","argument-validation"],"backgroundTag":"index-out-of-range","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}