Skip to content

Commit ac0da09

Browse files
committed
Fix UNKNOWN default values in ext/mbstring and ext/gd
Closes GH-5598
1 parent 118fa69 commit ac0da09

File tree

9 files changed

+240
-252
lines changed

9 files changed

+240
-252
lines changed

ext/gd/gd.c

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,20 +1869,20 @@ PHP_FUNCTION(imagexbm)
18691869
zval *imgind;
18701870
char *file = NULL;
18711871
size_t file_len = 0;
1872-
zend_long foreground_color = -1;
1872+
zend_long foreground_color;
1873+
zend_bool foreground_color_is_null = 1;
18731874
gdImagePtr im;
1874-
int argc = ZEND_NUM_ARGS();
18751875
int i;
18761876
gdIOCtx *ctx = NULL;
18771877
php_stream *stream;
18781878

1879-
if (zend_parse_parameters(argc, "Op!|l", &imgind, gd_image_ce, &file, &file_len, &foreground_color) == FAILURE) {
1879+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Op!|l!", &imgind, gd_image_ce, &file, &file_len, &foreground_color, &foreground_color_is_null) == FAILURE) {
18801880
RETURN_THROWS();
18811881
}
18821882

18831883
im = php_gd_libgdimageptr_from_zval_p(imgind);
18841884

1885-
if (argc > 1 && file != NULL) {
1885+
if (file != NULL) {
18861886
stream = php_stream_open_wrapper(file, "wb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL);
18871887
if (stream == NULL) {
18881888
RETURN_FALSE;
@@ -1893,10 +1893,13 @@ PHP_FUNCTION(imagexbm)
18931893
ctx = create_output_context();
18941894
}
18951895

1896-
if (argc < 3) {
1896+
if (foreground_color_is_null) {
18971897
for (i=0; i < gdImageColorsTotal(im); i++) {
1898-
if (!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) break;
1898+
if (!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) {
1899+
break;
1900+
}
18991901
}
1902+
19001903
foreground_color = i;
19011904
}
19021905

@@ -1946,20 +1949,20 @@ PHP_FUNCTION(imagejpeg)
19461949
PHP_FUNCTION(imagewbmp)
19471950
{
19481951
zval *imgind;
1949-
zend_long foreground_color = -1;
1952+
zend_long foreground_color;
1953+
zend_long foreground_color_is_null = 1;
19501954
gdImagePtr im;
1951-
int argc = ZEND_NUM_ARGS();
19521955
int i;
19531956
gdIOCtx *ctx = NULL;
19541957
zval *to_zval = NULL;
19551958

1956-
if (zend_parse_parameters(argc, "O|z!l", &imgind, gd_image_ce, &to_zval, &foreground_color) == FAILURE) {
1959+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!l!", &imgind, gd_image_ce, &to_zval, &foreground_color) == FAILURE) {
19571960
RETURN_THROWS();
19581961
}
19591962

19601963
im = php_gd_libgdimageptr_from_zval_p(imgind);
19611964

1962-
if (argc > 1 && to_zval != NULL) {
1965+
if (to_zval != NULL) {
19631966
ctx = create_stream_context_from_zval(to_zval);
19641967
if (!ctx) {
19651968
RETURN_FALSE;
@@ -1968,10 +1971,11 @@ PHP_FUNCTION(imagewbmp)
19681971
ctx = create_output_context();
19691972
}
19701973

1971-
if (argc < 3) {
1974+
if (foreground_color_is_null) {
19721975
for (i=0; i < gdImageColorsTotal(im); i++) {
1973-
if (!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i))
1976+
if (!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) {
19741977
break;
1978+
}
19751979
}
19761980

19771981
foreground_color = i;
@@ -2006,17 +2010,16 @@ PHP_FUNCTION(imagebmp)
20062010
zval *imgind;
20072011
zend_bool compressed = 1;
20082012
gdImagePtr im;
2009-
int argc = ZEND_NUM_ARGS();
20102013
gdIOCtx *ctx = NULL;
20112014
zval *to_zval = NULL;
20122015

2013-
if (zend_parse_parameters(argc, "O|z!b", &imgind, gd_image_ce, &to_zval, &compressed) == FAILURE) {
2016+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!b", &imgind, gd_image_ce, &to_zval, &compressed) == FAILURE) {
20142017
RETURN_THROWS();
20152018
}
20162019

20172020
im = php_gd_libgdimageptr_from_zval_p(imgind);
20182021

2019-
if (argc > 1 && to_zval != NULL) {
2022+
if (to_zval != NULL) {
20202023
ctx = create_stream_context_from_zval(to_zval);
20212024
if (!ctx) {
20222025
RETURN_FALSE;
@@ -2555,16 +2558,16 @@ PHP_FUNCTION(imagecolortransparent)
25552558
{
25562559
zval *IM;
25572560
zend_long COL = 0;
2561+
zend_bool COL_IS_NULL = 1;
25582562
gdImagePtr im;
2559-
int argc = ZEND_NUM_ARGS();
25602563

2561-
if (zend_parse_parameters(argc, "O|l", &IM, gd_image_ce, &COL) == FAILURE) {
2564+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l!", &IM, gd_image_ce, &COL, &COL_IS_NULL) == FAILURE) {
25622565
RETURN_THROWS();
25632566
}
25642567

25652568
im = php_gd_libgdimageptr_from_zval_p(IM);
25662569

2567-
if (argc > 1) {
2570+
if (!COL_IS_NULL) {
25682571
gdImageColorTransparent(im, COL);
25692572
}
25702573

@@ -2576,17 +2579,17 @@ PHP_FUNCTION(imagecolortransparent)
25762579
PHP_FUNCTION(imageinterlace)
25772580
{
25782581
zval *IM;
2579-
int argc = ZEND_NUM_ARGS();
25802582
zend_long INT = 0;
2583+
zend_bool INT_IS_NULL = 1;
25812584
gdImagePtr im;
25822585

2583-
if (zend_parse_parameters(argc, "O|l", &IM, gd_image_ce, &INT) == FAILURE) {
2586+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l!", &IM, gd_image_ce, &INT, &INT_IS_NULL) == FAILURE) {
25842587
RETURN_THROWS();
25852588
}
25862589

25872590
im = php_gd_libgdimageptr_from_zval_p(IM);
25882591

2589-
if (argc > 1) {
2592+
if (!INT_IS_NULL) {
25902593
gdImageInterlace(im, INT);
25912594
}
25922595

@@ -2603,15 +2606,16 @@ static void php_imagepolygon(INTERNAL_FUNCTION_PARAMETERS, int filled)
26032606
{
26042607
zval *IM, *POINTS;
26052608
zend_long NPOINTS, COL;
2609+
zend_bool COL_IS_NULL = 1;
26062610
zval *var = NULL;
26072611
gdImagePtr im;
26082612
gdPointPtr points;
26092613
int npoints, col, nelem, i;
26102614

2611-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oal|l", &IM, gd_image_ce, &POINTS, &NPOINTS, &COL) == FAILURE) {
2615+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oal|l!", &IM, gd_image_ce, &POINTS, &NPOINTS, &COL, &COL_IS_NULL) == FAILURE) {
26122616
RETURN_THROWS();
26132617
}
2614-
if (ZEND_NUM_ARGS() == 3) {
2618+
if (COL_IS_NULL) {
26152619
COL = NPOINTS;
26162620
NPOINTS = zend_hash_num_elements(Z_ARRVAL_P(POINTS));
26172621
if (NPOINTS % 2 != 0) {
@@ -3733,7 +3737,7 @@ PHP_FUNCTION(imageaffine)
37333737
int i, nelems;
37343738
zval *zval_affine_elem = NULL;
37353739

3736-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oa|a", &IM, gd_image_ce, &z_affine, &z_rect) == FAILURE) {
3740+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oa|a!", &IM, gd_image_ce, &z_affine, &z_rect) == FAILURE) {
37373741
RETURN_THROWS();
37383742
}
37393743

@@ -3856,7 +3860,7 @@ PHP_FUNCTION(imageaffinematrixget)
38563860
double angle;
38573861

38583862
if (!options) {
3859-
zend_argument_type_error(2, "must be of type int|double when using rotate or shear");
3863+
zend_argument_type_error(2, "must be of type int|float when using rotate or shear");
38603864
RETURN_THROWS();
38613865
}
38623866

@@ -4003,26 +4007,29 @@ PHP_FUNCTION(imageresolution)
40034007
{
40044008
zval *IM;
40054009
gdImagePtr im;
4006-
zend_long res_x = GD_RESOLUTION, res_y = GD_RESOLUTION;
4010+
zend_long res_x, res_y;
4011+
zend_bool res_x_is_null = 1, res_y_is_null = 1;
40074012

4008-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|ll", &IM, gd_image_ce, &res_x, &res_y) == FAILURE) {
4013+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l!l!", &IM, gd_image_ce, &res_x, &res_x_is_null, &res_y, &res_y_is_null) == FAILURE) {
40094014
RETURN_THROWS();
40104015
}
40114016

40124017
im = php_gd_libgdimageptr_from_zval_p(IM);
40134018

4014-
switch (ZEND_NUM_ARGS()) {
4015-
case 3:
4016-
gdImageSetResolution(im, res_x, res_y);
4017-
RETURN_TRUE;
4018-
case 2:
4019-
gdImageSetResolution(im, res_x, res_x);
4020-
RETURN_TRUE;
4021-
default:
4022-
array_init(return_value);
4023-
add_next_index_long(return_value, gdImageResolutionX(im));
4024-
add_next_index_long(return_value, gdImageResolutionY(im));
4019+
if (!res_x_is_null && !res_y_is_null) {
4020+
gdImageSetResolution(im, res_x, res_y);
4021+
RETURN_TRUE;
4022+
} else if (!res_x_is_null && res_y_is_null) {
4023+
gdImageSetResolution(im, res_x, res_x);
4024+
RETURN_TRUE;
4025+
} else if (res_x_is_null && !res_y_is_null) {
4026+
gdImageSetResolution(im, res_y, res_y);
4027+
RETURN_TRUE;
40254028
}
4029+
4030+
array_init(return_value);
4031+
add_next_index_long(return_value, gdImageResolutionX(im));
4032+
add_next_index_long(return_value, gdImageResolutionY(im));
40264033
}
40274034
/* }}} */
40284035

@@ -4151,24 +4158,26 @@ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type,
41514158
zval *imgind;
41524159
zend_long quality = -1, basefilter = -1;
41534160
gdImagePtr im;
4154-
int argc = ZEND_NUM_ARGS();
4155-
int i;
41564161
gdIOCtx *ctx = NULL;
41574162
zval *to_zval = NULL;
41584163

4159-
if (image_type == PHP_GDIMG_TYPE_PNG) {
4160-
if (zend_parse_parameters(argc, "O|z!ll", &imgind, gd_image_ce, &to_zval, &quality, &basefilter) == FAILURE) {
4164+
if (image_type == PHP_GDIMG_TYPE_GIF) {
4165+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z", &imgind, gd_image_ce, &to_zval) == FAILURE) {
4166+
RETURN_THROWS();
4167+
}
4168+
} else if (image_type == PHP_GDIMG_TYPE_PNG) {
4169+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!ll", &imgind, gd_image_ce, &to_zval, &quality, &basefilter) == FAILURE) {
41614170
RETURN_THROWS();
41624171
}
41634172
} else {
4164-
if (zend_parse_parameters(argc, "O|z!l", &imgind, gd_image_ce, &to_zval, &quality) == FAILURE) {
4173+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!l", &imgind, gd_image_ce, &to_zval, &quality) == FAILURE) {
41654174
RETURN_THROWS();
41664175
}
41674176
}
41684177

41694178
im = php_gd_libgdimageptr_from_zval_p(imgind);
41704179

4171-
if (argc > 1 && to_zval != NULL) {
4180+
if (to_zval != NULL) {
41724181
ctx = create_stream_context_from_zval(to_zval);
41734182
if (!ctx) {
41744183
RETURN_FALSE;
@@ -4190,16 +4199,6 @@ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type,
41904199
case PHP_GDIMG_TYPE_PNG:
41914200
(*func_p)(im, ctx, (int) quality, (int) basefilter);
41924201
break;
4193-
case PHP_GDIMG_TYPE_WBM:
4194-
if (argc < 3) {
4195-
for (i=0; i < gdImageColorsTotal(im); i++) {
4196-
if (!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) break;
4197-
}
4198-
quality = (int) i;
4199-
}
4200-
4201-
(*func_p)(im, (int) quality, ctx);
4202-
break;
42034202
case PHP_GDIMG_TYPE_GIF:
42044203
(*func_p)(im, ctx);
42054204
break;

ext/gd/gd.stub.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function gd_info(): array {}
1010

1111
function imageloadfont(string $filename): int|false {}
1212

13-
function imagesetstyle($im, array $styles): bool {}
13+
function imagesetstyle(GdImage $im, array $styles): bool {}
1414

1515
function imagecreatetruecolor(int $x_size, int $y_size): GdImage|false {}
1616

@@ -20,7 +20,7 @@ function imagetruecolortopalette(GdImage $im, bool $ditherFlag, int $colorWanted
2020

2121
function imagepalettetotruecolor(GdImage $im): bool {}
2222

23-
function imagecolormatch($im1, $im2): bool {}
23+
function imagecolormatch(GdImage $im1, GdImage $im2): bool {}
2424

2525
function imagesetthickness(GdImage $im, int $thickness): bool {}
2626

@@ -54,9 +54,9 @@ function imagegrabscreen(): GdImage|false {}
5454

5555
function imagerotate(GdImage $im, float $angle, int $bgdcolor, int $ignoretransparent = 0): GdImage|false {}
5656

57-
function imagesettile(GdImage $im, $tile): bool {}
57+
function imagesettile(GdImage $im, GdImage $tile): bool {}
5858

59-
function imagesetbrush(GdImage $im, $brush): bool {}
59+
function imagesetbrush(GdImage $im, GdImage $brush): bool {}
6060

6161
function imagecreate(int $x_size, int $y_size): GdImage|false {}
6262

@@ -100,30 +100,36 @@ function imagecreatefrombmp(string $filename): GdImage|false {}
100100
function imagecreatefromtga(string $filename): GdImage|false {}
101101
#endif
102102

103-
function imagexbm(GdImage $im, ?string $filename, int $foreground = UNKNOWN): bool {}
103+
function imagexbm(GdImage $im, ?string $filename, ?int $foreground = null): bool {}
104104

105+
/** @param resource|string|null $to */
105106
function imagegif(GdImage $im, $to = null): bool {}
106107

107108
#ifdef HAVE_GD_PNG
108-
function imagepng(GdImage $im, $to = null, int $quality = UNKNOWN, int $filters = UNKNOWN): bool {}
109+
/** @param resource|string|null $to */
110+
function imagepng(GdImage $im, $to = null, int $quality = -1, int $filters = -1): bool {}
109111
#endif
110112

111113
#ifdef HAVE_GD_WEBP
112-
function imagewebp(GdImage $im, $to = null, int $quality = UNKNOWN): bool {}
114+
/** @param resource|string|null $to */
115+
function imagewebp(GdImage $im, $to = null, int $quality = -1): bool {}
113116
#endif
114117

115118
#ifdef HAVE_GD_JPG
116-
function imagejpeg(GdImage $im, $to = null, int $quality = UNKNOWN): bool {}
119+
/** @param resource|string|null $to */
120+
function imagejpeg(GdImage $im, $to = null, int $quality = -1): bool {}
117121
#endif
118122

119-
function imagewbmp(GdImage $im, $to = null, int $foreground = UNKNOWN): bool {}
123+
/** @param resource|string|null $to */
124+
function imagewbmp(GdImage $im, $to = null, ?int $foreground = null): bool {}
120125

121126
function imagegd(GdImage $im, $to = UNKNOWN): bool {}
122127

123128
function imagegd2(GdImage $im, $to = UNKNOWN, int $chunk_size = UNKNOWN, int $type = UNKNOWN): bool {}
124129

125130
#ifdef HAVE_GD_BMP
126-
function imagebmp(GdImage $im, $to = null, int $compressed = 1): bool {}
131+
/** @param resource|string|null $to */
132+
function imagebmp(GdImage $im, $to = null, bool $compressed = true): bool {}
127133
#endif
128134

129135
function imagedestroy(GdImage $im): bool {}
@@ -170,15 +176,15 @@ function imagefill(GdImage $im, int $x, int $y, int $col): bool {}
170176

171177
function imagecolorstotal(GdImage $im): int {}
172178

173-
function imagecolortransparent(GdImage $im, int $col = UNKNOWN): ?int {}
179+
function imagecolortransparent(GdImage $im, ?int $col = null): ?int {}
174180

175-
function imageinterlace(GdImage $im, int $interlace = UNKNOWN): ?int {}
181+
function imageinterlace(GdImage $im, ?int $interlace = null): ?int {}
176182

177-
function imagepolygon(GdImage $im, array $points, int $num_points_or_col, int $col = UNKNOWN): bool {}
183+
function imagepolygon(GdImage $im, array $points, int $num_points_or_col, ?int $col = null): bool {}
178184

179-
function imageopenpolygon(GdImage $im, array $points, int $num_points_or_col, int $col = UNKNOWN): bool {}
185+
function imageopenpolygon(GdImage $im, array $points, int $num_points_or_col, ?int $col = null): bool {}
180186

181-
function imagefilledpolygon(GdImage $im, array $points, int $num_points_or_col, int $col = UNKNOWN): bool {}
187+
function imagefilledpolygon(GdImage $im, array $points, int $num_points_or_col, ?int $col = null): bool {}
182188

183189
function imagefontwidth(int $font): int {}
184190

@@ -230,10 +236,11 @@ function imagecrop(GdImage $im, array $rect): GdImage|false {}
230236

231237
function imagecropauto(GdImage $im, int $mode = IMG_CROP_DEFAULT, float $threshold = 0.5, int $color = -1): GdImage|false {}
232238

233-
function imagescale(GdImage $im, int $new_width, int $new_height = UNKNOWN, int $mode = IMG_BILINEAR_FIXED): GdImage|false {}
239+
function imagescale(GdImage $im, int $new_width, int $new_height = -1, int $mode = IMG_BILINEAR_FIXED): GdImage|false {}
234240

235-
function imageaffine(GdImage $im, array $affine, array $clip = UNKNOWN): GdImage|false {}
241+
function imageaffine(GdImage $im, array $affine, ?array $clip = null): GdImage|false {}
236242

243+
/** @param array|int|float $options */
237244
function imageaffinematrixget(int $type, $options = UNKNOWN): array|false {}
238245

239246
function imageaffinematrixconcat(array $m1, array $m2): array|false {}
@@ -242,4 +249,4 @@ function imagegetinterpolation(GdImage $im): int {}
242249

243250
function imagesetinterpolation(GdImage $im, int $method = IMG_BILINEAR_FIXED): bool {}
244251

245-
function imageresolution(GdImage $im, int $res_x = UNKNOWN, int $res_y = UNKNOWN): array|bool {}
252+
function imageresolution(GdImage $im, ?int $res_x = null, ?int $res_y = null): array|bool {}

0 commit comments

Comments
 (0)