{"record":{"id":"79fc46dd4cde02ed","repo":"Yalantis/uCrop","slug":"get-shared-channels-invalid-request-of-a-shared","errorCode":null,"errorMessage":"get_shared_channels(): Invalid request of a shared-memory subset (0->%u,0->%u,0->%u,%u->%u).","messagePattern":"get_shared_channels\\(\\): Invalid request of a shared-memory subset \\(0->%u,0->%u,0->%u,%u->%u\\)\\.","errorType":"validation","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":41696,"sourceCode":"      return get_shared_slices(z0,z0,c0);\n    }\n\n    //! Return a shared-memory image referencing one slice of the image instance \\const.\n    const CImg<T> get_shared_slice(const unsigned int z0, const unsigned int c0=0) const {\n      return get_shared_slices(z0,z0,c0);\n    }\n\n    //! Return a shared-memory image referencing a range of channels of the image instance.\n    /**\n       \\param c0 C-coordinate of the starting channel.\n       \\param c1 C-coordinate of the ending channel.\n    **/\n    CImg<T> get_shared_channels(const unsigned int c0, const unsigned int c1) {\n      const ulongT\n        beg = (ulongT)offset(0,0,0,c0),\n        end = (ulongT)offset(0,0,0,c1);\n      if (beg>end || beg>=size() || end>=size())\n        throw CImgArgumentException(_cimg_instance\n                                    \"get_shared_channels(): Invalid request of a shared-memory subset \"\n                                    \"(0->%u,0->%u,0->%u,%u->%u).\",\n                                    cimg_instance,\n                                    _width - 1,_height - 1,_depth - 1,c0,c1);\n      return CImg<T>(_data + beg,_width,_height,_depth,c1 - c0 + 1,true);\n    }\n\n    //! Return a shared-memory image referencing a range of channels of the image instance \\const.\n    const CImg<T> get_shared_channels(const unsigned int c0, const unsigned int c1) const {\n      const ulongT\n        beg = (ulongT)offset(0,0,0,c0),\n        end = (ulongT)offset(0,0,0,c1);\n      if (beg>end || beg>=size() || end>=size())\n        throw CImgArgumentException(_cimg_instance\n                                    \"get_shared_channels(): Invalid request of a shared-memory subset \"\n                                    \"(0->%u,0->%u,0->%u,%u->%u).\",\n                                    cimg_instance,\n                                    _width - 1,_height - 1,_depth - 1,c0,c1);","sourceCodeStart":41678,"sourceCodeEnd":41714,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L41678-L41714","documentation":"CImg<T>::get_shared_channels() returns a shared-memory image referencing channels c0..c1 of the instance. It throws CImgArgumentException when the channel offsets are inverted (beg > end) or outside the instance memory (beg >= size() or end >= size()), i.e. the requested channel range does not exist in the image.","triggerScenarios":"Calling img.get_shared_channels(c0,c1) with c0 > c1, c1 >= img.spectrum() (e.g. requesting channels 0-3 of an RGB image with spectrum 3), or on an image with spectrum < 2.","commonSituations":"Requesting RGBA (4) channels from an RGB (3) image; assuming an alpha channel exists; extracting channels from a grayscale image (spectrum 1); palette-indexed images where spectrum is 1.","solutions":["Ensure 0 <= c0 <= c1 < img.spectrum(); clamp c1 to spectrum-1 before the call.","Verify the image actually has the expected channel count (e.g. img.spectrum() >= 4 before extracting RGBA).","If the source lacks channels, append dummy channels first (img.append(CImg<T>(img.width(),img.height(),img.depth(),k),'c')) or handle the lower channel count.","For grayscale images, decide explicitly whether to duplicate the channel instead of extracting a range."],"exampleFix":"// before\nCImg<float> rgba = img.get_shared_channels(0, 3);   // img is RGB (spectrum 3)\n// after\nif (img.spectrum() < 4) img.append(CImg<float>(img.width(),img.height(),img.depth(),1,1),'c');\nCImg<float> rgba = img.get_shared_channels(0, 3);","handlingStrategy":"validation","validationCode":"bool ok = c0 <= c1 && c1 < img.spectrum();","typeGuard":null,"tryCatchPattern":"try { auto ch = img.get_shared_channels(c0, c1); }\ncatch (CImgArgumentException& e) { /* clamp c1 to spectrum-1 and retry */ }","preventionTips":["Never assume 4 channels; read img.spectrum() first","Clamp c1 to spectrum-1","Append channels explicitly when RGBA semantics are required"],"tags":["cimg","shared-memory","channels","bounds-check"],"backgroundTag":"index-out-of-bounds","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"}