Skip to content

Commit aa44a8e

Browse files
committed
Fix UNKNOWN default values in ext/mbstring and ext/gd
1 parent 58cb0e4 commit aa44a8e

File tree

9 files changed

+242
-243
lines changed

9 files changed

+242
-243
lines changed

ext/gd/gd.c

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,6 @@ typedef struct _gd_ext_image_object {
155155

156156
static zend_object_handlers php_gd_image_object_handlers;
157157

158-
static const zend_function_entry gd_image_object_methods[] = {
159-
PHP_FE_END
160-
};
161-
162158
static zend_function *php_gd_image_object_get_constructor(zend_object *object)
163159
{
164160
zend_throw_error(NULL, "You cannot initialize a GdImage object except through helper functions");
@@ -221,7 +217,7 @@ void php_gd_assign_libgdimageptr_as_extgdimage(zval *val, gdImagePtr image)
221217
static void php_gd_object_minit_helper()
222218
{
223219
zend_class_entry ce;
224-
INIT_CLASS_ENTRY(ce, "GdImage", gd_image_object_methods);
220+
INIT_CLASS_ENTRY(ce, "GdImage", class_GdImage_methods);
225221
gd_image_ce = zend_register_internal_class(&ce);
226222
gd_image_ce->ce_flags |= ZEND_ACC_FINAL;
227223
gd_image_ce->create_object = php_gd_image_object_create;
@@ -2542,16 +2538,16 @@ PHP_FUNCTION(imagecolortransparent)
25422538
{
25432539
zval *IM;
25442540
zend_long COL = 0;
2541+
zend_bool COL_IS_NULL = 1;
25452542
gdImagePtr im;
2546-
int argc = ZEND_NUM_ARGS();
25472543

2548-
if (zend_parse_parameters(argc, "O|l", &IM, gd_image_ce, &COL) == FAILURE) {
2544+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l!", &IM, gd_image_ce, &COL, &COL_IS_NULL) == FAILURE) {
25492545
RETURN_THROWS();
25502546
}
25512547

25522548
im = php_gd_libgdimageptr_from_zval_p(IM);
25532549

2554-
if (argc > 1) {
2550+
if (!COL_IS_NULL) {
25552551
gdImageColorTransparent(im, COL);
25562552
}
25572553

@@ -2564,17 +2560,17 @@ PHP_FUNCTION(imagecolortransparent)
25642560
PHP_FUNCTION(imageinterlace)
25652561
{
25662562
zval *IM;
2567-
int argc = ZEND_NUM_ARGS();
25682563
zend_long INT = 0;
2564+
zend_bool INT_IS_NULL = 1;
25692565
gdImagePtr im;
25702566

2571-
if (zend_parse_parameters(argc, "O|l", &IM, gd_image_ce, &INT) == FAILURE) {
2567+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l!", &IM, gd_image_ce, &INT, &INT_IS_NULL) == FAILURE) {
25722568
RETURN_THROWS();
25732569
}
25742570

25752571
im = php_gd_libgdimageptr_from_zval_p(IM);
25762572

2577-
if (argc > 1) {
2573+
if (!INT_IS_NULL) {
25782574
gdImageInterlace(im, INT);
25792575
}
25802576

@@ -2591,15 +2587,16 @@ static void php_imagepolygon(INTERNAL_FUNCTION_PARAMETERS, int filled)
25912587
{
25922588
zval *IM, *POINTS;
25932589
zend_long NPOINTS, COL;
2590+
zend_bool COL_IS_NULL = 1;
25942591
zval *var = NULL;
25952592
gdImagePtr im;
25962593
gdPointPtr points;
25972594
int npoints, col, nelem, i;
25982595

2599-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oal|l", &IM, gd_image_ce, &POINTS, &NPOINTS, &COL) == FAILURE) {
2596+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oal|l!", &IM, gd_image_ce, &POINTS, &NPOINTS, &COL, &COL_IS_NULL) == FAILURE) {
26002597
RETURN_THROWS();
26012598
}
2602-
if (ZEND_NUM_ARGS() == 3) {
2599+
if (COL_IS_NULL) {
26032600
COL = NPOINTS;
26042601
NPOINTS = zend_hash_num_elements(Z_ARRVAL_P(POINTS));
26052602
if (NPOINTS % 2 != 0) {
@@ -3752,7 +3749,7 @@ PHP_FUNCTION(imageaffine)
37523749
int i, nelems;
37533750
zval *zval_affine_elem = NULL;
37543751

3755-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oa|a", &IM, gd_image_ce, &z_affine, &z_rect) == FAILURE) {
3752+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oa|a!", &IM, gd_image_ce, &z_affine, &z_rect) == FAILURE) {
37563753
RETURN_THROWS();
37573754
}
37583755

@@ -3876,7 +3873,7 @@ PHP_FUNCTION(imageaffinematrixget)
38763873
double angle;
38773874

38783875
if (!options) {
3879-
zend_argument_type_error(2, "must be of type int|double when using rotate or shear");
3876+
zend_argument_type_error(2, "must be of type int|float when using rotate or shear");
38803877
RETURN_THROWS();
38813878
}
38823879

@@ -4027,26 +4024,29 @@ PHP_FUNCTION(imageresolution)
40274024
{
40284025
zval *IM;
40294026
gdImagePtr im;
4030-
zend_long res_x = GD_RESOLUTION, res_y = GD_RESOLUTION;
4027+
zend_long res_x, res_y;
4028+
zend_bool res_x_is_null = 1, res_y_is_null = 1;
40314029

4032-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|ll", &IM, gd_image_ce, &res_x, &res_y) == FAILURE) {
4030+
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) {
40334031
RETURN_THROWS();
40344032
}
40354033

40364034
im = php_gd_libgdimageptr_from_zval_p(IM);
40374035

4038-
switch (ZEND_NUM_ARGS()) {
4039-
case 3:
4040-
gdImageSetResolution(im, res_x, res_y);
4041-
RETURN_TRUE;
4042-
case 2:
4043-
gdImageSetResolution(im, res_x, res_x);
4044-
RETURN_TRUE;
4045-
default:
4046-
array_init(return_value);
4047-
add_next_index_long(return_value, gdImageResolutionX(im));
4048-
add_next_index_long(return_value, gdImageResolutionY(im));
4036+
if (!res_x_is_null && !res_y_is_null) {
4037+
gdImageSetResolution(im, res_x, res_y);
4038+
RETURN_TRUE;
4039+
} else if (!res_x_is_null && res_y_is_null) {
4040+
gdImageSetResolution(im, res_x, res_x);
4041+
RETURN_TRUE;
4042+
} else if (res_x_is_null && !res_y_is_null) {
4043+
gdImageSetResolution(im, res_y, res_y);
4044+
RETURN_TRUE;
40494045
}
4046+
4047+
array_init(return_value);
4048+
add_next_index_long(return_value, gdImageResolutionX(im));
4049+
add_next_index_long(return_value, gdImageResolutionY(im));
40504050
}
40514051
/* }}} */
40524052

@@ -4123,12 +4123,10 @@ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type,
41234123
zval *imgind;
41244124
char *file = NULL;
41254125
size_t file_len = 0;
4126-
zend_long quality, basefilter;
4126+
zend_long quality = -1, basefilter = -1;
41274127
zend_bool compressed = 1;
41284128
gdImagePtr im;
4129-
int argc = ZEND_NUM_ARGS();
4130-
int q = -1, i;
4131-
int f = -1;
4129+
int i;
41324130
gdIOCtx *ctx = NULL;
41334131
zval *to_zval = NULL;
41344132
php_stream *stream;
@@ -4139,37 +4137,45 @@ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type,
41394137
*/
41404138
switch (image_type) {
41414139
case PHP_GDIMG_TYPE_XBM:
4142-
if (zend_parse_parameters(argc, "Op!|ll", &imgind, gd_image_ce, &file, &file_len, &quality, &basefilter) == FAILURE) {
4140+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Op!|l",
4141+
&imgind, gd_image_ce, &file, &file_len, &quality
4142+
) == FAILURE) {
41434143
RETURN_THROWS();
41444144
}
41454145
break;
41464146
case PHP_GDIMG_TYPE_BMP:
4147-
if (zend_parse_parameters(argc, "O|z!b", &imgind, gd_image_ce, &to_zval, &compressed) == FAILURE) {
4147+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!b", &imgind, gd_image_ce, &to_zval, &compressed) == FAILURE) {
4148+
RETURN_THROWS();
4149+
}
4150+
break;
4151+
case PHP_GDIMG_TYPE_PNG:
4152+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!ll",
4153+
&imgind, gd_image_ce, &to_zval, &quality, &basefilter
4154+
) == FAILURE) {
4155+
RETURN_THROWS();
4156+
}
4157+
break;
4158+
case PHP_GDIMG_TYPE_GIF:
4159+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!", &imgind, gd_image_ce, &to_zval) == FAILURE) {
41484160
RETURN_THROWS();
41494161
}
41504162
break;
41514163
default:
4152-
/* PHP_GDIMG_TYPE_GIF
4153-
* PHP_GDIMG_TYPE_PNG
4164+
/*
41544165
* PHP_GDIMG_TYPE_JPG
41554166
* PHP_GDIMG_TYPE_WBM
41564167
* PHP_GDIMG_TYPE_WEBP
4157-
* */
4158-
if (zend_parse_parameters(argc, "O|z!ll", &imgind, gd_image_ce, &to_zval, &quality, &basefilter) == FAILURE) {
4168+
*/
4169+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!l",
4170+
&imgind, gd_image_ce, &to_zval, &quality
4171+
) == FAILURE) {
41594172
RETURN_THROWS();
41604173
}
41614174
}
41624175

41634176
im = php_gd_libgdimageptr_from_zval_p(imgind);
41644177

4165-
if (image_type != PHP_GDIMG_TYPE_BMP && argc >= 3) {
4166-
q = quality; /* or colorindex for foreground of BW images (defaults to black) */
4167-
if (argc == 4) {
4168-
f = basefilter;
4169-
}
4170-
}
4171-
4172-
if (argc > 1 && to_zval != NULL) {
4178+
if (to_zval != NULL) {
41734179
if (Z_TYPE_P(to_zval) == IS_RESOURCE) {
41744180
php_stream_from_zval_no_verify(stream, to_zval);
41754181
if (stream == NULL) {
@@ -4190,7 +4196,7 @@ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type,
41904196
php_error_docref(NULL, E_WARNING, "Invalid 2nd parameter, it must a filename or a stream");
41914197
RETURN_FALSE;
41924198
}
4193-
} else if (argc > 1 && file != NULL) {
4199+
} else if (file != NULL) {
41944200
stream = php_stream_open_wrapper(file, "wb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL);
41954201
if (stream == NULL) {
41964202
RETURN_FALSE;
@@ -4216,29 +4222,29 @@ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type,
42164222

42174223
switch(image_type) {
42184224
case PHP_GDIMG_TYPE_JPG:
4219-
(*func_p)(im, ctx, q);
4225+
(*func_p)(im, ctx, quality);
42204226
break;
42214227
case PHP_GDIMG_TYPE_WEBP:
4222-
if (q == -1) {
4223-
q = 80;
4228+
if (quality == -1) {
4229+
quality = 80;
42244230
}
4225-
(*func_p)(im, ctx, q);
4231+
(*func_p)(im, ctx, quality);
42264232
break;
42274233
case PHP_GDIMG_TYPE_PNG:
4228-
(*func_p)(im, ctx, q, f);
4234+
(*func_p)(im, ctx, quality, basefilter);
42294235
break;
42304236
case PHP_GDIMG_TYPE_XBM:
42314237
case PHP_GDIMG_TYPE_WBM:
4232-
if (argc < 3) {
4238+
if (quality == -1) {
42334239
for(i=0; i < gdImageColorsTotal(im); i++) {
42344240
if(!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) break;
42354241
}
4236-
q = i;
4242+
quality = i;
42374243
}
42384244
if (image_type == PHP_GDIMG_TYPE_XBM) {
4239-
(*func_p)(im, file ? file : "", q, ctx);
4245+
(*func_p)(im, file ? file : "", quality, ctx);
42404246
} else {
4241-
(*func_p)(im, q, ctx);
4247+
(*func_p)(im, quality, ctx);
42424248
}
42434249
break;
42444250
case PHP_GDIMG_TYPE_BMP:

ext/gd/gd.stub.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
/** @generate-function-entries */
44

5+
final class GdImage
6+
{
7+
}
8+
59
function gd_info(): array {}
610

711
function imageloadfont(string $filename): int|false {}
@@ -96,30 +100,30 @@ function imagecreatefrombmp(string $filename): GdImage|false {}
96100
function imagecreatefromtga(string $filename): GdImage|false {}
97101
#endif
98102

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

101105
function imagegif(GdImage $im, $to = null): bool {}
102106

103107
#ifdef HAVE_GD_PNG
104-
function imagepng(GdImage $im, $to = null, int $quality = UNKNOWN, int $filters = UNKNOWN): bool {}
108+
function imagepng(GdImage $im, $to = null, int $quality = -1, int $filters = -1): bool {}
105109
#endif
106110

107111
#ifdef HAVE_GD_WEBP
108-
function imagewebp(GdImage $im, $to = null, int $quality = UNKNOWN): bool {}
112+
function imagewebp(GdImage $im, $to = null, int $quality = -1): bool {}
109113
#endif
110114

111115
#ifdef HAVE_GD_JPG
112-
function imagejpeg(GdImage $im, $to = null, int $quality = UNKNOWN): bool {}
116+
function imagejpeg(GdImage $im, $to = null, int $quality = -1): bool {}
113117
#endif
114118

115-
function imagewbmp(GdImage $im, $to = null, int $foreground = UNKNOWN): bool {}
119+
function imagewbmp(GdImage $im, $to = null, int $foreground = -1): bool {}
116120

117121
function imagegd(GdImage $im, $to = UNKNOWN): bool {}
118122

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

121125
#ifdef HAVE_GD_BMP
122-
function imagebmp(GdImage $im, $to = null, int $compressed = 1): bool {}
126+
function imagebmp(GdImage $im, $to = null, bool $compressed = true): bool {}
123127
#endif
124128

125129
function imagedestroy(GdImage $im): bool {}
@@ -166,15 +170,15 @@ function imagefill(GdImage $im, int $x, int $y, int $col): bool {}
166170

167171
function imagecolorstotal(GdImage $im): int {}
168172

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

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

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

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

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

179183
function imagefontwidth(int $font): int {}
180184

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

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

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

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

237+
/** @param array|int|float $options */
233238
function imageaffinematrixget(int $type, $options = UNKNOWN): array|false {}
234239

235240
function imageaffinematrixconcat(array $m1, array $m2): array|false {}
@@ -238,4 +243,4 @@ function imagegetinterpolation(GdImage $im): int {}
238243

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

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

0 commit comments

Comments
 (0)