From 10fa457f32fee83bb16ff57eddb2c1823b2f85ee Mon Sep 17 00:00:00 2001 From: Kairat Jenishev Date: Sun, 8 Dec 2024 13:46:32 +0600 Subject: [PATCH 1/4] Fix "Trying to access array offset on null" warning, since the return value of `error_get_last()` can be `null` #17365 --- framework/caching/FileCache.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/framework/caching/FileCache.php b/framework/caching/FileCache.php index aaa5049fef9..cc6092b3f26 100644 --- a/framework/caching/FileCache.php +++ b/framework/caching/FileCache.php @@ -158,8 +158,14 @@ protected function setValue($key, $value, $duration) return @touch($cacheFile, $duration + time()); } - $error = error_get_last(); - Yii::warning("Unable to write cache file '{$cacheFile}': {$error['message']}", __METHOD__); + $message = "Unable to write cache file '{$cacheFile}'"; + + if ($error = error_get_last()) { + $message .= ": {$error['message']}"; + } + + Yii::warning($message, __METHOD__); + return false; } From 0b3370eb368b6d8a46302c124f2e4a5dd93a42c0 Mon Sep 17 00:00:00 2001 From: Kairat Jenishev Date: Sun, 8 Dec 2024 18:49:49 +0600 Subject: [PATCH 2/4] Update changelog --- framework/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 13f581ff405..60cc9c34596 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -21,6 +21,7 @@ Yii Framework 2 Change Log - Enh #20279: Add to the `\yii\web\Request` `csrfTokenSafeMethods` property to configure a custom safe HTTP methods list (olegbaturin) - Bug #20140: Fix compatibility with PHP 8.4: calling `session_set_save_handler()` (Izumi-kun) - New #20185: Add `BackedEnum` support to `AttributeTypecastBehavior` (briedis) +- Bug #17365: Fix "Trying to access array offset on null" warning (xcopy) 2.0.51 July 18, 2024 -------------------- From b3c23da52f4e2e8a28581b7e397f1131a46b9c9a Mon Sep 17 00:00:00 2001 From: Kairat Jenishev Date: Sun, 8 Dec 2024 23:34:05 +0600 Subject: [PATCH 3/4] Append additional (error) message only if `error_get_last()` returns non-null value #20294 --- framework/caching/FileCache.php | 15 +++++++-------- framework/log/FileTarget.php | 5 +++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/framework/caching/FileCache.php b/framework/caching/FileCache.php index cc6092b3f26..bdca7789b1b 100644 --- a/framework/caching/FileCache.php +++ b/framework/caching/FileCache.php @@ -159,10 +159,7 @@ protected function setValue($key, $value, $duration) } $message = "Unable to write cache file '{$cacheFile}'"; - - if ($error = error_get_last()) { - $message .= ": {$error['message']}"; - } + ($error = error_get_last()) and $message .= ": {$error['message']}"; Yii::warning($message, __METHOD__); @@ -271,20 +268,22 @@ protected function gcRecursive($path, $expiredOnly) continue; } $fullPath = $path . DIRECTORY_SEPARATOR . $file; + $message = null; if (is_dir($fullPath)) { $this->gcRecursive($fullPath, $expiredOnly); if (!$expiredOnly) { if (!@rmdir($fullPath)) { - $error = error_get_last(); - Yii::warning("Unable to remove directory '{$fullPath}': {$error['message']}", __METHOD__); + $message = "Unable to remove directory '$fullPath'"; + ($error = error_get_last()) and $message .= ": {$error['message']}"; } } } elseif (!$expiredOnly || $expiredOnly && @filemtime($fullPath) < time()) { if (!@unlink($fullPath)) { - $error = error_get_last(); - Yii::warning("Unable to remove file '{$fullPath}': {$error['message']}", __METHOD__); + $message = "Unable to remove file '$fullPath'"; + ($error = error_get_last()) and $message .= ": {$error['message']}"; } } + $message and Yii::warning($message, __METHOD__); } closedir($handle); } diff --git a/framework/log/FileTarget.php b/framework/log/FileTarget.php index d4c267e2ed1..b68da965622 100644 --- a/framework/log/FileTarget.php +++ b/framework/log/FileTarget.php @@ -131,8 +131,9 @@ public function export() } $writeResult = @fwrite($fp, $text); if ($writeResult === false) { - $error = error_get_last(); - throw new LogRuntimeException("Unable to export log through file ({$this->logFile})!: {$error['message']}"); + $message = "Unable to export log through file ($this->logFile)!"; + ($error = error_get_last()) and $message .= ": {$error['message']}"; + throw new LogRuntimeException($message); } $textSize = strlen($text); if ($writeResult < $textSize) { From 52ff9dbd485a5fbe370d5b29ae0a1d4ca7700a6b Mon Sep 17 00:00:00 2001 From: Kairat Jenishev Date: Mon, 9 Dec 2024 17:59:51 +0600 Subject: [PATCH 4/4] Prefer classic `if` construct #20294 --- framework/caching/FileCache.php | 13 ++++++++++--- framework/log/FileTarget.php | 4 +++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/framework/caching/FileCache.php b/framework/caching/FileCache.php index bdca7789b1b..864299f840f 100644 --- a/framework/caching/FileCache.php +++ b/framework/caching/FileCache.php @@ -159,7 +159,10 @@ protected function setValue($key, $value, $duration) } $message = "Unable to write cache file '{$cacheFile}'"; - ($error = error_get_last()) and $message .= ": {$error['message']}"; + + if ($error = error_get_last()) { + $message .= ": {$error['message']}"; + } Yii::warning($message, __METHOD__); @@ -274,13 +277,17 @@ protected function gcRecursive($path, $expiredOnly) if (!$expiredOnly) { if (!@rmdir($fullPath)) { $message = "Unable to remove directory '$fullPath'"; - ($error = error_get_last()) and $message .= ": {$error['message']}"; + if ($error = error_get_last()) { + $message .= ": {$error['message']}"; + } } } } elseif (!$expiredOnly || $expiredOnly && @filemtime($fullPath) < time()) { if (!@unlink($fullPath)) { $message = "Unable to remove file '$fullPath'"; - ($error = error_get_last()) and $message .= ": {$error['message']}"; + if ($error = error_get_last()) { + $message .= ": {$error['message']}"; + } } } $message and Yii::warning($message, __METHOD__); diff --git a/framework/log/FileTarget.php b/framework/log/FileTarget.php index b68da965622..d860c4014ef 100644 --- a/framework/log/FileTarget.php +++ b/framework/log/FileTarget.php @@ -132,7 +132,9 @@ public function export() $writeResult = @fwrite($fp, $text); if ($writeResult === false) { $message = "Unable to export log through file ($this->logFile)!"; - ($error = error_get_last()) and $message .= ": {$error['message']}"; + if ($error = error_get_last()) { + $message .= ": {$error['message']}"; + } throw new LogRuntimeException($message); } $textSize = strlen($text);