Skip to content

Commit 9abb472

Browse files
committed
Fixed phpGH-18241: imagefilledpolygon underflow with poly coordinates.
backporting most of gdImageFilledPolygon except the array sort "optimisation" and adding new checks, if relevant needs to be upstreamed.
1 parent 821e346 commit 9abb472

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

Diff for: ext/gd/libgd/gd.c

+25-11
Original file line numberDiff line numberDiff line change
@@ -2688,18 +2688,20 @@ void gdImageFilledPolygon (gdImagePtr im, gdPointPtr p, int n, int c)
26882688
return;
26892689
}
26902690

2691-
if (overflow2(sizeof(int), n)) {
2692-
return;
2693-
}
2694-
26952691
if (c == gdAntiAliased) {
26962692
fill_color = im->AA_color;
26972693
} else {
26982694
fill_color = c;
26992695
}
27002696

27012697
if (!im->polyAllocated) {
2698+
if (overflow2(sizeof(int), n)) {
2699+
return;
2700+
}
27022701
im->polyInts = (int *) gdMalloc(sizeof(int) * n);
2702+
if (!im->polyInts) {
2703+
return;
2704+
}
27032705
im->polyAllocated = n;
27042706
}
27052707
if (im->polyAllocated < n) {
@@ -2710,6 +2712,9 @@ void gdImageFilledPolygon (gdImagePtr im, gdPointPtr p, int n, int c)
27102712
return;
27112713
}
27122714
im->polyInts = (int *) gdRealloc(im->polyInts, sizeof(int) * im->polyAllocated);
2715+
if (!im->polyInts) {
2716+
return;
2717+
}
27132718
}
27142719
miny = p[0].y;
27152720
maxy = p[0].y;
@@ -2736,11 +2741,12 @@ void gdImageFilledPolygon (gdImagePtr im, gdPointPtr p, int n, int c)
27362741
}
27372742
pmaxy = maxy;
27382743
/* 2.0.16: Optimization by Ilia Chipitsine -- don't waste time offscreen */
2739-
if (miny < 0) {
2740-
miny = 0;
2744+
/* 2.0.26: clipping rectangle is even better */
2745+
if (miny < im->cy1) {
2746+
miny = im->cy1;
27412747
}
2742-
if (maxy >= gdImageSY(im)) {
2743-
maxy = gdImageSY(im) - 1;
2748+
if (maxy > im->cy2) {
2749+
maxy = im->cy2;
27442750
}
27452751

27462752
/* Fix in 1.3: count a vertex only once */
@@ -2774,9 +2780,17 @@ void gdImageFilledPolygon (gdImagePtr im, gdPointPtr p, int n, int c)
27742780
* that Polygon and FilledPolygon for the same set of points have the
27752781
* same footprint.
27762782
*/
2777-
if (y >= y1 && y < y2) {
2778-
im->polyInts[ints++] = (float) ((y - y1) * (x2 - x1)) / (float) (y2 - y1) + 0.5 + x1;
2779-
} else if (y == pmaxy && y == y2) {
2783+
if ((y >= y1) && (y < y2)) {
2784+
if ((y1 > 0 && y < INT_MIN + y1) ||
2785+
(y1 < 0 && y > INT_MAX + y1) ||
2786+
(x1 > 0 && x2 < INT_MIN + x1) ||
2787+
(x1 < 0 && x2 > INT_MAX + x1) ||
2788+
overflow2((y - y1), (x2 - x1))) {
2789+
continue;
2790+
}
2791+
im->polyInts[ints++] = (int) ((float) ((y - y1) * (x2 - x1)) /
2792+
(float) (y2 - y1) + 0.5 + x1);
2793+
} else if ((y == pmaxy) && (y == y2)) {
27802794
im->polyInts[ints++] = x2;
27812795
}
27822796
}

0 commit comments

Comments
 (0)