Skip to content

ext/standard: Deprecate passing integers outside the interval [0, 255] to chr() #19441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -4142,17 +4142,19 @@ static zend_result zend_compile_func_defined(znode *result, zend_ast_list *args)
}
/* }}} */

static zend_result zend_compile_func_chr(znode *result, zend_ast_list *args) /* {{{ */
static zend_result zend_compile_func_chr(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_LONG) {

zend_long c = Z_LVAL_P(zend_ast_get_zval(args->child[0])) & 0xff;

zval *zint;
if (
args->children == 1
&& args->child[0]->kind == ZEND_AST_ZVAL
&& (zint = zend_ast_get_zval(args->child[0]))
&& Z_TYPE_P(zint) == IS_LONG
&& Z_LVAL_P(zint) >= 0
&& Z_LVAL_P(zint) <= 255
) {
result->op_type = IS_CONST;
ZVAL_CHAR(&result->u.constant, c);
ZVAL_CHAR(&result->u.constant, Z_LVAL_P(zint));
return SUCCESS;
} else {
return FAILURE;
Expand Down
6 changes: 6 additions & 0 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -2665,6 +2665,12 @@ PHP_FUNCTION(chr)
Z_PARAM_LONG(c)
ZEND_PARSE_PARAMETERS_END();

if (UNEXPECTED(c < 0 || c > 255)) {
php_error_docref(NULL, E_DEPRECATED,
"Providing a value not in-between 0 and 255 is deprecated,"
" this is because a byte value must be in the [0, 255] interval."
" The value used will be constrained using %% 256");
}
c &= 0xff;
RETURN_CHAR(c);
}
Expand Down
15 changes: 15 additions & 0 deletions ext/standard/tests/strings/chr_out_of_range.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
chr() with out of range values
--FILE--
<?php

var_dump("\xFF" == chr(-1));
var_dump("\0" == chr(256));

?>
--EXPECTF--
Deprecated: chr(): Providing a value not in-between 0 and 255 is deprecated, this is because a byte value must be in the [0, 255] interval. The value used will be constrained using % 256 in %s on line 3
bool(true)

Deprecated: chr(): Providing a value not in-between 0 and 255 is deprecated, this is because a byte value must be in the [0, 255] interval. The value used will be constrained using % 256 in %s on line 4
bool(true)
57 changes: 12 additions & 45 deletions ext/standard/tests/strings/chr_variation1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,17 @@ Test chr() function : usage variations - test values for $ascii argument

echo "*** Testing chr() function: with unexpected inputs for 'ascii' argument ***\n";

//defining a class
class sample {
public function __toString() {
return "sample object";
}
}

//getting the resource
$file_handle = fopen(__FILE__, "r");

// array with different values for $input
$inputs = array (

// integer values
/*1*/ 0,
1,
255,
256,

// float values
/*5*/ 10.5,
-20.5,
1.1234e6,

// boolean values
/*11*/ true,
false,
TRUE,
FALSE,
);
$inputs = [
0,
1,
255,
// float values
10.5,
// bool values
true,
false,
];

// loop through with each element of the $inputs array to test chr() function
$count = 1;
Expand All @@ -44,8 +25,6 @@ foreach($inputs as $input) {
$count ++;
}

fclose($file_handle); //closing the file handle

?>
--EXPECTF--
*** Testing chr() function: with unexpected inputs for 'ascii' argument ***
Expand All @@ -56,22 +35,10 @@ string(2) "01"
-- Iteration 3 --
string(2) "ff"
-- Iteration 4 --
string(2) "00"
-- Iteration 5 --

Deprecated: Implicit conversion from float 10.5 to int loses precision in %s on line %d
string(2) "0a"
-- Iteration 6 --

Deprecated: Implicit conversion from float -20.5 to int loses precision in %s on line %d
string(2) "ec"
-- Iteration 7 --
string(2) "48"
-- Iteration 8 --
string(2) "01"
-- Iteration 9 --
string(2) "00"
-- Iteration 10 --
-- Iteration 5 --
string(2) "01"
-- Iteration 11 --
-- Iteration 6 --
string(2) "00"
Loading
Loading