Skip to content

ext/gd: imageaffinematrixget() strengthening options type check. #18208

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
37 changes: 32 additions & 5 deletions ext/gd/gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -4138,15 +4138,33 @@ PHP_FUNCTION(imageaffinematrixget)
RETURN_THROWS();
}

if ((tmp = zend_hash_str_find(Z_ARRVAL_P(options), "x", sizeof("x") - 1)) != NULL) {
x = zval_get_double(tmp);
if ((tmp = zend_hash_str_find_deref(Z_ARRVAL_P(options), "x", sizeof("x") - 1)) != NULL) {
switch (Z_TYPE_P(tmp)) {
case IS_LONG:
case IS_DOUBLE:
case IS_STRING:
x = zval_get_double(tmp);
break;
Copy link
Member

Choose a reason for hiding this comment

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

Won't this return 0 for garbage strings like "string" ? Obviously an improvement over the current situation, but still not amazing. Also this probably will fail for GMP objects which might be supported rn?

default:
zend_argument_type_error(2, "must be a float for the \"x\" key, %s given", zend_zval_value_name(tmp));
RETURN_THROWS();
}
} else {
zend_argument_value_error(2, "must have an \"x\" key");
RETURN_THROWS();
}

if ((tmp = zend_hash_str_find(Z_ARRVAL_P(options), "y", sizeof("y") - 1)) != NULL) {
y = zval_get_double(tmp);
if ((tmp = zend_hash_str_find_deref(Z_ARRVAL_P(options), "y", sizeof("y") - 1)) != NULL) {
switch (Z_TYPE_P(tmp)) {
case IS_LONG:
case IS_DOUBLE:
case IS_STRING:
y = zval_get_double(tmp);
break;
default:
zend_argument_type_error(2, "must be a float for the \"y\" key, %s given", zend_zval_value_name(tmp));
RETURN_THROWS();
}
} else {
zend_argument_value_error(2, "must have a \"y\" key");
RETURN_THROWS();
Expand All @@ -4165,7 +4183,16 @@ PHP_FUNCTION(imageaffinematrixget)
case GD_AFFINE_SHEAR_VERTICAL: {
double angle;

angle = zval_get_double(options);
switch (Z_TYPE_P(options)) {
case IS_LONG:
case IS_DOUBLE:
case IS_STRING:
angle = zval_get_double(options);
break;
default:
zend_argument_type_error(2, "must be of type float, %s given", zend_zval_value_name(options));
RETURN_THROWS();
}

if (type == GD_AFFINE_SHEAR_HORIZONTAL) {
res = gdAffineShearHorizontal(affine, angle);
Expand Down
51 changes: 3 additions & 48 deletions ext/gd/tests/bug67248.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,8 @@ for($i=0;$i<7;$i++) {
--EXPECTF--
!! [TypeError] imageaffinematrixget(): Argument #1 ($type) must be of type array when using translate or scale
!! [TypeError] imageaffinematrixget(): Argument #1 ($type) must be of type array when using translate or scale

Warning: Object of class stdClass could not be converted to float in %s on line %d
array(6) {
[0]=>
float(%f)
[1]=>
float(%f)
[2]=>
float(%f)
[3]=>
float(%f)
[4]=>
float(0)
[5]=>
float(0)
}

Warning: Object of class stdClass could not be converted to float in %s on line %d
array(6) {
[0]=>
float(1)
[1]=>
float(0)
[2]=>
float(%f)
[3]=>
float(1)
[4]=>
float(0)
[5]=>
float(0)
}

Warning: Object of class stdClass could not be converted to float in %s on line %d
array(6) {
[0]=>
float(1)
[1]=>
float(%f)
[2]=>
float(0)
[3]=>
float(1)
[4]=>
float(0)
[5]=>
float(0)
}
!! [TypeError] imageaffinematrixget(): Argument #2 ($options) must be of type float, stdClass given
!! [TypeError] imageaffinematrixget(): Argument #2 ($options) must be of type float, stdClass given
!! [TypeError] imageaffinematrixget(): Argument #2 ($options) must be of type float, stdClass given
!! [ValueError] imageaffinematrixget(): Argument #1 ($type) must be a valid element type
!! [ValueError] imageaffinematrixget(): Argument #1 ($type) must be a valid element type
43 changes: 43 additions & 0 deletions ext/gd/tests/imageaffinematrixget_errors.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
imageaffinematrixget
--EXTENSIONS--
gd
--FILE--
<?php
class A{}
$a = 10;
try {
imageaffinematrixget(IMG_AFFINE_ROTATE, new A());
} catch (\TypeError $e) {
echo $e->getMessage(), PHP_EOL;
}
try {
imageaffinematrixget(IMG_AFFINE_SCALE, ["x" => new A(), "y" => 1]);
} catch (\TypeError $e) {
echo $e->getMessage(), PHP_EOL;
}
try {
imageaffinematrixget(IMG_AFFINE_SCALE, ["x" => 10, "y" => new A()]);
} catch (\TypeError $e) {
echo $e->getMessage(), PHP_EOL;
}
var_dump(imageaffinematrixget(IMG_AFFINE_SCALE, ["x" => &$a, "y" => &$a]));
?>
--EXPECT--
imageaffinematrixget(): Argument #2 ($options) must be of type float, A given
imageaffinematrixget(): Argument #2 ($options) must be a float for the "x" key, A given
imageaffinematrixget(): Argument #2 ($options) must be a float for the "y" key, A given
array(6) {
[0]=>
float(10)
[1]=>
float(0)
[2]=>
float(0)
[3]=>
float(10)
[4]=>
float(0)
[5]=>
float(0)
}
Loading