Skip to content

Fix UNKNOWN default values in ext/xml-rpc, ext/mbstring, and ext/gd #5598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
107 changes: 53 additions & 54 deletions ext/gd/gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1869,20 +1869,20 @@ PHP_FUNCTION(imagexbm)
zval *imgind;
char *file = NULL;
size_t file_len = 0;
zend_long foreground_color = -1;
zend_long foreground_color;
zend_bool foreground_color_is_null = 1;
gdImagePtr im;
int argc = ZEND_NUM_ARGS();
int i;
gdIOCtx *ctx = NULL;
php_stream *stream;

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

im = php_gd_libgdimageptr_from_zval_p(imgind);

if (argc > 1 && file != NULL) {
if (file != NULL) {
stream = php_stream_open_wrapper(file, "wb", REPORT_ERRORS|IGNORE_PATH|IGNORE_URL_WIN, NULL);
if (stream == NULL) {
RETURN_FALSE;
Expand All @@ -1893,10 +1893,13 @@ PHP_FUNCTION(imagexbm)
ctx = create_output_context();
}

if (argc < 3) {
if (foreground_color_is_null) {
for (i=0; i < gdImageColorsTotal(im); i++) {
if (!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) break;
if (!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) {
break;
}
}

foreground_color = i;
}

Expand Down Expand Up @@ -1946,20 +1949,20 @@ PHP_FUNCTION(imagejpeg)
PHP_FUNCTION(imagewbmp)
{
zval *imgind;
zend_long foreground_color = -1;
zend_long foreground_color;
zend_long foreground_color_is_null = 1;
gdImagePtr im;
int argc = ZEND_NUM_ARGS();
int i;
gdIOCtx *ctx = NULL;
zval *to_zval = NULL;

if (zend_parse_parameters(argc, "O|z!l", &imgind, gd_image_ce, &to_zval, &foreground_color) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!l!", &imgind, gd_image_ce, &to_zval, &foreground_color) == FAILURE) {
RETURN_THROWS();
}

im = php_gd_libgdimageptr_from_zval_p(imgind);

if (argc > 1 && to_zval != NULL) {
if (to_zval != NULL) {
ctx = create_stream_context_from_zval(to_zval);
if (!ctx) {
RETURN_FALSE;
Expand All @@ -1968,10 +1971,11 @@ PHP_FUNCTION(imagewbmp)
ctx = create_output_context();
}

if (argc < 3) {
if (foreground_color_is_null) {
for (i=0; i < gdImageColorsTotal(im); i++) {
if (!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i))
if (!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) {
break;
}
}

foreground_color = i;
Expand Down Expand Up @@ -2006,17 +2010,16 @@ PHP_FUNCTION(imagebmp)
zval *imgind;
zend_bool compressed = 1;
gdImagePtr im;
int argc = ZEND_NUM_ARGS();
gdIOCtx *ctx = NULL;
zval *to_zval = NULL;

if (zend_parse_parameters(argc, "O|z!b", &imgind, gd_image_ce, &to_zval, &compressed) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!b", &imgind, gd_image_ce, &to_zval, &compressed) == FAILURE) {
RETURN_THROWS();
}

im = php_gd_libgdimageptr_from_zval_p(imgind);

if (argc > 1 && to_zval != NULL) {
if (to_zval != NULL) {
ctx = create_stream_context_from_zval(to_zval);
if (!ctx) {
RETURN_FALSE;
Expand Down Expand Up @@ -2555,16 +2558,16 @@ PHP_FUNCTION(imagecolortransparent)
{
zval *IM;
zend_long COL = 0;
zend_bool COL_IS_NULL = 1;
gdImagePtr im;
int argc = ZEND_NUM_ARGS();

if (zend_parse_parameters(argc, "O|l", &IM, gd_image_ce, &COL) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l!", &IM, gd_image_ce, &COL, &COL_IS_NULL) == FAILURE) {
RETURN_THROWS();
}

im = php_gd_libgdimageptr_from_zval_p(IM);

if (argc > 1) {
if (!COL_IS_NULL) {
gdImageColorTransparent(im, COL);
}

Expand All @@ -2576,17 +2579,17 @@ PHP_FUNCTION(imagecolortransparent)
PHP_FUNCTION(imageinterlace)
{
zval *IM;
int argc = ZEND_NUM_ARGS();
zend_long INT = 0;
zend_bool INT_IS_NULL = 1;
gdImagePtr im;

if (zend_parse_parameters(argc, "O|l", &IM, gd_image_ce, &INT) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|l!", &IM, gd_image_ce, &INT, &INT_IS_NULL) == FAILURE) {
RETURN_THROWS();
}

im = php_gd_libgdimageptr_from_zval_p(IM);

if (argc > 1) {
if (!INT_IS_NULL) {
gdImageInterlace(im, INT);
}

Expand All @@ -2603,15 +2606,16 @@ static void php_imagepolygon(INTERNAL_FUNCTION_PARAMETERS, int filled)
{
zval *IM, *POINTS;
zend_long NPOINTS, COL;
zend_bool COL_IS_NULL = 1;
zval *var = NULL;
gdImagePtr im;
gdPointPtr points;
int npoints, col, nelem, i;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oal|l", &IM, gd_image_ce, &POINTS, &NPOINTS, &COL) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oal|l!", &IM, gd_image_ce, &POINTS, &NPOINTS, &COL, &COL_IS_NULL) == FAILURE) {
RETURN_THROWS();
}
if (ZEND_NUM_ARGS() == 3) {
if (COL_IS_NULL) {
COL = NPOINTS;
NPOINTS = zend_hash_num_elements(Z_ARRVAL_P(POINTS));
if (NPOINTS % 2 != 0) {
Expand Down Expand Up @@ -3733,7 +3737,7 @@ PHP_FUNCTION(imageaffine)
int i, nelems;
zval *zval_affine_elem = NULL;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oa|a", &IM, gd_image_ce, &z_affine, &z_rect) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oa|a!", &IM, gd_image_ce, &z_affine, &z_rect) == FAILURE) {
RETURN_THROWS();
}

Expand Down Expand Up @@ -3856,7 +3860,7 @@ PHP_FUNCTION(imageaffinematrixget)
double angle;

if (!options) {
zend_argument_type_error(2, "must be of type int|double when using rotate or shear");
zend_argument_type_error(2, "must be of type int|float when using rotate or shear");
RETURN_THROWS();
}

Expand Down Expand Up @@ -4003,26 +4007,29 @@ PHP_FUNCTION(imageresolution)
{
zval *IM;
gdImagePtr im;
zend_long res_x = GD_RESOLUTION, res_y = GD_RESOLUTION;
zend_long res_x, res_y;
zend_bool res_x_is_null = 1, res_y_is_null = 1;

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

im = php_gd_libgdimageptr_from_zval_p(IM);

switch (ZEND_NUM_ARGS()) {
case 3:
gdImageSetResolution(im, res_x, res_y);
RETURN_TRUE;
case 2:
gdImageSetResolution(im, res_x, res_x);
RETURN_TRUE;
default:
array_init(return_value);
add_next_index_long(return_value, gdImageResolutionX(im));
add_next_index_long(return_value, gdImageResolutionY(im));
if (!res_x_is_null && !res_y_is_null) {
gdImageSetResolution(im, res_x, res_y);
RETURN_TRUE;
} else if (!res_x_is_null && res_y_is_null) {
gdImageSetResolution(im, res_x, res_x);
RETURN_TRUE;
} else if (res_x_is_null && !res_y_is_null) {
gdImageSetResolution(im, res_y, res_y);
RETURN_TRUE;
}

array_init(return_value);
add_next_index_long(return_value, gdImageResolutionX(im));
add_next_index_long(return_value, gdImageResolutionY(im));
}
/* }}} */

Expand Down Expand Up @@ -4151,24 +4158,26 @@ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type,
zval *imgind;
zend_long quality = -1, basefilter = -1;
gdImagePtr im;
int argc = ZEND_NUM_ARGS();
int i;
gdIOCtx *ctx = NULL;
zval *to_zval = NULL;

if (image_type == PHP_GDIMG_TYPE_PNG) {
if (zend_parse_parameters(argc, "O|z!ll", &imgind, gd_image_ce, &to_zval, &quality, &basefilter) == FAILURE) {
if (image_type == PHP_GDIMG_TYPE_GIF) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z", &imgind, gd_image_ce, &to_zval) == FAILURE) {
RETURN_THROWS();
}
} else if (image_type == PHP_GDIMG_TYPE_PNG) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!ll", &imgind, gd_image_ce, &to_zval, &quality, &basefilter) == FAILURE) {
RETURN_THROWS();
}
} else {
if (zend_parse_parameters(argc, "O|z!l", &imgind, gd_image_ce, &to_zval, &quality) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|z!l", &imgind, gd_image_ce, &to_zval, &quality) == FAILURE) {
RETURN_THROWS();
}
}

im = php_gd_libgdimageptr_from_zval_p(imgind);

if (argc > 1 && to_zval != NULL) {
if (to_zval != NULL) {
ctx = create_stream_context_from_zval(to_zval);
if (!ctx) {
RETURN_FALSE;
Expand All @@ -4190,16 +4199,6 @@ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type,
case PHP_GDIMG_TYPE_PNG:
(*func_p)(im, ctx, (int) quality, (int) basefilter);
break;
case PHP_GDIMG_TYPE_WBM:
if (argc < 3) {
for (i=0; i < gdImageColorsTotal(im); i++) {
if (!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) break;
}
quality = (int) i;
}

(*func_p)(im, (int) quality, ctx);
break;
case PHP_GDIMG_TYPE_GIF:
(*func_p)(im, ctx);
break;
Expand Down
43 changes: 25 additions & 18 deletions ext/gd/gd.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function gd_info(): array {}

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

function imagesetstyle($im, array $styles): bool {}
function imagesetstyle(GdImage $im, array $styles): bool {}

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

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

function imagepalettetotruecolor(GdImage $im): bool {}

function imagecolormatch($im1, $im2): bool {}
function imagecolormatch(GdImage $im1, GdImage $im2): bool {}

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

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

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

function imagesettile(GdImage $im, $tile): bool {}
function imagesettile(GdImage $im, GdImage $tile): bool {}

function imagesetbrush(GdImage $im, $brush): bool {}
function imagesetbrush(GdImage $im, GdImage $brush): bool {}

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

Expand Down Expand Up @@ -100,30 +100,36 @@ function imagecreatefrombmp(string $filename): GdImage|false {}
function imagecreatefromtga(string $filename): GdImage|false {}
#endif

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

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

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

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

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

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

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

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

#ifdef HAVE_GD_BMP
function imagebmp(GdImage $im, $to = null, int $compressed = 1): bool {}
/** @param resource|string|null $to */
function imagebmp(GdImage $im, $to = null, bool $compressed = true): bool {}
#endif

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

function imagecolorstotal(GdImage $im): int {}

function imagecolortransparent(GdImage $im, int $col = UNKNOWN): ?int {}
function imagecolortransparent(GdImage $im, ?int $col = null): ?int {}

function imageinterlace(GdImage $im, int $interlace = UNKNOWN): ?int {}
function imageinterlace(GdImage $im, ?int $interlace = null): ?int {}

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

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

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

function imagefontwidth(int $font): int {}

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

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

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

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

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

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

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

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