Skip to content
Open
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
43 changes: 43 additions & 0 deletions Zend/tests/ignore_repeated_errors_clear_last.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
Test ignore_repeated_errors with error_clear_last() and error_get_last()
--INI--
ignore_repeated_errors=1
error_reporting=-1
html_errors=0
--FILE--
<?php
function warningA() {
trigger_error('A', E_USER_WARNING);
}
function noticeN() {
trigger_error('N', E_USER_NOTICE);
}

warningA();
error_clear_last();
warningA();
var_dump(error_get_last()['message']);

error_reporting(E_ALL & ~E_USER_DEPRECATED);
warningA();
trigger_error('D', E_USER_DEPRECATED);
warningA();
var_dump(error_get_last()['message']);

error_clear_last();
warningA();
noticeN();
warningA();
?>
--EXPECTF--
Warning: A in %s on line %d

Warning: A in %s on line %d
string(1) "A"
string(1) "A"

Warning: A in %s on line %d

Notice: N in %s on line %d

Warning: A in %s on line %d
23 changes: 23 additions & 0 deletions Zend/tests/ignore_repeated_errors_interleaved.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
Test ignore_repeated_errors with interleaved error types
--INI--
ignore_repeated_errors=1
error_reporting=-1
--FILE--
<?php
for ($i = 0; $i < 3; $i++) {
trim($missing);
}
?>
--EXPECTF--
Warning: Undefined variable $missing in %s on line %d

Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in %s on line %d

Warning: Undefined variable $missing in %s on line %d

Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in %s on line %d

Warning: Undefined variable $missing in %s on line %d

Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in %s on line %d
54 changes: 54 additions & 0 deletions Zend/tests/ignore_repeated_source_last_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
--TEST--
Repeated errors preserve error_get_last() when ignoring their source
--INI--
ignore_repeated_errors=1
ignore_repeated_source=1
error_reporting=-1
html_errors=0
--FILE--
<?php
trigger_error('S', E_USER_WARNING);
$last = error_get_last();
trigger_error('S', E_USER_NOTICE);
var_dump(error_get_last() === $last);

/* A different file must also be ignored, even with the same line number. */
eval(str_repeat("\n", $last['line'] - 1) . 'trigger_error("S", E_USER_NOTICE);');
var_dump(error_get_last() === $last);

/* A filtered error still updates error_get_last(). */
error_reporting(E_ALL & ~E_USER_DEPRECATED);
trigger_error('D', E_USER_DEPRECATED);
var_dump(error_get_last()['message']);
trigger_error('S', E_USER_WARNING);
var_dump(error_get_last()['type'] === E_USER_WARNING);
var_dump(error_get_last()['message']);
$last = error_get_last();
trigger_error('S', E_USER_NOTICE);
var_dump(error_get_last() === $last);

error_clear_last();
var_dump(error_get_last());
trigger_error('S', E_USER_NOTICE);
var_dump(error_get_last()['type'] === E_USER_NOTICE);

/* With source comparison enabled, a different line is a new error. */
ini_set('ignore_repeated_source', '0');
trigger_error('S', E_USER_WARNING);
var_dump(error_get_last()['type'] === E_USER_WARNING);
?>
--EXPECTF--
Warning: S in %s on line %d
bool(true)
bool(true)
string(1) "D"
bool(true)
string(1) "S"
bool(true)
NULL

Notice: S in %s on line %d
bool(true)

Warning: S in %s on line %d
bool(true)
15 changes: 3 additions & 12 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1471,18 +1471,9 @@ PHP_FUNCTION(error_clear_last)
{
ZEND_PARSE_PARAMETERS_NONE();

if (PG(last_error_message)) {
PG(last_error_type) = 0;
PG(last_error_lineno) = 0;

zend_string_release(PG(last_error_message));
PG(last_error_message) = NULL;

if (PG(last_error_file)) {
zend_string_release(PG(last_error_file));
PG(last_error_file) = NULL;
}
}
PG(last_error_type) = 0;
PG(last_error_lineno) = 0;
php_clear_last_error(true);

zval_ptr_dtor(&EG(last_fatal_error_backtrace));
ZVAL_UNDEF(&EG(last_fatal_error_backtrace));
Expand Down
53 changes: 38 additions & 15 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ PHPAPI void php_html_puts(const char *str, size_t size)
}
/* }}} */

static void clear_last_error(void) {
PHPAPI void php_clear_last_error(bool clear_repeated_errors) {
if (PG(last_error_message)) {
zend_string_release(PG(last_error_message));
PG(last_error_message) = NULL;
Expand All @@ -1295,6 +1295,14 @@ static void clear_last_error(void) {
zend_string_release(PG(last_error_file));
PG(last_error_file) = NULL;
}
if (clear_repeated_errors) {
if (PG(repeated_error_message)) {
zend_string_release(PG(repeated_error_message));
PG(repeated_error_message) = NULL;
zend_string_release(PG(repeated_error_file));
PG(repeated_error_file) = NULL;
}
}
}

#if ZEND_DEBUG
Expand Down Expand Up @@ -1329,15 +1337,14 @@ static ZEND_COLD void php_error_cb(int orig_type, zend_string *error_filename, c
bool display;
int type = orig_type & E_ALL;
zend_string *backtrace = ZSTR_EMPTY_ALLOC();
zend_string *repeated_error_file = error_filename ? error_filename : ZSTR_KNOWN(ZEND_STR_UNKNOWN_CAPITALIZED);

/* check for repeated errors to be ignored */
if (PG(ignore_repeated_errors) && PG(last_error_message)) {
/* no check for PG(last_error_file) is needed since it cannot
* be NULL if PG(last_error_message) is not NULL */
if (!zend_string_equals(PG(last_error_message), message)
if (PG(ignore_repeated_errors) && PG(repeated_error_message)) {
if (!zend_string_equals(PG(repeated_error_message), message)
|| (!PG(ignore_repeated_source)
&& ((PG(last_error_lineno) != error_lineno)
|| !zend_string_equals(PG(last_error_file), error_filename)))) {
&& ((PG(repeated_error_lineno) != error_lineno)
|| !zend_string_equals(PG(repeated_error_file), repeated_error_file)))) {
display = 1;
} else {
display = 0;
Expand Down Expand Up @@ -1372,18 +1379,34 @@ static ZEND_COLD void php_error_cb(int orig_type, zend_string *error_filename, c
backtrace = zend_trace_to_string(Z_ARRVAL(EG(last_fatal_error_backtrace)), /* include_main */ true);
}

/* store the error if it has changed */
if (display) {
clear_last_error();
/* A suppressed repeat may replace an intervening filtered error, but must
* use the same source comparison as repeat suppression. */
if (display || !PG(last_error_message)
|| !zend_string_equals(PG(last_error_message), message)
|| (!PG(ignore_repeated_source)
&& (PG(last_error_lineno) != error_lineno
|| !zend_string_equals(PG(last_error_file), repeated_error_file)))) {
php_clear_last_error(false);
if (!error_filename) {
error_filename = ZSTR_KNOWN(ZEND_STR_UNKNOWN_CAPITALIZED);
error_filename = repeated_error_file;
}
PG(last_error_type) = type;
PG(last_error_message) = zend_string_copy(message);
PG(last_error_file) = zend_string_copy(error_filename);
PG(last_error_lineno) = error_lineno;
}

if ((!PG(ignore_repeated_errors) || display)
&& ((EG(error_reporting) & type) || (type & E_CORE))) {
if (PG(repeated_error_message)) {
zend_string_release(PG(repeated_error_message));
zend_string_release(PG(repeated_error_file));
}
PG(repeated_error_lineno) = error_lineno;
PG(repeated_error_message) = zend_string_copy(message);
PG(repeated_error_file) = zend_string_copy(repeated_error_file);
}

/* Discarding the output buffers frees every handler on the stack, which is
* not safe while one of them is running */
if (zend_alloc_in_memory_limit_error_reporting()
Expand Down Expand Up @@ -1708,7 +1731,7 @@ static zval *php_get_configuration_directive_for_zend(zend_string *name)
/* {{{ php_free_request_globals */
static void php_free_request_globals(void)
{
clear_last_error();
php_clear_last_error(true);
if (PG(php_sys_temp_dir)) {
efree(PG(php_sys_temp_dir));
PG(php_sys_temp_dir) = NULL;
Expand Down Expand Up @@ -2039,7 +2062,7 @@ void php_request_shutdown(void *dummy)
/* 16. Free Willy (here be crashes) */
zend_arena_destroy(CG(arena));
zend_interned_strings_deactivate();
clear_last_error();
php_clear_last_error(true);
zend_try {
shutdown_memory_manager(CG(unclean_shutdown) || !report_memleaks, 0);
} zend_end_try();
Expand Down Expand Up @@ -2456,7 +2479,7 @@ zend_result php_module_startup(sapi_module_struct *sf, zend_module_entry *additi
module_startup = false;

/* Don't leak errors from startup into the per-request phase. */
clear_last_error();
php_clear_last_error(true);
shutdown_memory_manager(1, 0);
virtual_cwd_activate();

Expand Down Expand Up @@ -2519,7 +2542,7 @@ void php_module_shutdown(void)

/* close down the ini config */
php_shutdown_config();
clear_last_error();
php_clear_last_error(true);

#ifndef ZTS
zend_ini_shutdown();
Expand Down
3 changes: 3 additions & 0 deletions main/php_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ struct _php_core_globals {
uint32_t last_error_lineno;
zend_string *last_error_message;
zend_string *last_error_file;
uint32_t repeated_error_lineno;
zend_string *repeated_error_message;
zend_string *repeated_error_file;

char *php_sys_temp_dir;

Expand Down
1 change: 1 addition & 0 deletions main/php_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ PHPAPI char *php_get_version(sapi_module_struct *sapi_module);
PHPAPI void php_print_version(sapi_module_struct *sapi_module);

PHPAPI void php_child_init(void);
PHPAPI void php_clear_last_error(bool clear_repeated_errors);
PHPAPI zend_result php_request_startup(void);
PHPAPI void php_request_shutdown(void *dummy);
PHPAPI zend_result php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_module);
Expand Down
Loading