Skip to content
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

[PoC] [WIP] throw_legacy_failure declare statement #4549

Closed
Closed
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
19 changes: 19 additions & 0 deletions Zend/tests/throw_legacy_failure/declare_statement_isolated.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
Test throw_legacy_failure declare statement is isolated to file
--FILE--
<?php
include 'fixture/no_declare.inc';
try {
include 'fixture/declare_on.inc';
} catch (\Throwable $e) {
echo "Error caught\n";
}
include 'fixture/declare_off.inc';
?>
DONE
--EXPECTF--
Warning: array_combine(): Both parameters should have an equal number of elements in %sfixture/no_declare.inc on line %d
Error caught

Warning: array_combine(): Both parameters should have an equal number of elements in %sfixture/declare_off.inc on line %d
DONE
4 changes: 4 additions & 0 deletions Zend/tests/throw_legacy_failure/fixture/declare_off.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
declare(throw_legacy_failure=0);

array_combine(array(1, 2), array(1, 2, 3));
4 changes: 4 additions & 0 deletions Zend/tests/throw_legacy_failure/fixture/declare_on.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
declare(throw_legacy_failure=1);

array_combine(array(1, 2), array(1, 2, 3));
3 changes: 3 additions & 0 deletions Zend/tests/throw_legacy_failure/fixture/no_declare.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

array_combine(array(1, 2), array(1, 2, 3));
20 changes: 20 additions & 0 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -5185,6 +5185,26 @@ void zend_compile_declare(zend_ast *ast) /* {{{ */
CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES;
}

} else if (zend_string_equals_literal_ci(name, "throw_legacy_failure")) {
zval value_zv;

if (FAILURE == zend_declare_is_first_statement(ast)) {
zend_error_noreturn(E_COMPILE_ERROR, "throw_legacy_failure declaration must be "
"the very first statement in the script");
}

if (ast->child[1] != NULL) {
zend_error_noreturn(E_COMPILE_ERROR, "throw_legacy_failure declaration must not use block mode");
}

zend_const_expr_to_zval(&value_zv, value_ast);

if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) {
zend_error_noreturn(E_COMPILE_ERROR, "throw_legacy_failure declaration must have 0 or 1 as its value");
}

CG(throw_legacy_failure) = zval_get_long(&value_zv);

} else {
zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name));
}
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ struct _zend_compiler_globals {
zend_bool multibyte;
zend_bool detect_unicode;
zend_bool encoding_declared;
zend_bool throw_legacy_failure;

zend_ast *ast;
zend_arena *ast_arena;
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -6419,7 +6419,7 @@ PHP_FUNCTION(array_combine)
num_values = zend_hash_num_elements(values);

if (num_keys != num_values) {
php_error_docref(NULL, E_WARNING, "Both parameters should have an equal number of elements");
php_exception_or_error_docref(NULL, E_WARNING, "Both parameters should have an equal number of elements");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's okay to convert this kind of error condition to an Error unconditionally (I've been doing this occasionally). Basically any error condition that indicates a programming error and no reasonable code should handle locally.

For this kind of directive, I think the area to focus on would be things like I/O errors.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, what is the best way to convert these? Using zend_throw_exception(zend_ce_error_exception, message, E_ERROR); directly instead or is there a predefined function/macro for throwing ErrorExceptions?

Copy link
Contributor

@strongholdmedia strongholdmedia Aug 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally think these are not strictly programmer errors, and could save us from an awful lot of boilerplate pre-validation code if e.g. the arrays are populated from some file or even a HTTP request.
But surely, anything catchable is of help.

RETURN_FALSE;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Test array_combine() function : error conditions - with throw_legacy_failure declare statement
--FILE--
<?php

declare(throw_legacy_failure=1);

try {
array_combine(array(1, 2), array(1, 2, 3));
} catch (\ErrorException $e) {
echo "Error caught\n";
}
?>
DONE
--EXPECTF--
Error caught
DONE
21 changes: 21 additions & 0 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,27 @@ PHPAPI ZEND_COLD void php_error_docref2(const char *docref, const char *param1,
}
/* }}} */

/* {{{ php_exception_or_error_docref */
/* Throw an Exception if throw_legacy_failure declare statement is present otherwise fallback to a traditional
* docref error. */
PHPAPI ZEND_COLD void php_exception_or_error_docref(const char *docref, int type, const char *format, ...)
{
va_list args;

va_start(args, format);
if (CG(throw_legacy_failure) == 1) {
char *message = NULL;

zend_vspprintf(&message, 0, format, args);
zend_throw_exception(zend_ce_error_exception, message, E_ERROR);
efree(message);
} else {
php_verror(docref, "", type, format, args);
}
va_end(args);
}
/* }}} */

#ifdef PHP_WIN32
PHPAPI ZEND_COLD void php_win32_docref2_from_error(DWORD error, const char *param1, const char *param2) {
char *buf = php_win32_error_to_msg(error);
Expand Down
3 changes: 3 additions & 0 deletions main/php.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@ PHPAPI ZEND_COLD void php_error_docref1(const char *docref, const char *param1,
PHP_ATTRIBUTE_FORMAT(printf, 4, 5);
PHPAPI ZEND_COLD void php_error_docref2(const char *docref, const char *param1, const char *param2, int type, const char *format, ...)
PHP_ATTRIBUTE_FORMAT(printf, 5, 6);
PHPAPI ZEND_COLD void php_exception_or_error_docref(const char *docref, int type, const char *format, ...)
PHP_ATTRIBUTE_FORMAT(printf, 3, 4);

#ifdef PHP_WIN32
PHPAPI ZEND_COLD void php_win32_docref2_from_error(DWORD error, const char *param1, const char *param2);
#endif
Expand Down