Skip to content

Commit 788a696

Browse files
committed
Change warning type for well formed numeric float strings used as string offset
1 parent 1bdd095 commit 788a696

File tree

6 files changed

+74
-9
lines changed

6 files changed

+74
-9
lines changed

Zend/tests/numeric_strings/string_offset.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ echo "Done\n";
3030
--EXPECTF--
3131
string(1) "l"
3232

33-
Warning: Illegal string offset "7.5" in %s on line 6
33+
Warning: String offset cast occurred in %s on line 6
3434
string(1) "l"
3535
string(1) "l"
3636

37-
Warning: Illegal string offset " 7.5" in %s on line 8
37+
Warning: String offset cast occurred in %s on line 8
3838
string(1) "l"
3939
string(1) "l"
4040

41-
Warning: Illegal string offset " 7.5 " in %s on line 10
41+
Warning: String offset cast occurred in %s on line 10
4242
string(1) "l"
4343
string(1) "l"
4444

45-
Warning: Illegal string offset "7.5 " in %s on line 12
45+
Warning: String offset cast occurred in %s on line 12
4646
string(1) "l"
4747

4848
Warning: Illegal string offset "7str" in %s on line 13

Zend/tests/offset_string.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ string(1) "S"
5454
Illegal offset type
5555
string(1) "c"
5656

57-
Warning: Illegal string offset "14.5" in %s on line %d
57+
Warning: String offset cast occurred in %s on line %d
5858
string(1) "o"
5959

6060
Warning: Illegal string offset "15 and then some" in %s on line %d

Zend/zend_execute.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,11 @@ static zend_never_inline zend_long zend_check_string_offset(zval *dim, int type
13401340
return offset;
13411341
}
13421342
if (IS_DOUBLE == numeric_string_type) {
1343-
zend_error(E_WARNING, "Illegal string offset \"%s\"", Z_STRVAL_P(dim));
1343+
if (0 == is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), NULL, NULL, false)) {
1344+
zend_error(E_WARNING, "Illegal string offset \"%s\"", Z_STRVAL_P(dim));
1345+
} else {
1346+
zend_error(E_WARNING, "String offset cast occurred");
1347+
}
13441348
break;
13451349
}
13461350
zend_illegal_offset();
@@ -2336,7 +2340,12 @@ static zend_always_inline void zend_fetch_dimension_address_read(zval *result, z
23362340
goto out;
23372341
}
23382342
if (IS_DOUBLE == numeric_string_type) {
2339-
zend_error(E_WARNING, "Illegal string offset \"%s\"", Z_STRVAL_P(dim));
2343+
if (type != BP_VAR_IS &&
2344+
0 == is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), NULL, NULL, false)) {
2345+
zend_error(E_WARNING, "Illegal string offset \"%s\"", Z_STRVAL_P(dim));
2346+
} else if (type != BP_VAR_IS) {
2347+
zend_error(E_WARNING, "String offset cast occurred");
2348+
}
23402349
break;
23412350
}
23422351
if (type == BP_VAR_IS) {

ext/opcache/jit/zend_jit_helpers.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,11 @@ static void ZEND_FASTCALL zend_jit_fetch_dim_str_r_helper(zval *container, zval
689689
goto out;
690690
}
691691
if (IS_DOUBLE == numeric_string_type) {
692-
zend_error(E_WARNING, "Illegal string offset \"%s\"", Z_STRVAL_P(dim));
692+
if (0 == is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), NULL, NULL, false)) {
693+
zend_error(E_WARNING, "Illegal string offset \"%s\"", Z_STRVAL_P(dim));
694+
} else {
695+
zend_error(E_WARNING, "String offset cast occurred");
696+
}
693697
break;
694698
}
695699
zend_type_error("Illegal offset type");
@@ -848,7 +852,11 @@ static zend_never_inline zend_long zend_check_string_offset(zval *dim, int type)
848852
return offset;
849853
}
850854
if (IS_DOUBLE == numeric_string_type) {
851-
zend_error(E_WARNING, "Illegal string offset \"%s\"", Z_STRVAL_P(dim));
855+
if (0 == is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), NULL, NULL, false)) {
856+
zend_error(E_WARNING, "Illegal string offset \"%s\"", Z_STRVAL_P(dim));
857+
} else {
858+
zend_error(E_WARNING, "String offset cast occurred");
859+
}
852860
break;
853861
}
854862
zend_type_error("Illegal offset type");

ext/opcache/tests/jit/fetch_dim_r_003.phpt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ function foo() {
3636
$x = "2";
3737
$y = "x";
3838
var_dump($a[$x . $y]);
39+
try {
40+
var_dump($a["5.5"]);
41+
} catch (\TypeError $e) {
42+
echo $e->getMessage() . \PHP_EOL;
43+
}
44+
$x = "5.";
45+
$y = "5";
46+
try {
47+
var_dump($a[$x . $y]);
48+
} catch (\TypeError $e) {
49+
echo $e->getMessage() . \PHP_EOL;
50+
}
51+
var_dump($a["5.5cx"]);
52+
$x = "5.5";
53+
$y = "c";
54+
var_dump($a[$x . $y]);
3955
}
4056
foo();
4157
--EXPECTF--
@@ -63,3 +79,15 @@ string(1) "C"
6379

6480
Warning: Illegal string offset "2x" in %sfetch_dim_r_003.php on line 27
6581
string(1) "C"
82+
83+
Warning: String offset cast occurred in %sfetch_dim_r_003.php on line 29
84+
string(1) "F"
85+
86+
Warning: String offset cast occurred in %sfetch_dim_r_003.php on line 36
87+
string(1) "F"
88+
89+
Warning: Illegal string offset "5.5cx" in %sfetch_dim_r_003.php on line 40
90+
string(1) "F"
91+
92+
Warning: Illegal string offset "5.5c" in %sfetch_dim_r_003.php on line 43
93+
string(1) "F"

ext/opcache/tests/jit/fetch_dim_r_004.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ foo("2x");
3434
$x=2;
3535
$y="x";
3636
foo($x.$y);
37+
foo("5.5");
38+
$x = "5.";
39+
$y = "5";
40+
foo($x.$y);
41+
foo("5.5c");
42+
$x = "5.5";
43+
$y = "c";
44+
foo($x.$y);
3745
--EXPECTF--
3846
string(1) "A"
3947
string(1) "C"
@@ -59,3 +67,15 @@ string(1) "C"
5967

6068
Warning: Illegal string offset "2x" in %sfetch_dim_r_004.php on line 5
6169
string(1) "C"
70+
71+
Warning: String offset cast occurred in %sfetch_dim_r_004.php on line 5
72+
string(1) "F"
73+
74+
Warning: String offset cast occurred in %sfetch_dim_r_004.php on line 5
75+
string(1) "F"
76+
77+
Warning: Illegal string offset "5.5c" in %sfetch_dim_r_004.php on line 5
78+
string(1) "F"
79+
80+
Warning: Illegal string offset "5.5c" in %sfetch_dim_r_004.php on line 5
81+
string(1) "F"

0 commit comments

Comments
 (0)