Skip to content

Commit 2f9013f

Browse files
committed
use a actual fix for testing
1 parent d8b08cb commit 2f9013f

10 files changed

Lines changed: 128 additions & 166 deletions

File tree

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ PHP NEWS
3535
wrong argument in error messages. (Weilin Du)
3636
. Fixed imageaffinematrixget() to enforce the documented array|float type
3737
for the $options parameter. (Weilin Du)
38+
. Fixed imageaffinematrixget() accepting non-convertible "x" and "y" entries
39+
for translate and scale matrices. (Weilin Du)
3840

3941
- Intl:
4042
. Fixed grapheme_strrev() treating UBRK_DONE as a byte index and leaving

UPGRADING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ PHP 8.6 UPGRADE NOTES
5454
. imageaffinematrixget() now enforces the documented array|float type for the
5555
$options parameter, including the corresponding weak and strict typing
5656
behavior.
57+
. The "x" and "y" entries accepted by imageaffinematrixget() for translate
58+
and scale matrices now throw a TypeError when they cannot be converted to
59+
float, instead of coercing arbitrary values.
5760

5861
- GMP:
5962
. GMP power and shift operators now throw a ValueError when GMP right operands

ext/gd/gd.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3844,6 +3844,18 @@ static bool php_gd_zval_try_get_c_int(zval *tmp, const char *field, int *res) {
38443844
return true;
38453845
}
38463846

3847+
static bool php_gd_zval_try_get_double(const zval *value, const char *field, double *res) {
3848+
bool failed;
3849+
*res = zval_try_get_double(value, &failed);
3850+
if (UNEXPECTED(failed)) {
3851+
if (!EG(exception)) {
3852+
zend_argument_type_error(2, "\"%s\" key must be of type float, %s given", field, zend_zval_type_name(value));
3853+
}
3854+
return false;
3855+
}
3856+
return true;
3857+
}
3858+
38473859
/* {{{ Crop an image using the given coordinates and size, x, y, width and height. */
38483860
PHP_FUNCTION(imagecrop)
38493861
{
@@ -4190,14 +4202,18 @@ PHP_FUNCTION(imageaffinematrixget)
41904202
}
41914203

41924204
if ((tmp = zend_hash_str_find(options, "x", sizeof("x") - 1)) != NULL) {
4193-
x = zval_get_double(tmp);
4205+
if (!php_gd_zval_try_get_double(tmp, "x", &x)) {
4206+
RETURN_THROWS();
4207+
}
41944208
} else {
41954209
zend_argument_value_error(2, "must have an \"x\" key");
41964210
RETURN_THROWS();
41974211
}
41984212

41994213
if ((tmp = zend_hash_str_find(options, "y", sizeof("y") - 1)) != NULL) {
4200-
y = zval_get_double(tmp);
4214+
if (!php_gd_zval_try_get_double(tmp, "y", &y)) {
4215+
RETURN_THROWS();
4216+
}
42014217
} else {
42024218
zend_argument_value_error(2, "must have a \"y\" key");
42034219
RETURN_THROWS();
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
--TEST--
2+
imageaffinematrixget() translate option conversions
3+
--EXTENSIONS--
4+
gd
5+
--FILE--
6+
<?php
7+
8+
$resource = fopen(__FILE__, 'r');
9+
$values = [
10+
'null' => null,
11+
'false' => false,
12+
'true' => true,
13+
'int' => 42,
14+
'float' => 42.5,
15+
'numeric integer string' => '42',
16+
'numeric float string' => '42.5',
17+
'numeric scientific string' => '1e3',
18+
'non-numeric string' => 'not numeric',
19+
'array' => [],
20+
'object' => new stdClass(),
21+
'resource' => $resource,
22+
];
23+
24+
foreach ($values as $name => $value) {
25+
echo "$name:\n";
26+
try {
27+
$matrix = imageaffinematrixget(IMG_AFFINE_TRANSLATE, ['x' => $value, 'y' => 0]);
28+
var_dump($matrix[4]);
29+
} catch (Throwable $e) {
30+
echo $e::class, ': ', $e->getMessage(), "\n";
31+
}
32+
}
33+
34+
echo "references:\n";
35+
$x = 1.5;
36+
$y = 2.5;
37+
$matrix = imageaffinematrixget(IMG_AFFINE_TRANSLATE, ['x' => &$x, 'y' => &$y]);
38+
var_dump($matrix[4], $matrix[5]);
39+
40+
echo "invalid y:\n";
41+
try {
42+
imageaffinematrixget(IMG_AFFINE_TRANSLATE, ['x' => 0, 'y' => []]);
43+
} catch (Throwable $e) {
44+
echo $e::class, ': ', $e->getMessage(), "\n";
45+
}
46+
47+
echo "trailing data:\n";
48+
$matrix = imageaffinematrixget(IMG_AFFINE_TRANSLATE, ['x' => '42 with trailing data', 'y' => 0]);
49+
var_dump($matrix[4]);
50+
51+
echo "warning converted to exception:\n";
52+
set_error_handler(static function (int $errno, string $errstr): never {
53+
throw new Exception($errstr);
54+
});
55+
try {
56+
imageaffinematrixget(IMG_AFFINE_TRANSLATE, ['x' => '42 with trailing data', 'y' => 0]);
57+
} catch (Throwable $e) {
58+
echo $e::class, ': ', $e->getMessage(), "\n";
59+
}
60+
61+
fclose($resource);
62+
63+
?>
64+
--EXPECTF--
65+
null:
66+
float(0)
67+
false:
68+
float(0)
69+
true:
70+
float(1)
71+
int:
72+
float(42)
73+
float:
74+
float(42.5)
75+
numeric integer string:
76+
float(42)
77+
numeric float string:
78+
float(42.5)
79+
numeric scientific string:
80+
float(1000)
81+
non-numeric string:
82+
TypeError: imageaffinematrixget(): Argument #2 ($options) "x" key must be of type float, string given
83+
array:
84+
TypeError: imageaffinematrixget(): Argument #2 ($options) "x" key must be of type float, array given
85+
object:
86+
TypeError: imageaffinematrixget(): Argument #2 ($options) "x" key must be of type float, stdClass given
87+
resource:
88+
TypeError: imageaffinematrixget(): Argument #2 ($options) "x" key must be of type float, resource given
89+
references:
90+
float(1.5)
91+
float(2.5)
92+
invalid y:
93+
TypeError: imageaffinematrixget(): Argument #2 ($options) "y" key must be of type float, array given
94+
trailing data:
95+
96+
Warning: A non-numeric value encountered in %s on line %d
97+
float(42)
98+
warning converted to exception:
99+
Exception: A non-numeric value encountered

ext/zend_test/test.c

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,25 +1138,6 @@ static ZEND_FUNCTION(zend_test_refcount)
11381138
RETURN_LONG(Z_REFCOUNT_P(value));
11391139
}
11401140

1141-
static ZEND_FUNCTION(zend_test_zval_try_get_double)
1142-
{
1143-
zval *value;
1144-
1145-
ZEND_PARSE_PARAMETERS_START(1, 1)
1146-
Z_PARAM_ZVAL(value)
1147-
ZEND_PARSE_PARAMETERS_END();
1148-
1149-
bool failed;
1150-
double result = zval_try_get_double(value, &failed);
1151-
if (UNEXPECTED(EG(exception))) {
1152-
RETURN_THROWS();
1153-
}
1154-
1155-
array_init(return_value);
1156-
add_assoc_double(return_value, "value", result);
1157-
add_assoc_bool(return_value, "failed", failed);
1158-
}
1159-
11601141
static ZEND_FUNCTION(zend_get_unit_enum)
11611142
{
11621143
ZEND_PARSE_PARAMETERS_NONE();

ext/zend_test/test.stub.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,6 @@ function zend_test_call_with_consumed_args(callable $cb, array $args, int $consu
406406

407407
function zend_test_refcount(mixed $value): int {}
408408

409-
function zend_test_zval_try_get_double(mixed $value): array {}
410-
411409
function zend_test_zend_ini_parse_quantity(string $str): int {}
412410
function zend_test_zend_ini_parse_uquantity(string $str): int {}
413411

ext/zend_test/test_arginfo.h

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/zend_test/test_decl.h

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/zend_test/test_legacy_arginfo.h

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)