1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

Unify headers already sent/session already started error handler (#16451)

* Unify headers already sent errors

Now whenever we need to check where headers were already sent in
ext/session, we call a single location that prints where, keeping it
consistent output wise.

* Unify session aready started errors

Similar to the one for headers.

* Also change session active checks too

This usually go hand in hand with the headers already sent checks, but
is in a separate commit because of the amount of tests it changes.
This commit is contained in:
Calvin Buckley
2024-10-17 13:13:56 -03:00
committed by GitHub
parent 39fa9cfd1a
commit 84d6cb8cf0
20 changed files with 89 additions and 101 deletions

View File

@@ -83,13 +83,13 @@ zend_class_entry *php_session_update_timestamp_iface_entry;
#define SESSION_CHECK_ACTIVE_STATE \
if (PS(session_status) == php_session_active) { \
php_error_docref(NULL, E_WARNING, "Session ini settings cannot be changed when a session is active"); \
php_session_session_already_started_error(E_WARNING, "Session ini settings cannot be changed when a session is active"); \
return FAILURE; \
}
#define SESSION_CHECK_OUTPUT_STATE \
if (SG(headers_sent) && stage != ZEND_INI_STAGE_DEACTIVATE) { \
php_error_docref(NULL, E_WARNING, "Session ini settings cannot be changed after headers have already been sent"); \
php_session_headers_already_sent_error(E_WARNING, "Session ini settings cannot be changed after headers have already been sent"); \
return FAILURE; \
}
@@ -121,6 +121,29 @@ static inline void php_rinit_session_globals(void) /* {{{ */
}
/* }}} */
static inline void php_session_headers_already_sent_error(int severity, const char *message) { /* {{{ */
const char *output_start_filename = php_output_get_start_filename();
int output_start_lineno = php_output_get_start_lineno();
if (output_start_filename != NULL) {
php_error_docref(NULL, severity, "%s (sent from %s on line %d)", message, output_start_filename, output_start_lineno);
} else {
php_error_docref(NULL, severity, "%s", message);
}
}
/* }}} */
static inline void php_session_session_already_started_error(int severity, const char *message) { /* {{{ */
if (PS(session_started_filename) != NULL) {
php_error_docref(NULL, severity, "%s (started from %s on line %"PRIu32")", message, ZSTR_VAL(PS(session_started_filename)), PS(session_started_lineno));
} else if (PS(auto_start)) {
/* This option can't be changed at runtime, so we can assume it's because of this */
php_error_docref(NULL, severity, "%s (session started automatically)", message);
} else {
php_error_docref(NULL, severity, "%s", message);
}
}
/* }}} */
static inline void php_session_cleanup_filename(void) /* {{{ */
{
if (PS(session_started_filename)) {
@@ -1327,15 +1350,8 @@ static int php_session_cache_limiter(void) /* {{{ */
if (PS(session_status) != php_session_active) return -1;
if (SG(headers_sent)) {
const char *output_start_filename = php_output_get_start_filename();
int output_start_lineno = php_output_get_start_lineno();
php_session_abort();
if (output_start_filename) {
php_error_docref(NULL, E_WARNING, "Session cache limiter cannot be sent after headers have already been sent (output started at %s:%d)", output_start_filename, output_start_lineno);
} else {
php_error_docref(NULL, E_WARNING, "Session cache limiter cannot be sent after headers have already been sent");
}
php_session_headers_already_sent_error(E_WARNING, "Session cache limiter cannot be sent after headers have already been sent");
return -2;
}
@@ -1404,14 +1420,7 @@ static zend_result php_session_send_cookie(void) /* {{{ */
zend_string *e_id;
if (SG(headers_sent)) {
const char *output_start_filename = php_output_get_start_filename();
int output_start_lineno = php_output_get_start_lineno();
if (output_start_filename) {
php_error_docref(NULL, E_WARNING, "Session cookie cannot be sent after headers have already been sent (output started at %s:%d)", output_start_filename, output_start_lineno);
} else {
php_error_docref(NULL, E_WARNING, "Session cookie cannot be sent after headers have already been sent");
}
php_session_headers_already_sent_error(E_WARNING, "Session cookie cannot be sent after headers have already been sent");
return FAILURE;
}
@@ -1606,14 +1615,7 @@ PHPAPI zend_result php_session_start(void) /* {{{ */
switch (PS(session_status)) {
case php_session_active:
if (PS(session_started_filename)) {
php_error(E_NOTICE, "Ignoring session_start() because a session has already been started (started from %s on line %"PRIu32")", ZSTR_VAL(PS(session_started_filename)), PS(session_started_lineno));
} else if (PS(auto_start)) {
/* This option can't be changed at runtime, so we can assume it's because of this */
php_error(E_NOTICE, "Ignoring session_start() because a session has already been started automatically");
} else {
php_error(E_NOTICE, "Ignoring session_start() because a session has already been started");
}
php_session_session_already_started_error(E_NOTICE, "Ignoring session_start() because a session has already been started");
return FAILURE;
break;
@@ -1796,12 +1798,12 @@ PHP_FUNCTION(session_set_cookie_params)
}
if (PS(session_status) == php_session_active) {
php_error_docref(NULL, E_WARNING, "Session cookie parameters cannot be changed when a session is active");
php_session_session_already_started_error(E_WARNING, "Session cookie parameters cannot be changed when a session is active");
RETURN_FALSE;
}
if (SG(headers_sent)) {
php_error_docref(NULL, E_WARNING, "Session cookie parameters cannot be changed after headers have already been sent");
php_session_headers_already_sent_error(E_WARNING, "Session cookie parameters cannot be changed after headers have already been sent");
RETURN_FALSE;
}
@@ -1968,12 +1970,12 @@ PHP_FUNCTION(session_name)
}
if (name && PS(session_status) == php_session_active) {
php_error_docref(NULL, E_WARNING, "Session name cannot be changed when a session is active");
php_session_session_already_started_error(E_WARNING, "Session name cannot be changed when a session is active");
RETURN_FALSE;
}
if (name && SG(headers_sent)) {
php_error_docref(NULL, E_WARNING, "Session name cannot be changed after headers have already been sent");
php_session_headers_already_sent_error(E_WARNING, "Session name cannot be changed after headers have already been sent");
RETURN_FALSE;
}
@@ -1998,12 +2000,12 @@ PHP_FUNCTION(session_module_name)
}
if (name && PS(session_status) == php_session_active) {
php_error_docref(NULL, E_WARNING, "Session save handler module cannot be changed when a session is active");
php_session_session_already_started_error(E_WARNING, "Session save handler module cannot be changed when a session is active");
RETURN_FALSE;
}
if (name && SG(headers_sent)) {
php_error_docref(NULL, E_WARNING, "Session save handler module cannot be changed after headers have already been sent");
php_session_headers_already_sent_error(E_WARNING, "Session save handler module cannot be changed after headers have already been sent");
RETURN_FALSE;
}
@@ -2039,12 +2041,12 @@ PHP_FUNCTION(session_module_name)
static bool can_session_handler_be_changed(void) {
if (PS(session_status) == php_session_active) {
php_error_docref(NULL, E_WARNING, "Session save handler cannot be changed when a session is active");
php_session_session_already_started_error(E_WARNING, "Session save handler cannot be changed when a session is active");
return false;
}
if (SG(headers_sent)) {
php_error_docref(NULL, E_WARNING, "Session save handler cannot be changed after headers have already been sent");
php_session_headers_already_sent_error(E_WARNING, "Session save handler cannot be changed after headers have already been sent");
return false;
}
@@ -2279,12 +2281,12 @@ PHP_FUNCTION(session_save_path)
}
if (name && PS(session_status) == php_session_active) {
php_error_docref(NULL, E_WARNING, "Session save path cannot be changed when a session is active");
php_session_session_already_started_error(E_WARNING, "Session save path cannot be changed when a session is active");
RETURN_FALSE;
}
if (name && SG(headers_sent)) {
php_error_docref(NULL, E_WARNING, "Session save path cannot be changed after headers have already been sent");
php_session_headers_already_sent_error(E_WARNING, "Session save path cannot be changed after headers have already been sent");
RETURN_FALSE;
}
@@ -2308,12 +2310,12 @@ PHP_FUNCTION(session_id)
}
if (name && PS(session_status) == php_session_active) {
php_error_docref(NULL, E_WARNING, "Session ID cannot be changed when a session is active");
php_session_session_already_started_error(E_WARNING, "Session ID cannot be changed when a session is active");
RETURN_FALSE;
}
if (name && PS(use_cookies) && SG(headers_sent)) {
php_error_docref(NULL, E_WARNING, "Session ID cannot be changed after headers have already been sent");
php_session_headers_already_sent_error(E_WARNING, "Session ID cannot be changed after headers have already been sent");
RETURN_FALSE;
}
@@ -2350,12 +2352,12 @@ PHP_FUNCTION(session_regenerate_id)
}
if (PS(session_status) != php_session_active) {
php_error_docref(NULL, E_WARNING, "Session ID cannot be regenerated when there is no active session");
php_session_session_already_started_error(E_WARNING, "Session ID cannot be regenerated when there is no active session");
RETURN_FALSE;
}
if (SG(headers_sent)) {
php_error_docref(NULL, E_WARNING, "Session ID cannot be regenerated after headers have already been sent");
php_session_headers_already_sent_error(E_WARNING, "Session ID cannot be regenerated after headers have already been sent");
RETURN_FALSE;
}
@@ -2521,12 +2523,12 @@ PHP_FUNCTION(session_cache_limiter)
}
if (limiter && PS(session_status) == php_session_active) {
php_error_docref(NULL, E_WARNING, "Session cache limiter cannot be changed when a session is active");
php_session_session_already_started_error(E_WARNING, "Session cache limiter cannot be changed when a session is active");
RETURN_FALSE;
}
if (limiter && SG(headers_sent)) {
php_error_docref(NULL, E_WARNING, "Session cache limiter cannot be changed after headers have already been sent");
php_session_headers_already_sent_error(E_WARNING, "Session cache limiter cannot be changed after headers have already been sent");
RETURN_FALSE;
}
@@ -2551,12 +2553,12 @@ PHP_FUNCTION(session_cache_expire)
}
if (!expires_is_null && PS(session_status) == php_session_active) {
php_error_docref(NULL, E_WARNING, "Session cache expiration cannot be changed when a session is active");
php_session_session_already_started_error(E_WARNING, "Session cache expiration cannot be changed when a session is active");
RETURN_LONG(PS(cache_expire));
}
if (!expires_is_null && SG(headers_sent)) {
php_error_docref(NULL, E_WARNING, "Session cache expiration cannot be changed after headers have already been sent");
php_session_headers_already_sent_error(E_WARNING, "Session cache expiration cannot be changed after headers have already been sent");
RETURN_FALSE;
}
@@ -2637,14 +2639,7 @@ PHP_FUNCTION(session_start)
}
if (PS(session_status) == php_session_active) {
if (PS(session_started_filename)) {
php_error_docref(NULL, E_NOTICE, "Ignoring session_start() because a session is already active (started from %s on line %"PRIu32")", ZSTR_VAL(PS(session_started_filename)), PS(session_started_lineno));
} else if (PS(auto_start)) {
/* This option can't be changed at runtime, so we can assume it's because of this */
php_error_docref(NULL, E_NOTICE, "Ignoring session_start() because a session is already automatically active");
} else {
php_error_docref(NULL, E_NOTICE, "Ignoring session_start() because a session is already active");
}
php_session_session_already_started_error(E_NOTICE, "Ignoring session_start() because a session is already active");
RETURN_TRUE;
}
@@ -2654,14 +2649,7 @@ PHP_FUNCTION(session_start)
* module is unable to rewrite output.
*/
if (PS(use_cookies) && SG(headers_sent)) {
/* It's the header sent to blame, not the session in this case */
const char *output_start_filename = php_output_get_start_filename();
int output_start_lineno = php_output_get_start_lineno();
if (output_start_filename != NULL) {
php_error_docref(NULL, E_WARNING, "Session cannot be started after headers have already been sent (sent from %s on line %d)", output_start_filename, output_start_lineno);
} else {
php_error_docref(NULL, E_WARNING, "Session cannot be started after headers have already been sent");
}
php_session_headers_already_sent_error(E_WARNING, "Session cannot be started after headers have already been sent");
RETURN_FALSE;
}

View File

@@ -35,8 +35,8 @@ session_destroy();
--EXPECTF--
<a href="/link">
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
<a href="/link">
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
<a href="/link">

View File

@@ -22,7 +22,7 @@ try {
--EXPECTF--
bool(true)
Warning: session_module_name(): Session save handler module cannot be changed when a session is active in %s on line %d
Warning: session_module_name(): Session save handler module cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(true)
session_module_name(): Argument #1 ($module) cannot be "user"
===DONE===

View File

@@ -30,7 +30,7 @@ string(7) "nocache"
bool(true)
string(7) "nocache"
Warning: session_cache_limiter(): Session cache limiter cannot be changed when a session is active in %s on line %d
Warning: session_cache_limiter(): Session cache limiter cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
string(7) "nocache"
bool(true)

View File

@@ -29,7 +29,7 @@ string(7) "nocache"
bool(true)
string(7) "nocache"
Warning: session_cache_limiter(): Session cache limiter cannot be changed when a session is active in %s on line %d
Warning: session_cache_limiter(): Session cache limiter cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
string(7) "nocache"
bool(true)

View File

@@ -28,7 +28,7 @@ string(7) "nocache"
bool(true)
string(7) "nocache"
Warning: session_cache_limiter(): Session cache limiter cannot be changed when a session is active in %s on line %d
Warning: session_cache_limiter(): Session cache limiter cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
string(7) "nocache"
bool(true)

View File

@@ -31,7 +31,7 @@ string(4) "test"
string(10) "1234567890"
bool(true)
Warning: session_id(): Session ID cannot be changed when a session is active in %s on line %d
Warning: session_id(): Session ID cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
bool(true)
string(0) ""

View File

@@ -118,67 +118,67 @@ string(1) "4"
string(1) "1"
string(15) "session started"
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
bool(false)
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
Warning: ini_set(): Session ini settings cannot be changed when a session is active in %s on line %d
Warning: ini_set(): Session ini settings cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
Done

View File

@@ -40,7 +40,7 @@ string(%d) "%stests"
bool(true)
string(%d) "%stests"
Warning: session_save_path(): Session save path cannot be changed when a session is active in %s on line %d
Warning: session_save_path(): Session save path cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
string(%d) "%stests"
bool(true)

View File

@@ -25,7 +25,7 @@ ob_end_flush();
bool(true)
bool(true)
Warning: session_set_cookie_params(): Session cookie parameters cannot be changed when a session is active in %s on line %d
Warning: session_set_cookie_params(): Session cookie parameters cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
bool(true)
bool(true)

View File

@@ -38,7 +38,7 @@ string(4) "3600"
bool(true)
string(4) "3600"
Warning: session_set_cookie_params(): Session cookie parameters cannot be changed when a session is active in %s on line %d
Warning: session_set_cookie_params(): Session cookie parameters cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
string(4) "3600"
bool(true)

View File

@@ -36,7 +36,7 @@ string(4) "/foo"
bool(true)
string(4) "/foo"
Warning: session_set_cookie_params(): Session cookie parameters cannot be changed when a session is active in %s on line %d
Warning: session_set_cookie_params(): Session cookie parameters cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
string(4) "/foo"
bool(true)

View File

@@ -36,7 +36,7 @@ string(4) "blah"
bool(true)
string(4) "blah"
Warning: session_set_cookie_params(): Session cookie parameters cannot be changed when a session is active in %s on line %d
Warning: session_set_cookie_params(): Session cookie parameters cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
string(4) "blah"
bool(true)

View File

@@ -36,7 +36,7 @@ string(1) "0"
bool(true)
string(1) "0"
Warning: session_set_cookie_params(): Session cookie parameters cannot be changed when a session is active in %s on line %d
Warning: session_set_cookie_params(): Session cookie parameters cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
string(1) "0"
bool(true)

View File

@@ -36,7 +36,7 @@ string(1) "0"
bool(true)
string(1) "0"
Warning: session_set_cookie_params(): Session cookie parameters cannot be changed when a session is active in %s on line %d
Warning: session_set_cookie_params(): Session cookie parameters cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
string(1) "0"
bool(true)

View File

@@ -36,7 +36,7 @@ string(7) "nothing"
bool(true)
string(7) "nothing"
Warning: session_set_cookie_params(): Session cookie parameters cannot be changed when a session is active in %s on line %d
Warning: session_set_cookie_params(): Session cookie parameters cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
string(7) "nothing"
bool(true)

View File

@@ -26,7 +26,7 @@ ob_end_flush();
*** Testing session_start() : variation ***
string(%d) "%s"
Notice: session_start(): Ignoring session_start() because a session is already automatically active in %s on line %d
Notice: session_start(): Ignoring session_start() because a session is already active (session started automatically) in %s on line %d
bool(true)
string(%d) "%s"
bool(true)

View File

@@ -33,6 +33,6 @@ var_dump($initHandler, $setHandler);
--EXPECTF--
Deprecated: session_set_save_handler(): Providing individual callbacks instead of an object implementing SessionHandlerInterface is deprecated in %s on line %d
Warning: session_set_save_handler(): Session save handler cannot be changed after headers have already been sent in %s on line %d
Warning: session_set_save_handler(): Session save handler cannot be changed after headers have already been sent (sent from %s on line %d) in %s on line %d
string(8) "whatever"
string(8) "whatever"

View File

@@ -30,6 +30,6 @@ bool(true)
Deprecated: session_set_save_handler(): Providing individual callbacks instead of an object implementing SessionHandlerInterface is deprecated in %s on line %d
Warning: session_set_save_handler(): Session save handler cannot be changed when a session is active in %s on line %d
Warning: session_set_save_handler(): Session save handler cannot be changed when a session is active (started from %s on line %d) in %s on line %d
bool(false)
bool(true)

View File

@@ -29,10 +29,10 @@ rmdir($path);
*** Testing session_set_save_handler() : variation ***
int(2)
Warning: session_save_path(): Session save path cannot be changed when a session is active in %s on line %d
Warning: session_save_path(): Session save path cannot be changed when a session is active (session started automatically) in %s on line %d
Deprecated: session_set_save_handler(): Providing individual callbacks instead of an object implementing SessionHandlerInterface is deprecated in %s on line %d
Warning: session_set_save_handler(): Session save handler cannot be changed when a session is active in %s on line %d
Warning: session_set_save_handler(): Session save handler cannot be changed when a session is active (session started automatically) in %s on line %d
bool(false)
bool(true)