Subject: | Bug in GD::Polygon |
Date: | Sun, 18 Feb 2018 11:24:58 -0700 |
To: | bug-GD [...] rt.cpan.org |
From: | Michael Cain <mcain6925 [...] comcast.net> |
GD has been enormously helpful for a personal project. There's at least one bug in the GD::Polygon clear method. Exists in at least v2.66, run with Perl versions 5.16 and 5.24, on multiple operating systems. Here's a short sample of code:
use GD;
$image = new GD::Image(200, 200, 1);
$white = $image->colorResolve(255,255,255);
$black = $image->colorResolve( 0, 0, 0);
$image->filledRectangle(0, 0, 199, 199, $white);
$poly = GD::Polygon->new;
$poly->addPt(100,100);
$poly->clear();
printf "Length of poly is %d\n", $poly->length;
$poly = GD::Polygon->new;
$poly->addPt(100,100);
$poly->clear();
$poly->addPt(100,100);
$poly->addPt(100,50);
$poly->addPt(50, 50);
$image->polygon($poly, $black);
When run, this produces the output
Length of poly is 1
Can't use an undefined value as an ARRAY reference at /Users/mcain/Library/ActivePerl-5.16/lib/GD/Polygon.pm line 71.
When $poly->length is printed, it should be zero. Looking at the code, $poly->clear() clears the array of points, but doesn't reset the length value. I haven't dug into the second line in detail, but assume that the undefined ARRAY reference comes from running off the end of the points array because the count is wrong. I've been working around this by creating new GD::Polygon instances in places where I think $poly->clear() would have been more natural. I'm not submitting a proposed patch because I don't have any real test rig to run it against. I'd be inclined to just get rid of the separate $self{'length'} value and use the computed length of the array everywhere.