Skip to content

Commit 1011782

Browse files
authored
Declare proper parameter default values for imagegd2 (#10569)
1 parent a981ad7 commit 1011782

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

ext/gd/gd.c

+15-18
Original file line numberDiff line numberDiff line change
@@ -1720,38 +1720,35 @@ static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char
17201720
{
17211721
zval *imgind;
17221722
char *file = NULL;
1723-
zend_long quality = 0, type = 0;
1723+
zend_long quality = 128, type = 1;
17241724
gdImagePtr im;
17251725
FILE *fp;
17261726
size_t file_len = 0;
1727-
int argc = ZEND_NUM_ARGS();
1728-
int q = -1, t = 1;
17291727

17301728
/* The quality parameter for gd2 stands for chunk size */
17311729

17321730
switch (image_type) {
17331731
case PHP_GDIMG_TYPE_GD:
1734-
if (zend_parse_parameters(argc, "O|p!", &imgind, gd_image_ce, &file, &file_len) == FAILURE) {
1732+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|p!", &imgind, gd_image_ce, &file, &file_len) == FAILURE) {
17351733
RETURN_THROWS();
17361734
}
17371735
break;
17381736
case PHP_GDIMG_TYPE_GD2:
1739-
if (zend_parse_parameters(argc, "O|p!ll", &imgind, gd_image_ce, &file, &file_len, &quality, &type) == FAILURE) {
1737+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|p!ll", &imgind, gd_image_ce, &file, &file_len, &quality, &type) == FAILURE) {
17401738
RETURN_THROWS();
17411739
}
17421740
break;
17431741
EMPTY_SWITCH_DEFAULT_CASE()
17441742
}
17451743

1746-
im = php_gd_libgdimageptr_from_zval_p(imgind);
1747-
1748-
if (argc >= 3) {
1749-
q = quality;
1750-
if (argc == 4) {
1751-
t = type;
1752-
}
1744+
/* quality must fit in an int */
1745+
if (quality < INT_MIN || quality > INT_MAX) {
1746+
php_error_docref(NULL, E_WARNING, "Argument #3 ($chunk_size) must be between %d and %d", INT_MIN, INT_MAX);
1747+
RETURN_FALSE;
17531748
}
17541749

1750+
im = php_gd_libgdimageptr_from_zval_p(imgind);
1751+
17551752
if (file_len) {
17561753
PHP_GD_CHECK_OPEN_BASEDIR(file, "Invalid filename");
17571754

@@ -1766,10 +1763,10 @@ static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char
17661763
gdImageGd(im, fp);
17671764
break;
17681765
case PHP_GDIMG_TYPE_GD2:
1769-
if (q == -1) {
1770-
q = 128;
1766+
if (quality == -1) {
1767+
quality = 128;
17711768
}
1772-
gdImageGd2(im, fp, q, t);
1769+
gdImageGd2(im, fp, quality, type);
17731770
break;
17741771
EMPTY_SWITCH_DEFAULT_CASE()
17751772
}
@@ -1792,10 +1789,10 @@ static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char
17921789
gdImageGd(im, tmp);
17931790
break;
17941791
case PHP_GDIMG_TYPE_GD2:
1795-
if (q == -1) {
1796-
q = 128;
1792+
if (quality == -1) {
1793+
quality = 128;
17971794
}
1798-
gdImageGd2(im, tmp, q, t);
1795+
gdImageGd2(im, tmp, quality, type);
17991796
break;
18001797
EMPTY_SWITCH_DEFAULT_CASE()
18011798
}

ext/gd/gd.stub.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ function imagewbmp(GdImage $image, $file = null, ?int $foreground_color = null):
628628

629629
function imagegd(GdImage $image, ?string $file = null): bool {}
630630

631-
function imagegd2(GdImage $image, ?string $file = null, int $chunk_size = UNKNOWN, int $mode = UNKNOWN): bool {}
631+
function imagegd2(GdImage $image, ?string $file = null, int $chunk_size = 128, int $mode = IMG_GD2_RAW): bool {}
632632

633633
#ifdef HAVE_GD_BMP
634634
/** @param resource|string|null $file */

ext/gd/gd_arginfo.h

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)