Summary:
cms_filecache_driver::_write_cache_file() never truncates the file: "unserialize(): Extra data" warnings on PHP 8.3+
Detailed Description:
Version: CMS Made Simple 2.2.24 (the code appears unchanged in earlier 2.2.x
releases)
Environment: PHP 8.4.27 (official php:8.4-apache
In lib/classes/class.cms_filecache_driver.php, method _write_cache_file():
@touch($fn);
$fp = @fopen($fn,'r+');
...
$data = self::KEY_SERIALIZED.serialize($data);
@fwrite($fp,$data);
The file is opened with mode r+, which does not truncate it, and it is then
written without any call to ftruncate(). When the new serialized value is
shorter than the one already stored, the tail of the old content remains after
the new content.
On read, unserialize() still returns the first (correct) value, but it emits:
PHP Warning: unserialize(): Extra data starting at offset N of M bytes
in lib/classes/internal/class.CmsTemplateCache.php on line 67
This was a notice before PHP 8.3 and is a warning since. It is logged dozens of
times per page on a cold cache (the template_cache entry is the main one
affected), so the PHP error log fills up quickly. No data is lost; the noise is
the only visible effect.
Steps to reproduce
Empty tmp/cache.
Load several different front-end pages of the site (each page rewrites the
shared template_cache entry with a different list of templates).
Check the PHP error log.
Proposed fix :
Add @ftruncate($fp,0); right before @fwrite($fp,$data);. The write lock is
already held at that point, so the truncation is safe:
$data = self::KEY_SERIALIZED.serialize($data);
}
@ftruncate($fp,0);
@fwrite($fp,$data);
$this->_flock($fp,self::LOCK_UNLOCK);
With this one-line change, the warnings disappear completely (0 warnings over 18
requests across the front end and the admin login, with identical page output).