[php-src] PHP-8.4: ext/session: fix GH-21314 (session GC behaviour is different since 8.4)

Jorg Sowa via Gina Peter Banyard <[email protected]>
Newsgroups gmane.comp.php.cvs.general
Message-ID <[email protected]>
Author: Jorg Sowa (jorgsowa)
Committer: Gina Peter Banyard (Girgias)
Date: 2026-07-02T11:49:56+01:00

Commit: https://github.com/php/php-src/commit/fe53521e92eb141a3365df8e98eb41003cdf0887
Raw diff: https://github.com/php/php-src/commit/fe53521e92eb141a3365df8e98eb41003cdf0887.diff

ext/session: fix GH-21314 (session GC behaviour is different since 8.4)

Closes GH-22164

Changed paths:
  M  ext/session/php_session.h
  M  ext/session/session.c


Diff:

diff --git a/ext/session/php_session.h b/ext/session/php_session.h
index dfa7632e5a44..b52cf43dfba3 100644
--- a/ext/session/php_session.h
+++ b/ext/session/php_session.h
@@ -202,6 +202,7 @@ typedef struct _php_ps_globals {
 	bool lazy_write; /* omit session write when it is possible */
 	bool in_save_handler; /* state if session is in save handler or not */
 	bool set_handler;     /* state if session module i setting handler or not */
+	bool random_seeded;
 	zend_string *session_vars; /* serialized original session data */
 } php_ps_globals;
 
diff --git a/ext/session/session.c b/ext/session/session.c
index 70e1673d87f5..ba71d709a536 100644
--- a/ext/session/session.c
+++ b/ext/session/session.c
@@ -394,6 +394,18 @@ static zend_long php_session_gc(bool immediate) /* {{{ */
 	/* GC must be done before reading session data. */
 	if ((PS(mod_data) || PS(mod_user_implemented))) {
 		if (!collect && PS(gc_probability) > 0) {
+			/* Seed lazily on first GC draw per process. */
+			if (UNEXPECTED(!PS(random_seeded))) {
+				php_random_uint128_t seed;
+				if (php_random_bytes_silent(&seed, sizeof(seed)) == FAILURE) {
+					seed = php_random_uint128_constant(
+						php_random_generate_fallback_seed(),
+						php_random_generate_fallback_seed()
+					);
+				}
+				php_random_pcgoneseq128xslrr64_seed128(PS(random).state, seed);
+				PS(random_seeded) = true;
+			}
 			collect = php_random_range(PS(random), 0, PS(gc_divisor) - 1) < PS(gc_probability);
 		}
 
@@ -2984,14 +2996,7 @@ static PHP_GINIT_FUNCTION(ps) /* {{{ */
 		.algo = &php_random_algo_pcgoneseq128xslrr64,
 		.state = &ps_globals->random_state,
 	};
-	php_random_uint128_t seed;
-	if (php_random_bytes_silent(&seed, sizeof(seed)) == FAILURE) {
-		seed = php_random_uint128_constant(
-			php_random_generate_fallback_seed(),
-			php_random_generate_fallback_seed()
-		);
-	}
-	php_random_pcgoneseq128xslrr64_seed128(ps_globals->random.state, seed);
+	ps_globals->random_seeded = false;
 }
 /* }}} */
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.