Skip to content

Commit 855a6a7

Browse files
skjnldsvbackportbot[bot]
authored andcommitted
fix(config): add null coalescing fallback in getValueBool before strtolower
Followup to #59646: guard against null reaching strtolower() in both AppConfig and UserConfig getValueBool(). Also aligns AppConfig with the (string) cast added in UserConfig by the original PR. Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
1 parent 839c9fb commit 855a6a7

3 files changed

Lines changed: 18 additions & 11 deletions

File tree

build/psalm-baseline.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3478,11 +3478,6 @@
34783478
<code><![CDATA[$CONFIG]]></code>
34793479
</UndefinedVariable>
34803480
</file>
3481-
<file src="lib/private/Config/UserConfig.php">
3482-
<RedundantCast>
3483-
<code><![CDATA[(string)$this->getTypedValue($userId, $app, $key, $default ? 'true' : 'false', $lazy, ValueType::BOOL)]]></code>
3484-
</RedundantCast>
3485-
</file>
34863481
<file src="lib/private/Console/Application.php">
34873482
<NoInterfaceProperties>
34883483
<code><![CDATA[$this->request->server]]></code>

lib/private/AppConfig.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,15 @@ public function getValueFloat(string $app, string $key, float $default = 0, bool
424424
* @see IAppConfig for explanation about lazy loading
425425
*/
426426
public function getValueBool(string $app, string $key, bool $default = false, bool $lazy = false): bool {
427-
$b = strtolower($this->getTypedValue($app, $key, $default ? 'true' : 'false', $lazy, self::VALUE_BOOL));
427+
// The explicit (string) cast and ?? null guard defend against a PHP OPcache bug where
428+
// values passed by reference across function boundaries can have their type corrupted
429+
// (e.g. bool returned as int, or null). Affects PHP 8.x with OPcache enabled; fixed
430+
// upstream in https://github.com/php/php-src/pull/21973. Keep until minimum PHP version
431+
// is bumped. Psalm sees the declared return type (string) and flags these as redundant.
432+
/** @psalm-suppress RedundantCondition, TypeDoesNotContainNull */
433+
$value = $this->getTypedValue($app, $key, $default ? 'true' : 'false', $lazy, self::VALUE_BOOL) ?? ($default ? 'true' : 'false');
434+
/** @psalm-suppress RedundantCast */
435+
$b = strtolower((string)$value);
428436
return in_array($b, ['1', 'true', 'yes', 'on']);
429437
}
430438

lib/private/Config/UserConfig.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -682,11 +682,15 @@ public function getValueBool(
682682
bool $default = false,
683683
bool $lazy = false,
684684
): bool {
685-
// The explicit (string) cast guards against a PHP OPcache bug where values passed
686-
// by reference across function boundaries can have their type corrupted (e.g. bool
687-
// returned as int). Affects PHP 8.x with OPcache enabled; fixed upstream in
688-
// https://github.com/php/php-src/pull/21973. Keep until minimum PHP version is bumped.
689-
$b = strtolower((string)$this->getTypedValue($userId, $app, $key, $default ? 'true' : 'false', $lazy, ValueType::BOOL));
685+
// The explicit (string) cast and ?? null guard defend against a PHP OPcache bug where
686+
// values passed by reference across function boundaries can have their type corrupted
687+
// (e.g. bool returned as int, or null). Affects PHP 8.x with OPcache enabled; fixed
688+
// upstream in https://github.com/php/php-src/pull/21973. Keep until minimum PHP version
689+
// is bumped. Psalm sees the declared return type (string) and flags these as redundant.
690+
/** @psalm-suppress RedundantCondition, TypeDoesNotContainNull */
691+
$value = $this->getTypedValue($userId, $app, $key, $default ? 'true' : 'false', $lazy, ValueType::BOOL) ?? ($default ? 'true' : 'false');
692+
/** @psalm-suppress RedundantCast */
693+
$b = strtolower((string)$value);
690694
return in_array($b, ['1', 'true', 'yes', 'on']);
691695
}
692696

0 commit comments

Comments
 (0)