{"record":{"id":"9f3f822cacf93837","repo":"Yalantis/uCrop","slug":"draw-rectangle-specified-color-is-null","errorCode":null,"errorMessage":"draw_rectangle(): Specified color is (null).","messagePattern":"draw_rectangle\\(\\): Specified color is \\(null\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":52244,"sourceCode":"\n    //! Draw a filled 3D rectangle.\n    /**\n       \\param x0 X-coordinate of the upper-left rectangle corner.\n       \\param y0 Y-coordinate of the upper-left rectangle corner.\n       \\param z0 Z-coordinate of the upper-left rectangle corner.\n       \\param x1 X-coordinate of the lower-right rectangle corner.\n       \\param y1 Y-coordinate of the lower-right rectangle corner.\n       \\param z1 Z-coordinate of the lower-right rectangle corner.\n       \\param color Pointer to \\c spectrum() consecutive values of type \\c T, defining the drawing color.\n       \\param opacity Drawing opacity.\n    **/\n    template<typename tc>\n    CImg<T>& draw_rectangle(const int x0, const int y0, const int z0,\n                            const int x1, const int y1, const int z1,\n                            const tc *const color, const float opacity=1) {\n      if (is_empty()) return *this;\n      if (!color)\n        throw CImgArgumentException(_cimg_instance\n                                    \"draw_rectangle(): Specified color is (null).\",\n                                    cimg_instance);\n      cimg_forC(*this,c) draw_rectangle(x0,y0,z0,c,x1,y1,z1,c,(T)color[c],opacity);\n      return *this;\n    }\n\n    //! Draw a filled 2D rectangle.\n    /**\n       \\param x0 X-coordinate of the upper-left rectangle corner.\n       \\param y0 Y-coordinate of the upper-left rectangle corner.\n       \\param x1 X-coordinate of the lower-right rectangle corner.\n       \\param y1 Y-coordinate of the lower-right rectangle corner.\n       \\param color Pointer to \\c spectrum() consecutive values of type \\c T, defining the drawing color.\n       \\param opacity Drawing opacity.\n    **/\n    template<typename tc>\n    CImg<T>& draw_rectangle(const int x0, const int y0,\n                            const int x1, const int y1,","sourceCodeStart":52226,"sourceCodeEnd":52262,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L52226-L52262","documentation":"CImg's draw_rectangle() with a templated color pointer received a null color pointer. CImg throws CImgArgumentException because there are no pixel values to fill the rectangle with; drawing cannot proceed.","triggerScenarios":"Calling img.draw_rectangle(x0,y0,z0,x1,y1,z1,color,opacity) where color==nullptr — typically an uninitialized pointer, a failed allocation, or an empty std::vector's data().","commonSituations":"Color array declared but never filled/allocated; .data() of an empty vector; refactoring changed a value overload to a pointer overload; conditionally-initialized color buffers.","solutions":["Ensure the color buffer exists and points to at least spectrum() values before the call.","Use the array overload with a literal, e.g. unsigned char color[] = {255,0,0};","If color may be missing, early-return or draw with a default color instead of null.","Check the pointer for null before calling and handle the fallback path."],"exampleFix":"// before\nconst unsigned char* color = vec.empty() ? nullptr : vec.data();\nimg.draw_rectangle(0,0,0,99,99,0, color, 1.0f); // throws when vec empty\n// after\nif (!vec.empty()) {\n  img.draw_rectangle(0,0,0,99,99,0, vec.data(), 1.0f);\n} else {\n  const unsigned char black[3] = {0,0,0};\n  img.draw_rectangle(0,0,0,99,99,0, black, 1.0f);\n}","handlingStrategy":"type-guard","validationCode":"if (!color || is_empty_color(color)) {\n  static const unsigned char fallback[3] = {0, 0, 0};\n  color = fallback;\n}\nimg.draw_rectangle(x0,y0,z0,x1,y1,z1, color, opacity);","typeGuard":"template <typename tc>\nbool color_is_valid(const tc* color, unsigned int spectrum) { return color != nullptr; }","tryCatchPattern":"try {\n  img.draw_rectangle(x0,y0,z0,x1,y1,z1, color, opacity);\n} catch (CImgArgumentException& e) {\n  std::cerr << \"Null color: \" << e.what() << std::endl;\n}","preventionTips":["Never pass .data() of an empty vector as a color","Use stack arrays for literal colors","Check pointers for null before any draw_* call"],"tags":["cimg","image-processing","null-pointer","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"}