Skip to content

Commit 7c745cf

Browse files
committed
Better resource guarding.
1 parent 1bcd6ee commit 7c745cf

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,5 @@ __*Why "v3.0.0" instead of "v1.0.0?"*__ Prior to phpMussel v3, the "phpMussel Co
131131
[2023.09.04; Maikuolan]: Added colouration to phpMussel's CLI mode (some code has been added to the core to facilitate this). The atHit method has been migrated from the Loader class to the Scanner class.
132132

133133
[2023.09.16~18; Maikuolan]: Significantly refactored all L10N data.
134+
135+
[2023.09.18; Maikuolan]: Better resource guarding.

src/Loader.php

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* License: GNU/GPLv2
99
* @see LICENSE.txt
1010
*
11-
* This file: The loader (last modified: 2023.09.17).
11+
* This file: The loader (last modified: 2023.09.18).
1212
*/
1313

1414
namespace phpMussel\Core;
@@ -410,12 +410,12 @@ public function __construct(
410410
$WriteMode = 'ab';
411411
$Data = $this->InstanceCache['PendingErrorLogData'];
412412
}
413-
$Handle = fopen($File, $WriteMode);
414-
if (is_resource($Handle)) {
415-
fwrite($Handle, $Data);
416-
fclose($Handle);
417-
$this->logRotation($this->Configuration['core']['error_log']);
413+
if (!is_resource($Handle = fopen($File, $WriteMode))) {
414+
return false;
418415
}
416+
fwrite($Handle, $Data);
417+
fclose($Handle);
418+
$this->logRotation($this->Configuration['core']['error_log']);
419419
return true;
420420
});
421421

@@ -476,8 +476,7 @@ public function readFile(string $File): string
476476
return '';
477477
}
478478

479-
$Handle = fopen($File, 'rb');
480-
if (!is_resource($Handle)) {
479+
if (!is_resource($Handle = fopen($File, 'rb'))) {
481480
return '';
482481
}
483482
$Data = fread($Handle, $Filesize);
@@ -960,8 +959,7 @@ public function readFileContentGZ(string $File): string
960959

961960
$Data = '';
962961
if ($BlocksToRead > 0) {
963-
$Handle = gzopen($File, 'rb');
964-
if (!is_resource($Handle)) {
962+
if (!is_resource($Handle = gzopen($File, 'rb'))) {
965963
return '';
966964
}
967965
$Done = 0;
@@ -1041,12 +1039,7 @@ public function gZCompressFile(string $File): bool
10411039
return false;
10421040
}
10431041

1044-
$Handle = fopen($File, 'rb');
1045-
if (!is_resource($Handle)) {
1046-
return false;
1047-
}
1048-
$HandleGZ = gzopen($File . '.gz', 'wb');
1049-
if (!is_resource($HandleGZ)) {
1042+
if (!is_resource($Handle = fopen($File, 'rb')) || !is_resource($HandleGZ = gzopen($File . '.gz', 'wb'))) {
10501043
return false;
10511044
}
10521045
while (!feof($Handle)) {
@@ -1237,8 +1230,7 @@ public function updateConfiguration(): bool
12371230
} else {
12381231
return false;
12391232
}
1240-
$Handle = fopen($this->ConfigurationPath, 'wb');
1241-
if (!is_resource($Handle)) {
1233+
if (!is_resource($Handle = fopen($this->ConfigurationPath, 'wb'))) {
12421234
return false;
12431235
}
12441236
$Err = fwrite($Handle, $Reconstructed);

src/Scanner.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* License: GNU/GPLv2
99
* @see LICENSE.txt
1010
*
11-
* This file: The scanner (last modified: 2023.09.04).
11+
* This file: The scanner (last modified: 2023.09.18).
1212
*/
1313

1414
namespace phpMussel\Core;
@@ -108,7 +108,10 @@ public function __construct(\phpMussel\Core\Loader &$Loader)
108108
]) . "\n";
109109
$Truncate = $this->Loader->readBytes($this->Loader->Configuration['core']['truncate']);
110110
$WriteMode = (!file_exists($File) || ($Truncate > 0 && filesize($File) >= $Truncate)) ? 'wb' : 'ab';
111-
$Stream = fopen($File, $WriteMode);
111+
if (!is_resource($Stream = fopen($File, $WriteMode))) {
112+
trigger_error('The "writeToSerialLog" event failed to open "' . $File . '" for writing.');
113+
return false;
114+
}
112115
fwrite($Stream, $Data);
113116
fclose($Stream);
114117
$this->Loader->logRotation($this->Loader->Configuration['core']['scan_log_serialized']);
@@ -146,7 +149,10 @@ public function __construct(\phpMussel\Core\Loader &$Loader)
146149
$Truncate = $this->Loader->readBytes($this->Loader->Configuration['core']['truncate']);
147150
$WriteMode = ($Truncate > 0 && filesize($File) >= $Truncate) ? 'wb' : 'ab';
148151
}
149-
$Handle = fopen($File, 'ab');
152+
if (!is_resource($Handle = fopen($File, 'ab'))) {
153+
trigger_error('The "writeToScanLog" event failed to open "' . $File . '" for writing.');
154+
return false;
155+
}
150156
fwrite($Handle, $Results);
151157
fclose($Handle);
152158
$this->Loader->logRotation($this->Loader->Configuration['core']['scan_log']);

0 commit comments

Comments
 (0)