Skip to content

Commit 6b68d94

Browse files
committed
Fix GH-22121: double-free in gdImageSetStyle() after overflow early return
gdImageSetStyle freed im->style before checking overflow2(). When the overflow check tripped and the function early-returned, im->style was left dangling. The next gdImageSetStyle, gdImageDestroy, or gdImageSetPixel gdStyled/gdStyledBrushed dispatch then freed or dereferenced it. Move the overflow check above the free to match upstream libgd (libgd/libgd src/gd.c::gdImageSetStyle), which has always had the check first. The original divergence was an oversight in 77ba248 when the overflow check was ported from libgd 2.0.29. Fixes GH-22121 Closes GH-22125
1 parent b0ef5fc commit 6b68d94

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.4.23
44

5+
- GD:
6+
. Fixed bug GH-22121 (Double free in gdImageSetStyle() after
7+
overflow-triggered early return). (iliaal)
8+
59
- Intl:
610
. Fix incorrect argument positions for uninitialized calendar arguments in
711
IntlCalendar::equals(), ::before(), ::after(), and ::isEquivalentTo().

ext/gd/libgd/gd.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2854,12 +2854,12 @@ int gdCompareInt (const void *a, const void *b)
28542854

28552855
void gdImageSetStyle (gdImagePtr im, int *style, int noOfPixels)
28562856
{
2857-
if (im->style) {
2858-
gdFree(im->style);
2859-
}
28602857
if (overflow2(sizeof (int), noOfPixels)) {
28612858
return;
28622859
}
2860+
if (im->style) {
2861+
gdFree(im->style);
2862+
}
28632863
im->style = (int *) gdMalloc(sizeof(int) * noOfPixels);
28642864
memcpy(im->style, style, sizeof(int) * noOfPixels);
28652865
im->styleLength = noOfPixels;

0 commit comments

Comments
 (0)