Skip to content

Commit d558365

Browse files
committed
ext/standard: Deprecate passing string which are not one byte long to ord()
RFC: https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_passing_string_which_are_not_one_byte_long_to_ord
1 parent f041c13 commit d558365

File tree

7 files changed

+38
-10
lines changed

7 files changed

+38
-10
lines changed

Zend/tests/constants/class_constants_005.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ String interning during constants substitution
44
opcache.enable_cli=0
55
--FILE--
66
<?php
7-
define ("A", "." . ord(26) . ".");
7+
define ("A", "." . ord(2) . ".");
88
eval("class A {const a = A;}");
99
var_dump(A::a);
1010
?>

Zend/tests/nullsafe_operator/013.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Deprecated: chr(): Passing null to parameter #1 ($codepoint) of type int is depr
5353
string(1) "%s"
5454

5555
Deprecated: ord(): Passing null to parameter #1 ($character) of type string is deprecated in %s on line %d
56+
57+
Deprecated: ord(): Providing an empty string is deprecated in %s on line %d
5658
int(0)
5759
string(98) "call_user_func_array(): Argument #1 ($callback) must be a valid callback, no array or string given"
5860
string(77) "call_user_func_array(): Argument #2 ($args) must be of type array, null given"

Zend/zend_compile.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4167,14 +4167,18 @@ static zend_result zend_compile_func_chr(znode *result, const zend_ast_list *arg
41674167
}
41684168
/* }}} */
41694169

4170-
static zend_result zend_compile_func_ord(znode *result, zend_ast_list *args) /* {{{ */
4170+
static zend_result zend_compile_func_ord(znode *result, const zend_ast_list *args) /* {{{ */
41714171
{
4172-
if (args->children == 1 &&
4173-
args->child[0]->kind == ZEND_AST_ZVAL &&
4174-
Z_TYPE_P(zend_ast_get_zval(args->child[0])) == IS_STRING) {
4175-
4172+
zval *str;
4173+
if (
4174+
args->children == 1
4175+
&& args->child[0]->kind == ZEND_AST_ZVAL
4176+
&& (str = zend_ast_get_zval(args->child[0]))
4177+
&& Z_TYPE_P(str) == IS_STRING
4178+
&& Z_STRLEN_P(str) == 1
4179+
) {
41764180
result->op_type = IS_CONST;
4177-
ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(zend_ast_get_zval(args->child[0]))[0]);
4181+
ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(str)[0]);
41784182
return SUCCESS;
41794183
} else {
41804184
return FAILURE;

ext/standard/string.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2651,6 +2651,15 @@ PHP_FUNCTION(ord)
26512651
Z_PARAM_STR(str)
26522652
ZEND_PARSE_PARAMETERS_END();
26532653

2654+
if (UNEXPECTED(ZSTR_LEN(str) != 1)) {
2655+
if (ZSTR_LEN(str) == 0) {
2656+
php_error_docref(NULL, E_DEPRECATED,
2657+
"Providing an empty string is deprecated");
2658+
} else {
2659+
php_error_docref(NULL, E_DEPRECATED,
2660+
"Providing a string which is not one byte long is deprecated, use ord($str[0]) instead");
2661+
}
2662+
}
26542663
RETURN_LONG((unsigned char) ZSTR_VAL(str)[0]);
26552664
}
26562665
/* }}} */

ext/standard/tests/strings/get_html_translation_table_basic6.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Test get_html_translation_table() function : basic functionality - HTML 5/Window
33
--FILE--
44
<?php
55

6-
function so($a,$b) { return ord($a) - ord($b); }
6+
function so($a,$b) { return ord($a[0]) - ord($b[0]); }
77

88
echo "*** Testing get_html_translation_table() : basic functionality - HTML 5/Windows-1251 ***\n";
99

ext/standard/tests/strings/ord_basic.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ var_dump(ord("@"));
1515
var_dump(ord("\n"));
1616
var_dump(ord("\x0A"));
1717
var_dump(ord("\xFF"));
18-
var_dump(ord("Hello"));
1918

2019
// Make sure all valid ascii chars round trip
2120
for ($i = 0; $i < 255; $i++) {
@@ -37,4 +36,3 @@ int(64)
3736
int(10)
3837
int(10)
3938
int(255)
40-
int(72)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
ord() with values not one byte long
3+
--FILE--
4+
<?php
5+
6+
var_dump(ord(""));
7+
var_dump(ord("Hello"));
8+
9+
?>
10+
--EXPECTF--
11+
Deprecated: ord(): Providing an empty string is deprecated in %s on line 3
12+
int(0)
13+
14+
Deprecated: ord(): Providing a string which is not one byte long is deprecated, use ord($str[0]) instead in %s on line 4
15+
int(72)

0 commit comments

Comments
 (0)