Skip to content

Commit 7006002

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 9659f3e commit 7006002

File tree

7 files changed

+36
-10
lines changed

7 files changed

+36
-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
?>
77 Bytes
Binary file not shown.

Zend/zend_compile.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4186,14 +4186,18 @@ static zend_result zend_compile_func_chr(znode *result, const zend_ast_list *arg
41864186
}
41874187
/* }}} */
41884188

4189-
static zend_result zend_compile_func_ord(znode *result, zend_ast_list *args) /* {{{ */
4189+
static zend_result zend_compile_func_ord(znode *result, const zend_ast_list *args) /* {{{ */
41904190
{
4191-
if (args->children == 1 &&
4192-
args->child[0]->kind == ZEND_AST_ZVAL &&
4193-
Z_TYPE_P(zend_ast_get_zval(args->child[0])) == IS_STRING) {
4194-
4191+
zval *str;
4192+
if (
4193+
args->children == 1
4194+
&& args->child[0]->kind == ZEND_AST_ZVAL
4195+
&& (str = zend_ast_get_zval(args->child[0]))
4196+
&& Z_TYPE_P(str) == IS_STRING
4197+
&& Z_STRLEN_P(str) == 1
4198+
) {
41954199
result->op_type = IS_CONST;
4196-
ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(zend_ast_get_zval(args->child[0]))[0]);
4200+
ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(str)[0]);
41974201
return SUCCESS;
41984202
} else {
41994203
return FAILURE;

ext/standard/string.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,6 +2655,15 @@ PHP_FUNCTION(ord)
26552655
Z_PARAM_STR(str)
26562656
ZEND_PARSE_PARAMETERS_END();
26572657

2658+
if (UNEXPECTED(ZSTR_LEN(str) != 1)) {
2659+
if (ZSTR_LEN(str) == 0) {
2660+
php_error_docref(NULL, E_DEPRECATED,
2661+
"Providing an empty string is deprecated");
2662+
} else {
2663+
php_error_docref(NULL, E_DEPRECATED,
2664+
"Providing a string which is not one byte long is deprecated, use ord($str[0]) instead");
2665+
}
2666+
}
26582667
RETURN_LONG((unsigned char) ZSTR_VAL(str)[0]);
26592668
}
26602669
/* }}} */

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)