[php-src] PHP-8.4: Revert "ext/session: Truncate session file after successful write"

From: Date: Thu, 19 Nov 2026 15:26:45 +0000
Subject: [php-src] PHP-8.4: Revert "ext/session: Truncate session file after successful write"
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-143919@lists.php.net to get a copy of this message
Author: Jakub Zelenka (bukka) Date: 2026-11-19T16:10:03+01:00 Commit: https://github.com/php/php-src/commit/4c5abca7c111332f53f88221e1b0815f9b5ff4bb Raw diff: https://github.com/php/php-src/commit/4c5abca7c111332f53f88221e1b0815f9b5ff4bb.diff Revert "ext/session: Truncate session file after successful write" This reverts commit c2b02f24dffae346eef347db8a3323fbbe0f43fa. Writing over the previous data before truncating conflicts with the fix for GHSA-fm2m-fjf5-v3cp. Changed paths: D ext/session/tests/session_files_repeated_write.phpt D ext/session/tests/session_write_failure_keeps_data.phpt M ext/session/mod_files.c Diff: diff --git a/ext/session/mod_files.c b/ext/session/mod_files.c index 5097e0dc743a..29c1a7740f6a 100644 --- a/ext/session/mod_files.c +++ b/ext/session/mod_files.c @@ -236,6 +236,11 @@ static zend_result ps_files_write(ps_files *data, zend_string *key, zend_string return FAILURE; } + /* Truncate file if the amount of new data is smaller than the existing data set. */ + if (ZSTR_LEN(val) < data->st_size) { + php_ignore_value(ftruncate(data->fd, 0)); + } + #ifdef HAVE_PWRITE n = pwrite(data->fd, ZSTR_VAL(val), ZSTR_LEN(val), 0); #else @@ -269,12 +274,6 @@ static zend_result ps_files_write(ps_files *data, zend_string *key, zend_string return FAILURE; } - if (ZSTR_LEN(val) < data->st_size) { - php_ignore_value(ftruncate(data->fd, ZSTR_LEN(val))); - } - - data->st_size = ZSTR_LEN(val); - return SUCCESS; } diff --git a/ext/session/tests/session_files_repeated_write.phpt b/ext/session/tests/session_files_repeated_write.phpt deleted file mode 100644 index d6f3446ba27f..000000000000 --- a/ext/session/tests/session_files_repeated_write.phpt +++ /dev/null @@ -1,54 +0,0 @@ ---TEST-- -Session files handler truncates repeated writes to the current data length ---EXTENSIONS-- -session ---INI-- -session.save_handler=files -session.save_path={PWD} -session.use_cookies=0 -session.cache_limiter= -session.gc_probability=0 ---FILE-- -<?php -$id = 'session-files-repeated-write'; -$file = __DIR__ . '/sess_' . $id; -$handler = new SessionHandler(); -session_set_save_handler($handler); -$values = ['longer data', 'short', 'SHORT', 'much longer data', '']; - -for ($count = 1; $count <= count($values); $count++) { - file_put_contents($file, ''); - session_id($id); - session_start(); - foreach (array_slice($values, 0, $count) as $value) { - var_dump($handler->write($id, $value)); - } - session_abort(); - var_dump(file_get_contents($file)); -} -?> ---CLEAN-- -<?php -unlink(__DIR__ . '/sess_session-files-repeated-write'); -?> ---EXPECT-- -bool(true) -string(11) "longer data" -bool(true) -bool(true) -string(5) "short" -bool(true) -bool(true) -bool(true) -string(5) "SHORT" -bool(true) -bool(true) -bool(true) -bool(true) -string(16) "much longer data" -bool(true) -bool(true) -bool(true) -bool(true) -bool(true) -string(0) "" diff --git a/ext/session/tests/session_write_failure_keeps_data.phpt b/ext/session/tests/session_write_failure_keeps_data.phpt deleted file mode 100644 index 232b3e5313cf..000000000000 --- a/ext/session/tests/session_write_failure_keeps_data.phpt +++ /dev/null @@ -1,62 +0,0 @@ ---TEST-- -Session files handler must not truncate session file when write fails ---EXTENSIONS-- -session -posix -pcntl ---INI-- -error_reporting=E_ALL -display_errors=1 -session.use_strict_mode=0 -session.save_handler=files -session.serialize_handler=php ---FILE-- -<?php -$dir = __DIR__ . '/session_write_failure_keeps_data'; -mkdir($dir); -ini_set('session.save_path', $dir); - -$sid = 'write-failure'; -$file = $dir . '/sess_' . $sid; - -ob_start(); - -session_id($sid); -session_start(); -$_SESSION['data'] = 'original data'; -session_write_close(); - -$before = file_get_contents($file); -echo "Original contents: "; -var_dump($before); - -// A zero file size limit makes the next write fail before any byte lands; -// SIGXFSZ has to be ignored or the process is killed instead. -pcntl_signal(SIGXFSZ, SIG_IGN); -echo "Set file size limit to zero: "; -var_dump(posix_setrlimit(POSIX_RLIMIT_FSIZE, 0, POSIX_RLIMIT_INFINITY)); - -session_start(); -$_SESSION['data'] = 'new'; -@session_write_close(); - -// Restore the limit so that shutdown (and gcov) can write files -posix_setrlimit(POSIX_RLIMIT_FSIZE, POSIX_RLIMIT_INFINITY, POSIX_RLIMIT_INFINITY); - -echo "Contents unchanged after failed write: "; -var_dump(file_get_contents($file) === $before); - -ob_end_flush(); -?> ---CLEAN-- -<?php -$dir = __DIR__ . '/session_write_failure_keeps_data'; -foreach (glob($dir . '/sess_*') as $f) { - unlink($f); -} -rmdir($dir); -?> ---EXPECT-- -Original contents: string(26) "data|s:13:"original data";" -Set file size limit to zero: bool(true) -Contents unchanged after failed write: bool(true)

« previous php.cvs (#143919) next »