1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

ext/standard: Deprecate passing string which are not one byte long to ord() (#19440)

RFC: https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_passing_string_which_are_not_one_byte_long_to_ord

Co-authored-by: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
This commit is contained in:
Gina Peter Banyard
2025-09-14 11:42:59 +01:00
committed by GitHub
parent dd27cedddb
commit 93676a0425
9 changed files with 38 additions and 12 deletions

View File

@@ -4,7 +4,7 @@ String interning during constants substitution
opcache.enable_cli=0
--FILE--
<?php
define ("A", "." . ord(26) . ".");
define ("A", "." . ord(2) . ".");
eval("class A {const a = A;}");
var_dump(A::a);
?>

View File

@@ -4186,14 +4186,18 @@ static zend_result zend_compile_func_chr(znode *result, const zend_ast_list *arg
}
/* }}} */
static zend_result zend_compile_func_ord(znode *result, zend_ast_list *args) /* {{{ */
static zend_result zend_compile_func_ord(znode *result, const zend_ast_list *args) /* {{{ */
{
if (args->children == 1 &&
args->child[0]->kind == ZEND_AST_ZVAL &&
Z_TYPE_P(zend_ast_get_zval(args->child[0])) == IS_STRING) {
zval *str;
if (
args->children == 1
&& args->child[0]->kind == ZEND_AST_ZVAL
&& (str = zend_ast_get_zval(args->child[0]))
&& Z_TYPE_P(str) == IS_STRING
&& Z_STRLEN_P(str) == 1
) {
result->op_type = IS_CONST;
ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(zend_ast_get_zval(args->child[0]))[0]);
ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(str)[0]);
return SUCCESS;
} else {
return FAILURE;

View File

@@ -241,7 +241,7 @@ readGB18030_2022_ConversionTable(__DIR__ . '/data/GB18030-2022MappingTableBMP.tx
findInvalidChars($toUnicode, $invalid, $truncated);
function notFourByteCode($gb) {
return ((ord($gb) < 0x81 || ord($gb) > 0x84) && (ord($gb) < 0x90 || ord($gb) > 0xE3)) ||
return ((ord($gb[0]) < 0x81 || ord($gb[0]) > 0x84) && (ord($gb[0]) < 0x90 || ord($gb[0]) > 0xE3)) ||
(strlen($gb) > 1 && (ord($gb[1]) < 0x30 || ord($gb[1]) > 0x39));
}

View File

@@ -240,7 +240,7 @@ $gb18030_BMP_Mappings = [
findInvalidChars($toUnicode, $invalid, $truncated);
function notFourByteCode($gb) {
return ((ord($gb) < 0x81 || ord($gb) > 0x84) && (ord($gb) < 0x90 || ord($gb) > 0xE3)) ||
return ((ord($gb[0]) < 0x81 || ord($gb[0]) > 0x84) && (ord($gb[0]) < 0x90 || ord($gb[0]) > 0xE3)) ||
(strlen($gb) > 1 && (ord($gb[1]) < 0x30 || ord($gb[1]) > 0x39));
}

View File

@@ -2655,6 +2655,15 @@ PHP_FUNCTION(ord)
Z_PARAM_STR(str)
ZEND_PARSE_PARAMETERS_END();
if (UNEXPECTED(ZSTR_LEN(str) != 1)) {
if (ZSTR_LEN(str) == 0) {
php_error_docref(NULL, E_DEPRECATED,
"Providing an empty string is deprecated");
} else {
php_error_docref(NULL, E_DEPRECATED,
"Providing a string that is not one byte long is deprecated. Use ord($str[0]) instead");
}
}
RETURN_LONG((unsigned char) ZSTR_VAL(str)[0]);
}
/* }}} */

View File

@@ -3,7 +3,7 @@ Test get_html_translation_table() function : basic functionality - HTML 5/Window
--FILE--
<?php
function so($a,$b) { return ord($a) - ord($b); }
function so($a,$b) { return ord($a[0]) - ord($b[0]); }
echo "*** Testing get_html_translation_table() : basic functionality - HTML 5/Windows-1251 ***\n";

View File

@@ -15,7 +15,6 @@ var_dump(ord("@"));
var_dump(ord("\n"));
var_dump(ord("\x0A"));
var_dump(ord("\xFF"));
var_dump(ord("Hello"));
// Make sure all valid ascii chars round trip
for ($i = 0; $i < 255; $i++) {
@@ -37,4 +36,3 @@ int(64)
int(10)
int(10)
int(255)
int(72)

View File

@@ -0,0 +1,15 @@
--TEST--
ord() with values not one byte long
--FILE--
<?php
var_dump(ord(""));
var_dump(ord("Hello"));
?>
--EXPECTF--
Deprecated: ord(): Providing an empty string is deprecated in %s on line 3
int(0)
Deprecated: ord(): Providing a string that is not one byte long is deprecated. Use ord($str[0]) instead in %s on line 4
int(72)