Skip to content

ext/gd: calls with array types check strengthening. - #18005

Closed
devnexen wants to merge 1 commit into
php:masterfrom
devnexen:gd_to_try_get_long
Closed

ext/gd: calls with array types check strengthening.#18005
devnexen wants to merge 1 commit into
php:masterfrom
devnexen:gd_to_try_get_long

Conversation

@devnexen

Copy link
Copy Markdown
Member

No description provided.

@GirgiasGirgias left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like adding a common function php_gd_zval_try_get_c_int() seems like a good idea.

Comment threadext/gd/gd.c Outdated
Comment on lines +662 to +663
}
stylearr[index++] = tmp;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there a possibility of int under/overflow ?

Comment threadext/gd/gd.c Outdated
@@ -3840,28 +3862,48 @@ PHP_FUNCTION(imagecrop)
im = php_gd_libgdimageptr_from_zval_p(IM);

if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "x", sizeof("x") -1)) != NULL) {
rect.x = zval_get_long(tmp);
r = zval_get_long(tmp);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try get long?

Comment threadext/gd/gd.c Outdated
} else {
zend_argument_value_error(2, "must have an \"x\" key");
RETURN_THROWS();
}

if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "y", sizeof("y") - 1)) != NULL) {
rect.y = zval_get_long(tmp);
r = zval_get_long(tmp);
if (ZEND_LONG_EXCEEDS_INT(r)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try get long?

Comment threadext/gd/gd.c Outdated
} else {
zend_argument_value_error(2, "must have a \"y\" key");
RETURN_THROWS();
}

if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "width", sizeof("width") - 1)) != NULL) {
rect.width = zval_get_long(tmp);
r = zval_get_long(tmp);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Comment threadext/gd/gd.c Outdated
} else {
zend_argument_value_error(2, "must have a \"width\" key");
RETURN_THROWS();
}

if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "height", sizeof("height") - 1)) != NULL) {
rect.height = zval_get_long(tmp);
r = zval_get_long(tmp);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto?

@devnexen
devnexenforce-pushed the gd_to_try_get_long branch from bea99f9 to 3233235CompareMarch 8, 2025 21:43
@devnexen
devnexen marked this pull request as ready for review March 8, 2025 21:44
@devnexen
devnexen marked this pull request as draft March 8, 2025 23:01
@devnexen
devnexen marked this pull request as ready for review March 9, 2025 00:14

@GirgiasGirgias left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally makes sense to me, maybe @cmb69 wants to have a look?

Comment threadext/gd/gd.c Outdated
@@ -3822,6 +3848,22 @@ PHP_FUNCTION(imageantialias)
}
/* }}} */

static bool _php_gd_zval_try_get_c_int(zval *tmp, const char *field, int *res) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Symbols starting with _ are reserved in C, so please avoid this.

Suggested change
staticbool_php_gd_zval_try_get_c_int(zval*tmp, constchar*field, int*res) {
staticboolphp_gd_zval_try_get_c_int(zval*tmp, constchar*field, int*res) {

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry just saw your comment now, agreed.

@devnexen
devnexenforce-pushed the gd_to_try_get_long branch 2 times, most recently from 3645a3c to f54c5f3CompareMarch 28, 2025 17:39

@GirgiasGirgias left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry some more comments that I realized

Comment threadext/gd/gd.c Outdated
zend_long tmp = zval_try_get_long(item, &failed);
if (failed) {
efree(stylearr);
zend_argument_value_error(2, "value must be of type int, %s given", zend_zval_type_name(item));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be a type error? And maybe clarify that its the elements of the style array?

Comment threadext/gd/gd.c Outdated
zend_long tmp = zval_try_get_long(color, &failed);
if (failed) {
efree(colors);
zend_argument_value_error(5, "value must be of type int, %s given", zend_zval_type_name(color));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto type error and clarify value of the hash colors array

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address this review comment :)

Comment threadext/gd/tests/gh18005.phpt Outdated
echo $e->getMessage() . PHP_EOL;
}
try {
imagefilter($img, IMG_FILTER_SCATTER, 0, 0, array(new A()));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use [] instead of array() (especially as the prior 2 use the short syntax)

Comment threadext/gd/gd.c
Comment threadext/gd/gd.c Outdated
zend_long tmp = zval_try_get_long(item, &failed);
if (failed) {
efree(stylearr);
zend_argument_type_error(2, "must only have element of type int, %s given", zend_zval_type_name(item));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
zend_argument_type_error(2, "must only have element of type int, %s given", zend_zval_type_name(item));
zend_argument_type_error(2, "must only have elements of type int, %s given", zend_zval_type_name(item));

Comment threadext/gd/gd.c Outdated
}
if (ZEND_LONG_EXCEEDS_INT(tmp)) {
efree(stylearr);
zend_argument_type_error(2, "must have element between %d and %d", INT_MIN, INT_MAX);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wording could be improved here too IMO

Comment threadext/gd/gd.c Outdated
zend_long tmp = zval_try_get_long(color, &failed);
if (failed) {
efree(colors);
zend_argument_value_error(5, "value must be of type int, %s given", zend_zval_type_name(color));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

@Girgias

Copy link
Copy Markdown
Member

Could this be rebased?

@devnexen
devnexenforce-pushed the gd_to_try_get_long branch 2 times, most recently from 82d5bc7 to 0bf1882CompareApril 6, 2026 14:36
Comment threadext/gd/gd.c Outdated
ZVAL_DEREF(tmp);
r = zval_try_get_long(tmp, &failed);
if (failed) {
zend_argument_value_error(2, "\"%s\" key must be of type int, %s given", field, zend_zval_type_name(tmp));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a type error

Comment threadext/gd/tests/gh18005.phpt
Comment threadext/gd/gd.c Outdated
}
if (ZEND_LONG_EXCEEDS_INT(tmp)) {
efree(stylearr);
zend_argument_type_error(2, "elements must be between %d and %d", INT_MIN, INT_MAX);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a value error

@devnexen
devnexenforce-pushed the gd_to_try_get_long branch from 0bf1882 to 59831c6CompareApril 6, 2026 18:16

@GirgiasGirgias left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Next time can you please not rebase when dealing with a PR that can be squashed? It makes checking that the review feedback has been applied somewhat tedious.

@devnexen

Copy link
Copy Markdown
MemberAuthor

sorry because.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@devnexen@Girgias@ndossche