Skip to content

Commit 186a6a9

Browse files
committed
Clear last error in more places
Avoid leaking the last error between persistent and per-request phases.
1 parent 695396f commit 186a6a9

File tree

1 file changed

+23
-28
lines changed

1 file changed

+23
-28
lines changed

main/main.c

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,17 @@ PHPAPI void php_html_puts(const char *str, size_t size)
11881188
}
11891189
/* }}} */
11901190

1191+
static void clear_last_error() {
1192+
if (PG(last_error_message)) {
1193+
zend_string_release(PG(last_error_message));
1194+
PG(last_error_message) = NULL;
1195+
}
1196+
if (PG(last_error_file)) {
1197+
free(PG(last_error_file));
1198+
PG(last_error_file) = NULL;
1199+
}
1200+
}
1201+
11911202
/* {{{ php_error_cb
11921203
extended error handling function */
11931204
static ZEND_COLD void php_error_cb(int orig_type, const char *error_filename, const uint32_t error_lineno, zend_string *message)
@@ -1243,16 +1254,7 @@ static ZEND_COLD void php_error_cb(int orig_type, const char *error_filename, co
12431254

12441255
/* store the error if it has changed */
12451256
if (display) {
1246-
if (PG(last_error_message)) {
1247-
zend_string *s = PG(last_error_message);
1248-
PG(last_error_message) = NULL;
1249-
zend_string_release(s);
1250-
}
1251-
if (PG(last_error_file)) {
1252-
char *s = PG(last_error_file);
1253-
PG(last_error_file) = NULL;
1254-
free(s);
1255-
}
1257+
clear_last_error();
12561258
if (!error_filename) {
12571259
error_filename = "Unknown";
12581260
}
@@ -1587,14 +1589,7 @@ static zval *php_get_configuration_directive_for_zend(zend_string *name)
15871589
*/
15881590
static void php_free_request_globals(void)
15891591
{
1590-
if (PG(last_error_message)) {
1591-
zend_string_release(PG(last_error_message));
1592-
PG(last_error_message) = NULL;
1593-
}
1594-
if (PG(last_error_file)) {
1595-
free(PG(last_error_file));
1596-
PG(last_error_file) = NULL;
1597-
}
1592+
clear_last_error();
15981593
if (PG(php_sys_temp_dir)) {
15991594
efree(PG(php_sys_temp_dir));
16001595
PG(php_sys_temp_dir) = NULL;
@@ -1876,12 +1871,12 @@ void php_request_shutdown(void *dummy)
18761871
}
18771872
} zend_end_try();
18781873

1879-
/* 9. free request-bound globals */
1880-
php_free_request_globals();
1881-
1882-
/* 10. Shutdown scanner/executor/compiler and restore ini entries */
1874+
/* 9. Shutdown scanner/executor/compiler and restore ini entries */
18831875
zend_deactivate();
18841876

1877+
/* 10. free request-bound globals */
1878+
php_free_request_globals();
1879+
18851880
/* 11. Call all extensions post-RSHUTDOWN functions */
18861881
zend_try {
18871882
zend_post_deactivate_modules();
@@ -1953,12 +1948,10 @@ static void core_globals_ctor(php_core_globals *core_globals)
19531948
*/
19541949
static void core_globals_dtor(php_core_globals *core_globals)
19551950
{
1956-
if (core_globals->last_error_message) {
1957-
zend_string_release(core_globals->last_error_message);
1958-
}
1959-
if (core_globals->last_error_file) {
1960-
free(core_globals->last_error_file);
1961-
}
1951+
/* These should have been freed earlier. */
1952+
ZEND_ASSERT(!core_globals->last_error_message);
1953+
ZEND_ASSERT(!core_globals->last_error_file);
1954+
19621955
if (core_globals->disable_functions) {
19631956
free(core_globals->disable_functions);
19641957
}
@@ -2391,6 +2384,8 @@ int php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_mod
23912384
sapi_deactivate();
23922385
module_startup = 0;
23932386

2387+
/* Don't leak errors from startup into the per-request phase. */
2388+
clear_last_error();
23942389
shutdown_memory_manager(1, 0);
23952390
virtual_cwd_activate();
23962391

0 commit comments

Comments
 (0)