Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion ext/gd/libgd/gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,9 @@ void gdImageEllipse(gdImagePtr im, int mx, int my, int w, int h, int c)

a=w>>1;
b=h>>1;
if (overflowMul3(a, b, b) || overflowMul3(b, a, a)) {
return;
}
gdImageSetPixel(im,mx+a, my, c);
gdImageSetPixel(im,mx-a, my, c);
mx1 = mx-a;my1 = my;
Expand Down Expand Up @@ -1816,7 +1819,9 @@ void gdImageFilledEllipse (gdImagePtr im, int mx, int my, int w, int h, int c)

a=w>>1;
b=h>>1;

if (overflowMul3(a, b, b) || overflowMul3(b, a, a)) {
return;
}
for (x = mx-a; x <= mx+a; x++) {
gdImageSetPixel(im, x, my, c);
}
Expand Down
18 changes: 18 additions & 0 deletions ext/gd/libgd/gd_security.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include "gd.h"
#include "gd_errors.h"
Expand All @@ -30,3 +31,20 @@ int overflow2(int a, int b)
}
return 0;
}

int overflowMul3(int a, int b, int c)
{
if (a < 0 || b < 0 || c < 0) {
return 1;
}
if (a == 0 || b == 0 || c == 0) {
return 0;
}
if (a > INT_MAX / b) {
return 1;
}
if ((int64_t)a * b > INT64_MAX / c) {
return 1;
}
return 0;
}
1 change: 1 addition & 0 deletions ext/gd/libgd/gdhelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern char *gd_strtok_r(char *s, char *sep, char **state);
netpbm fixes by Alan Cox. */

int overflow2(int a, int b);
int overflowMul3(int a, int b, int c);

#ifdef ZTS
#define gdMutexDeclare(x) MUTEX_T x
Expand Down
19 changes: 19 additions & 0 deletions ext/gd/tests/gh19739.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
GH-19739 (integer overflow in imageellipse / imagefilledellipse)
--EXTENSIONS--
gd
--FILE--
<?php
$im = imagecreatetruecolor(400, 300);
$color = imagecolorallocate($im, 150, 255, 0);

var_dump(imageellipse($im, 64, 150, 2147483647, 2147483647, $color));
var_dump(imagefilledellipse($im, 64, 150, 2147483647, 2147483647, $color));

imagedestroy($im);
echo "done" . PHP_EOL;
?>
--EXPECT--
bool(true)
bool(true)
done
Loading