Skip to content

Commit 2ff0339

Browse files
committed
Protect EnvironmentConfig autoload from an endless loop.
1 parent 9360fbe commit 2ff0339

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

src/Parametizer/EnvironmentConfig.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,12 @@ public static function createFromConfigsBottomUpHierarchy(
163163
return $envConfig;
164164
}
165165

166-
$currentDirPath = dirname($currentDirPath);
166+
$previousDirPath = $currentDirPath;
167+
$currentDirPath = dirname($currentDirPath);
168+
// Prevents a possible endless loop, if `$topmostDirectoryPath` is unreachable:
169+
if ($currentDirPath === $previousDirPath) {
170+
return $envConfig;
171+
}
167172
}
168173
}
169174

tests/Tests/Parametizer/EnvironmentConfig/EnvironmentConfigTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,15 @@ public static function provideThrowingOnExceptions(): array {
226226
],
227227
];
228228
}
229+
230+
/**
231+
* Tests no endless loop happens if a topmost directory is unreachable (exists in another files branch).
232+
*
233+
* This test should execute momentarily, unless an endless loop happens.
234+
*
235+
* @covers EnvironmentConfig::createFromConfigsBottomUpHierarchy()
236+
*/
237+
public function testUnreachableTopmost(): void {
238+
assertSame('', self::assertNoErrorsOutput(__DIR__ . '/autoload/unreachable-topmost.php')->getStdOut());
239+
}
229240
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use MagicPush\CliToolkit\Parametizer\EnvironmentConfig;
6+
7+
require_once __DIR__ . '/../../../init-console.php';
8+
9+
// Simulates a case, where config files search continues until the top of a file system is reached.
10+
EnvironmentConfig::createFromConfigsBottomUpHierarchy('/var/log', '/tmp', true);

tests/Tests/init-console.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66

77
mb_internal_encoding('UTF-8');
88
setlocale(LC_ALL, 'en_US.UTF-8');
9+
ini_set('max_execution_time', 2);

0 commit comments

Comments
 (0)