{"record":{"id":"6ecbf5cd4ba8b108","repo":"Yalantis/uCrop","slug":"draw-polygon-specified-color-is-null","errorCode":null,"errorMessage":"draw_polygon(): Specified color is (null).","messagePattern":"draw_polygon\\(\\): Specified color is \\(null\\)\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":52298,"sourceCode":"                      draw_line(nx1,ny1,nx0,ny1,color,opacity,pattern,false);\n      return draw_line(nx0,ny0,nx1,ny0,color,opacity,pattern,true).\n        draw_line(nx1,ny0 + 1,nx1,ny1 - 1,color,opacity,pattern,false).\n        draw_line(nx1,ny1,nx0,ny1,color,opacity,pattern,false).\n        draw_line(nx0,ny1 - 1,nx0,ny0 + 1,color,opacity,pattern,false);\n    }\n\n    //! Draw a filled 2D polygon.\n    /**\n       \\param points Set of polygon vertices.\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 tp, typename tc>\n    CImg<T>& draw_polygon(const CImg<tp>& points,\n                          const tc *const color, const float opacity=1) {\n      if (is_empty() || !points) return *this;\n      if (!color)\n        throw CImgArgumentException(_cimg_instance\n                                    \"draw_polygon(): Specified color is (null).\",\n                                    cimg_instance);\n      if (points.height()!=2)\n        throw CImgArgumentException(_cimg_instance\n                                    \"draw_polygon(): Invalid specified point set (%u,%u,%u,%u).\",\n                                    cimg_instance,\n                                    points._width,points._height,points._depth,points._spectrum);\n      CImg<intT> ipoints;\n      if (cimg::type<tp>::is_float()) ipoints = points.get_round();\n      else ipoints.assign(points,cimg::type<tp>::string()==cimg::type<int>::string());\n\n      if (ipoints._width==1) return draw_point(ipoints(0,0),ipoints(0,1),color,opacity);\n      if (ipoints._width==2) return draw_line(ipoints(0,0),ipoints(0,1),ipoints(1,0),ipoints(1,1),color,opacity);\n      if (ipoints._width==3) return draw_triangle(ipoints(0,0),ipoints(0,1),ipoints(1,0),ipoints(1,1),\n                                                  ipoints(2,0),ipoints(2,1),color,opacity);\n      cimg_init_scanline(opacity);\n      int\n        xmin = 0, ymin = 0,","sourceCodeStart":52280,"sourceCodeEnd":52316,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L52280-L52316","documentation":"CImg's filled draw_polygon(const CImg<tp>& points, const tc* color, opacity) received a null color pointer. CImg throws CImgArgumentException because polygon filling needs per-channel color values that a null pointer cannot supply.","triggerScenarios":"Calling img.draw_polygon(points, color, opacity) with color==nullptr (uninitialized buffer, empty vector's data(), or failed allocation). Note the call is silently skipped if points is empty, but a null color on non-empty points throws.","commonSituations":"Color array freed or never allocated; passing .data() of an empty std::vector; API migration from a value-based color API to pointer-based one.","solutions":["Allocate and populate the color buffer with img.spectrum() values before the call.","Pass a stack array like const unsigned char color[3]={255,255,255}.","Guard: if (color) draw_polygon(...); else use a default color.","Verify the vector holding the color is non-empty before taking .data()."],"exampleFix":"// before\nstd::vector<unsigned char> col; // empty\nimg.draw_polygon(points, col.data(), 1.0f); // throws: null color\n// after\nstd::vector<unsigned char> col = {255, 0, 0};\nimg.draw_polygon(points, col.data(), 1.0f);","handlingStrategy":"type-guard","validationCode":"if (!color) {\n  static const unsigned char white[3] = {255, 255, 255};\n  color = white;\n}\nimg.draw_polygon(points, color, opacity);","typeGuard":"template <typename tc>\nbool color_is_valid(const tc* color) { return color != nullptr; }","tryCatchPattern":"try {\n  img.draw_polygon(points, color, opacity);\n} catch (CImgArgumentException& e) {\n  std::cerr << \"Null polygon color: \" << e.what() << std::endl;\n}","preventionTips":["Keep color buffers alive for the duration of the draw call","Guard pointer-based color APIs with null checks","Prefer literal arrays for fixed colors"],"tags":["cimg","image-processing","null-pointer","polygon"],"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"}