Skip to content

Commit a302d11

Browse files
committed
Don't silence fatal errors with @
1 parent b65435c commit a302d11

15 files changed

+53
-19
lines changed

Diff for: UPGRADING

+24
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@ PHP 8.0 UPGRADE NOTES
4242
. Any array that has a number n as its first numeric key will use n+1 for
4343
its next implicit key. Even if n is negative.
4444
RFC: https://wiki.php.net/rfc/negative_array_index
45+
. The @ operator will no longer silence fatal errors (E_ERROR, E_CORE_ERROR,
46+
E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR, E_PARSE). Error handlers
47+
that expect error_reporting to be 0 when @ is used, should be adjusted to
48+
use a mask check instead:
49+
50+
// Replace
51+
function my_error_handler($err_no, $err_msg, $filename, $linenum) {
52+
if (error_reporting() == 0)
53+
return; // Silenced
54+
}
55+
// ...
56+
}
57+
58+
// With
59+
function my_error_handler($err_no, $err_msg, $filename, $linenum) {
60+
if (error_reporting() & $err_no)
61+
return; // Silenced
62+
}
63+
// ...
64+
}
65+
66+
Additionally, care should be taken that error messages are not displayed in
67+
production environments, which can result in information leaks. Please
68+
ensure that display_errors=Off is used in conjunction with error logging.
4569

4670
- Date:
4771
. mktime() and gmmktime() now require at least one argument. time() can be

Diff for: Zend/tests/bug34786.phpt

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ function bar() {
1010
echo "bar: ".error_reporting()."\n";
1111
}
1212

13-
error_reporting(1);
13+
error_reporting(E_WARNING);
1414
echo "before: ".error_reporting()."\n";
1515
@foo(1,@bar(),3);
1616
echo "after: ".error_reporting()."\n";
1717
?>
1818
--EXPECT--
19-
before: 1
19+
before: 2
2020
bar: 0
2121
foo: 0
22-
after: 1
22+
after: 2

Diff for: Zend/zend_errors.h

+5
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@
3939
#define E_ALL (E_ERROR | E_WARNING | E_PARSE | E_NOTICE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING | E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE | E_RECOVERABLE_ERROR | E_DEPRECATED | E_USER_DEPRECATED | E_STRICT)
4040
#define E_CORE (E_CORE_ERROR | E_CORE_WARNING)
4141

42+
/* Fatal errors that are ignored by the silence operator */
43+
#define E_FATAL_ERRORS (E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR | E_PARSE)
44+
45+
#define E_HAS_ONLY_FATAL_ERRORS(mask) !((mask) & ~E_FATAL_ERRORS)
46+
4247
#endif /* ZEND_ERRORS_H */

Diff for: Zend/zend_execute.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -3757,7 +3757,8 @@ static void cleanup_live_vars(zend_execute_data *execute_data, uint32_t op_num,
37573757
}
37583758
} else if (kind == ZEND_LIVE_SILENCE) {
37593759
/* restore previous error_reporting value */
3760-
if (!EG(error_reporting) && Z_LVAL_P(var) != 0) {
3760+
if (E_HAS_ONLY_FATAL_ERRORS(EG(error_reporting))
3761+
&& !E_HAS_ONLY_FATAL_ERRORS(Z_LVAL_P(var))) {
37613762
EG(error_reporting) = Z_LVAL_P(var);
37623763
}
37633764
}

Diff for: Zend/zend_vm_def.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -6755,9 +6755,10 @@ ZEND_VM_HANDLER(57, ZEND_BEGIN_SILENCE, ANY, ANY)
67556755

67566756
ZVAL_LONG(EX_VAR(opline->result.var), EG(error_reporting));
67576757

6758-
if (EG(error_reporting)) {
6758+
if (!E_HAS_ONLY_FATAL_ERRORS(EG(error_reporting))) {
67596759
do {
6760-
EG(error_reporting) = 0;
6760+
/* Do not silence fatal errors */
6761+
EG(error_reporting) &= E_FATAL_ERRORS;
67616762
if (!EG(error_reporting_ini_entry)) {
67626763
zval *zv = zend_hash_find_ex(EG(ini_directives), ZSTR_KNOWN(ZEND_STR_ERROR_REPORTING), 1);
67636764
if (zv) {
@@ -6786,7 +6787,8 @@ ZEND_VM_HANDLER(58, ZEND_END_SILENCE, TMP, ANY)
67866787
{
67876788
USE_OPLINE
67886789

6789-
if (!EG(error_reporting) && Z_LVAL_P(EX_VAR(opline->op1.var)) != 0) {
6790+
if (E_HAS_ONLY_FATAL_ERRORS(EG(error_reporting))
6791+
&& !E_HAS_ONLY_FATAL_ERRORS(Z_LVAL_P(EX_VAR(opline->op1.var)))) {
67906792
EG(error_reporting) = Z_LVAL_P(EX_VAR(opline->op1.var));
67916793
}
67926794
ZEND_VM_NEXT_OPCODE();

Diff for: Zend/zend_vm_execute.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -1493,9 +1493,10 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_BEGIN_SILENCE_SPEC_HANDLER(ZEN
14931493

14941494
ZVAL_LONG(EX_VAR(opline->result.var), EG(error_reporting));
14951495

1496-
if (EG(error_reporting)) {
1496+
if (!E_HAS_ONLY_FATAL_ERRORS(EG(error_reporting))) {
14971497
do {
1498-
EG(error_reporting) = 0;
1498+
/* Do not silence fatal errors */
1499+
EG(error_reporting) &= E_FATAL_ERRORS;
14991500
if (!EG(error_reporting_ini_entry)) {
15001501
zval *zv = zend_hash_find_ex(EG(ini_directives), ZSTR_KNOWN(ZEND_STR_ERROR_REPORTING), 1);
15011502
if (zv) {
@@ -19620,7 +19621,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_END_SILENCE_SPEC_TMP_HANDLER(Z
1962019621
{
1962119622
USE_OPLINE
1962219623

19623-
if (!EG(error_reporting) && Z_LVAL_P(EX_VAR(opline->op1.var)) != 0) {
19624+
if (E_HAS_ONLY_FATAL_ERRORS(EG(error_reporting))
19625+
&& !E_HAS_ONLY_FATAL_ERRORS(Z_LVAL_P(EX_VAR(opline->op1.var)))) {
1962419626
EG(error_reporting) = Z_LVAL_P(EX_VAR(opline->op1.var));
1962519627
}
1962619628
ZEND_VM_NEXT_OPCODE();

Diff for: ext/mbstring/tests/mb_substitute_character_variation1.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ echo "*** Testing mb_substitute_character() : usage variation ***\n";
1717

1818
// Define error handler
1919
function test_error_handler($err_no, $err_msg, $filename, $linenum) {
20-
if (error_reporting() != 0) {
20+
if (error_reporting() & $err_no) {
2121
// report non-silenced errors
2222
echo "Error: $err_no - $err_msg, $filename($linenum)\n";
2323
}

Diff for: ext/spl/tests/class_implements_variation1.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ echo "*** Testing class_implements() : variation ***\n";
1313

1414
// Define error handler
1515
function test_error_handler($err_no, $err_msg, $filename, $linenum) {
16-
if (error_reporting() != 0) {
16+
if (error_reporting() & $err_no) {
1717
// report non-silenced errors
1818
echo "Error: $err_no - $err_msg, $filename($linenum)\n";
1919
}

Diff for: ext/spl/tests/class_uses_variation1.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ echo "*** Testing class_uses() : variation ***\n";
1313

1414
// Define error handler
1515
function test_error_handler($err_no, $err_msg, $filename, $linenum) {
16-
if (error_reporting() != 0) {
16+
if (error_reporting() & $err_no) {
1717
// report non-silenced errors
1818
echo "Error: $err_no - $err_msg, $filename($linenum)\n";
1919
}

Diff for: ext/standard/tests/array/array_multisort_variation1.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ echo "*** Testing array_multisort() : usage variation ***\n";
1212

1313
// Define error handler
1414
function test_error_handler($err_no, $err_msg, $filename, $linenum) {
15-
if (error_reporting() != 0) {
15+
if (error_reporting() & $err_no) {
1616
// report non-silenced errors
1717
echo "Error: $err_no - $err_msg, $filename($linenum)\n";
1818
}

Diff for: ext/standard/tests/array/array_multisort_variation2.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ echo "*** Testing array_multisort() : usage variation ***\n";
1212

1313
// Define error handler
1414
function test_error_handler($err_no, $err_msg, $filename, $linenum) {
15-
if (error_reporting() != 0) {
15+
if (error_reporting() & $err_no) {
1616
// report non-silenced errors
1717
echo "Error: $err_no - $err_msg, $filename($linenum)\n";
1818
}

Diff for: ext/standard/tests/array/array_multisort_variation3.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ echo "*** Testing array_multisort() : usage variation ***\n";
1212

1313
// Define error handler
1414
function test_error_handler($err_no, $err_msg, $filename, $linenum) {
15-
if (error_reporting() != 0) {
15+
if (error_reporting() & $err_no) {
1616
// report non-silenced errors
1717
echo "Error: $err_no - $err_msg, $filename($linenum)\n";
1818
}

Diff for: ext/standard/tests/file/file_put_contents_variation2.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ echo "*** Testing file_put_contents() : usage variation ***\n";
1414

1515
// Define error handler
1616
function test_error_handler($err_no, $err_msg, $filename, $linenum) {
17-
if (error_reporting() != 0) {
17+
if (error_reporting() & $err_no) {
1818
// report non-silenced errors
1919
echo "Error: $err_no - $err_msg, $filename($linenum)\n";
2020
}

Diff for: ext/standard/tests/file/file_put_contents_variation3.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ echo "*** Testing file_put_contents() : usage variation ***\n";
1414

1515
// Define error handler
1616
function test_error_handler($err_no, $err_msg, $filename, $linenum) {
17-
if (error_reporting() != 0) {
17+
if (error_reporting() & $err_no) {
1818
// report non-silenced errors
1919
echo "Error: $err_no - $err_msg, $filename($linenum)\n";
2020
}

Diff for: ext/standard/tests/general_functions/intval_variation1.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ echo "*** Testing intval() : usage variation ***\n";
1212

1313
// Define error handler
1414
function test_error_handler($err_no, $err_msg, $filename, $linenum) {
15-
if (error_reporting() != 0) {
15+
if (error_reporting() & $err_no) {
1616
// report non-silenced errors
1717
echo "Error: $err_no - $err_msg, $filename($linenum)\n";
1818
}

0 commit comments

Comments
 (0)