{"record":{"id":"f0e43e7f24d5c7a8","repo":"Yalantis/uCrop","slug":"operator-invalid-pixel-request-at-coordinates","errorCode":null,"errorMessage":"operator(): Invalid pixel request, at coordinates (%d,%d,%d,%d) [offset=%u].","messagePattern":"operator\\(\\): Invalid pixel request, at coordinates \\((.+?),(.+?),(.+?),(.+?)\\) \\[offset=%u\\]\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":13929,"sourceCode":"         checking operations in this operator. In that case, warning messages will be printed on the error output\n         when accessing out-of-bounds pixels.\n       \\par Example\n       \\code\n       CImg<float> img(100,100,1,3,0); // Construct a 100x100x1x3 (color) image with pixels set to '0'\n       const float\n          valR = img(10,10,0,0), // Read red value at coordinates (10,10)\n          valG = img(10,10,0,1), // Read green value at coordinates (10,10)\n          valB = img(10,10,2), // Read blue value at coordinates (10,10) (Z-coordinate can be omitted)\n          avg = (valR + valG + valB)/3; // Compute average pixel value\n       img(10,10,0) = img(10,10,1) = img(10,10,2) = avg; // Replace the color pixel (10,10) by the average grey value\n       \\endcode\n    **/\n#if cimg_verbosity>=3\n    T& operator()(const unsigned int x, const unsigned int y=0,\n                  const unsigned int z=0, const unsigned int c=0) {\n      const ulongT off = (ulongT)offset(x,y,z,c);\n      if (!_data || off>=size()) {\n        cimg::warn(_cimg_instance\n                   \"operator(): Invalid pixel request, at coordinates (%d,%d,%d,%d) [offset=%u].\",\n                   cimg_instance,\n                   (int)x,(int)y,(int)z,(int)c,off);\n        return *_data;\n      }\n      else return _data[off];\n    }\n\n    //! Access to a pixel value \\const.\n    const T& operator()(const unsigned int x, const unsigned int y=0,\n                        const unsigned int z=0, const unsigned int c=0) const {\n      return const_cast<CImg<T>*>(this)->operator()(x,y,z,c);\n    }\n\n    //! Access to a pixel value.\n    /**\n       \\param x X-coordinate of the pixel value.\n       \\param y Y-coordinate of the pixel value.","sourceCodeStart":13911,"sourceCodeEnd":13947,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L13911-L13947","documentation":"CImg<T>::operator()(x,y,z,c) (debug build, cimg_verbosity>=3) validates the computed offset against the image size before returning the pixel reference. If the image is empty (_data==0) or the coordinates fall outside [width,height,depth,spectrum], it warns non-fatally and returns *_data — on an empty image this dereferences a null pointer, so the warning flags a bug in the calling code rather than recovering safely.","triggerScenarios":"Indexing an empty (default-constructed or failed-load) CImg; passing x>=width(), y>=height(), z>=depth() or c>=spectrum(); loop bounds computed from a differently sized image; using stale dimensions after resize/assign.","commonSituations":"Off-by-one loops (e.g. `for x <= img.width()`), accessing pixels of an image whose load failed, iterating two images of different sizes in lockstep, forgetting that default-constructed CImg has size 0.","solutions":["Fix the loop bounds to use x<img.width(), y<img.height(), z<img.depth(), c<img.spectrum()","Check img.is_empty() (or !img) before pixel access, especially after load attempts","Use img.at(x,y,z,c) or img.atXY() which clamp/out-of-range-handle instead of raw operator()","Verify dimensions after every load/assign/resize before iterating"],"exampleFix":"// before\nfor (int x = 0; x <= img.width(); ++x) img(x,0,0,0);\n// after\nif (img.is_empty()) throw std::runtime_error(\"image not loaded\");\nfor (int x = 0; x < img.width(); ++x) img(x,0,0,0);","handlingStrategy":"type-guard","validationCode":"if (img.is_empty()) throw std::runtime_error(\"image not loaded\");\nif (!(x < img.width() && y < img.height() && z < img.depth() && c < img.spectrum()))\n  throw std::out_of_range(\"pixel coordinates out of bounds\");","typeGuard":"template <typename T>\nbool in_bounds(const CImg<T>& img, unsigned x, unsigned y, unsigned z = 0, unsigned c = 0) {\n  return !img.is_empty() && x < img.width() && y < img.height() &&\n         z < img.depth() && c < img.spectrum();\n}","tryCatchPattern":"try {\n  T& px = img.at(x, y, z, c); // at() clamps or throws safely\n} catch (const std::exception& e) {\n  // handle out-of-range access\n}","preventionTips":["Use strict < comparisons in pixel loops (never <=)","Check is_empty() after every load before accessing pixels","Prefer img.at()/atXY() accessors in debug phases","Keep image dimensions in sync with loop bounds derived from other images"],"tags":["bounds-check","pixel-access","null-deref","off-by-one","cimg"],"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-14T05:17:10.506Z"}