com php-src: Fix severity issues with callbacks, start work porting ZEND_STRLEN opcode to work with strict mode, more refactoring to come:
Zend/tests/typehints/intern al_function_strict_mode.phpt Zend/zend_API.c Ze nd/zend_API.h Zend/zend_vm_def.h Zend/zend_vm _execute.h
| From: | Dmitry Stogov | Date: | Thu, 19 Mar 2015 16:26:34 +0000 |
| Subject: | com php-src: Fix severity issues with callbacks, start work porting ZEND_STRLEN opcode to work with strict mode, more refactoring to come: Zend/tests/typehints/intern al_function_strict_mode.phpt Zend/zend_API.c Ze nd/zend_API.h Zend/zend_vm_def.h Zend/zend_vm _execute.h |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-85702@lists.php.net to get a copy of this message | ||
Commit: 0ef80ac351960cd0fa474f960638abc8921841d8
Author: Anthony Ferrara <ircmaxell@gmail.com> Thu, 19 Mar 2015 12:26:34 -0400
Parents: 78d2399493f4f1128cd98a9b60fb32f078f5f609
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=0ef80ac351960cd0fa474f960638abc8921841d8
Log:
Fix severity issues with callbacks, start work porting ZEND_STRLEN opcode to work with strict mode,
more refactoring to come
Changed paths:
A Zend/tests/typehints/internal_function_strict_mode.phpt
M Zend/zend_API.c
M Zend/zend_API.h
M Zend/zend_vm_def.h
M Zend/zend_vm_execute.h
Diff:
diff --git a/Zend/tests/typehints/internal_function_strict_mode.phpt
b/Zend/tests/typehints/internal_function_strict_mode.phpt
new file mode 100644
index 0000000..3e5629a
--- /dev/null
+++ b/Zend/tests/typehints/internal_function_strict_mode.phpt
@@ -0,0 +1,35 @@
+--TEST--
+Scalar type hint - internal function strict mode
+--FILE--
+<?php
+declare(strict_types=1);
+
+echo "*** Trying Ord With Integer" . PHP_EOL;
+try {
+ var_dump(ord(1));
+} catch (TypeException $e) {
+ echo "*** Caught " .. $e->getMessage() . PHP_EOL;
+}
+
+echo "*** Trying Array Map With Invalid Callback" . PHP_EOL;
+try {
+ array_map([null, "bar"], []);
+} catch (TypeException $e) {
+ echo "*** Caught " . $e->getMessage() . PHP_EOL;
+}
+
+echo "*** Trying Strlen With Float" . PHP_EOL;
+try {
+ var_dump(strlen(1.5));
+} catch (TypeException $e) {
+ echo "*** Caught " . $e->getMessage() . PHP_EOL;
+}
+
+?>
+--EXPECTF--
+*** Trying Ord With Integer
+*** Caught ord() expects parameter 1 to be string, integer given
+*** Trying Array Map With Invalid Callback
+*** Caught array_map() expects parameter 1 to be a valid callback, first array member is not a
valid class name or object
+*** Trying Strlen With Float
+*** Caught strlen() expects parameter 1 to be string, float given
\ No newline at end of file
diff --git a/Zend/zend_API.c b/Zend/zend_API.c
index 32888f1..f86bb66 100644
--- a/Zend/zend_API.c
+++ b/Zend/zend_API.c
@@ -284,13 +284,18 @@ ZEND_API void zend_wrong_paramer_class_error(int num, char *name, zval *arg,
zen
}
/* }}} */
-ZEND_API void zend_wrong_callback_error(int severity, int num, char *error) /* {{{ */
+ZEND_API void zend_wrong_callback_error(int severity, int num, char *error, zend_bool strict) /*
{{{ */
{
const char *space;
const char *class_name = get_active_class_name(&space);
- zend_error(severity, "%s%s%s() expects parameter %d to be a valid callback, %s",
- class_name, space, get_active_function_name(), num, error);
+ if (severity == E_WARNING) {
+ zend_internal_type_error(strict, "%s%s%s() expects parameter %d to be a valid callback,
%s",
+ class_name, space, get_active_function_name(), num, error);
+ } else {
+ zend_error(severity, "%s%s%s() expects parameter %d to be a valid callback, %s",
+ class_name, space, get_active_function_name(), num, error);
+ }
efree(error);
}
/* }}} */
diff --git a/Zend/zend_API.h b/Zend/zend_API.h
index a1989d1..810e9b8 100644
--- a/Zend/zend_API.h
+++ b/Zend/zend_API.h
@@ -710,7 +710,7 @@ typedef enum _zend_expected_type {
ZEND_API void zend_wrong_paramers_count_error(int num_args, int min_num_args, int max_num_args,
zend_bool strict);
ZEND_API void zend_wrong_paramer_type_error(int num, zend_expected_type expected_type, zval *arg,
zend_bool strict);
ZEND_API void zend_wrong_paramer_class_error(int num, char *name, zval *arg, zend_bool strict);
-ZEND_API void zend_wrong_callback_error(int severity, int num, char *error);
+ZEND_API void zend_wrong_callback_error(int severity, int num, char *error, zend_bool strict);
#define ZPP_ERROR_OK 0
#define ZPP_ERROR_FAILURE 1
@@ -761,7 +761,7 @@ ZEND_API void zend_wrong_callback_error(int severity, int num, char *error);
if (UNEXPECTED(error_code != ZPP_ERROR_OK)) { \
if (!(_flags & ZEND_PARSE_PARAMS_QUIET)) { \
if (error_code == ZPP_ERROR_WRONG_CALLBACK) { \
- zend_wrong_callback_error(_strict ? E_RECOVERABLE_ERROR : E_WARNING, _i, _error); \
+ zend_wrong_callback_error(E_WARNING, _i, _error, _strict); \
} else if (error_code == ZPP_ERROR_WRONG_CLASS) { \
zend_wrong_paramer_class_error(_i, _error, _arg, _strict); \
} else if (error_code == ZPP_ERROR_WRONG_ARG) { \
@@ -863,7 +863,7 @@ ZEND_API void zend_wrong_callback_error(int severity, int num, char *error);
break; \
} \
} else if (UNEXPECTED(_error != NULL)) { \
- zend_wrong_callback_error(E_STRICT, _i, _error); \
+ zend_wrong_callback_error(E_STRICT, _i, _error, _strict); \
}
#define Z_PARAM_FUNC(dest_fci, dest_fcc) \
diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h
index d116c4f..eb9f68e 100644
--- a/Zend/zend_vm_def.h
+++ b/Zend/zend_vm_def.h
@@ -7269,7 +7269,7 @@ ZEND_VM_HANDLER(121, ZEND_STRLEN, CONST|TMPVAR|CV, ANY)
ZEND_VM_C_LABEL(try_strlen):
if (EXPECTED(Z_TYPE_P(value) == IS_STRING)) {
ZVAL_LONG(EX_VAR(opline->result.var), Z_STRLEN_P(value));
- } else {
+ } else if (EXPECTED(0 == EX_USES_STRICT_TYPES())) {
if ((OP1_TYPE & (IS_VAR|IS_CV)) && Z_TYPE_P(value) == IS_REFERENCE) {
value = Z_REFVAL_P(value);
ZEND_VM_C_GOTO(try_strlen);
@@ -7296,6 +7296,8 @@ ZEND_VM_C_LABEL(strlen_error):
zend_error(E_WARNING, "strlen() expects parameter 1 to be string, %s given",
zend_get_type_by_const(Z_TYPE_P(value)));
ZVAL_NULL(EX_VAR(opline->result.var));
}
+ } else {
+ zend_type_error("strlen() expects parameter 1 to be string, %s given",
zend_get_type_by_const(Z_TYPE_P(value)));
}
FREE_OP1();
CHECK_EXCEPTION();
diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h
index d535b41..8eb8bf8 100644
--- a/Zend/zend_vm_execute.h
+++ b/Zend/zend_vm_execute.h
@@ -3815,7 +3815,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL
ZEND_STRLEN_SPEC_CONST_HANDLER(ZEND
try_strlen:
if (EXPECTED(Z_TYPE_P(value) == IS_STRING)) {
ZVAL_LONG(EX_VAR(opline->result.var), Z_STRLEN_P(value));
- } else {
+ } else if (EXPECTED(0 == EX_USES_STRICT_TYPES())) {
if ((IS_CONST & (IS_VAR|IS_CV)) && Z_TYPE_P(value) == IS_REFERENCE) {
value = Z_REFVAL_P(value);
goto try_strlen;
@@ -3842,6 +3842,8 @@ strlen_error:
zend_error(E_WARNING, "strlen() expects parameter 1 to be string, %s given",
zend_get_type_by_const(Z_TYPE_P(value)));
ZVAL_NULL(EX_VAR(opline->result.var));
}
+ } else {
+ zend_type_error("strlen() expects parameter 1 to be string, %s given",
zend_get_type_by_const(Z_TYPE_P(value)));
}
CHECK_EXCEPTION();
@@ -27300,7 +27302,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL
ZEND_STRLEN_SPEC_CV_HANDLER(ZEND_OP
try_strlen:
if (EXPECTED(Z_TYPE_P(value) == IS_STRING)) {
ZVAL_LONG(EX_VAR(opline->result.var), Z_STRLEN_P(value));
- } else {
+ } else if (EXPECTED(0 == EX_USES_STRICT_TYPES())) {
if ((IS_CV & (IS_VAR|IS_CV)) && Z_TYPE_P(value) == IS_REFERENCE) {
value = Z_REFVAL_P(value);
goto try_strlen;
@@ -27327,6 +27329,8 @@ strlen_error:
zend_error(E_WARNING, "strlen() expects parameter 1 to be string, %s given",
zend_get_type_by_const(Z_TYPE_P(value)));
ZVAL_NULL(EX_VAR(opline->result.var));
}
+ } else {
+ zend_type_error("strlen() expects parameter 1 to be string, %s given",
zend_get_type_by_const(Z_TYPE_P(value)));
}
CHECK_EXCEPTION();
@@ -37559,7 +37563,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL
ZEND_STRLEN_SPEC_TMPVAR_HANDLER(ZEN
try_strlen:
if (EXPECTED(Z_TYPE_P(value) == IS_STRING)) {
ZVAL_LONG(EX_VAR(opline->result.var), Z_STRLEN_P(value));
- } else {
+ } else if (EXPECTED(0 == EX_USES_STRICT_TYPES())) {
if (((IS_TMP_VAR|IS_VAR) & (IS_VAR|IS_CV)) && Z_TYPE_P(value) == IS_REFERENCE) {
value = Z_REFVAL_P(value);
goto try_strlen;
@@ -37586,6 +37590,8 @@ strlen_error:
zend_error(E_WARNING, "strlen() expects parameter 1 to be string, %s given",
zend_get_type_by_const(Z_TYPE_P(value)));
ZVAL_NULL(EX_VAR(opline->result.var));
}
+ } else {
+ zend_type_error("strlen() expects parameter 1 to be string, %s given",
zend_get_type_by_const(Z_TYPE_P(value)));
}
zval_ptr_dtor_nogc(free_op1);
CHECK_EXCEPTION();