Skip to content

Commit 279eae2

Browse files
authored
fix: suppress stty stderr leak in CLI::generateDimensions() when stdin is not a TTY (#10124)
1 parent a442b12 commit 279eae2

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

system/CLI/CLI.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ public static function generateDimensions()
778778
static::$width = (int) $matches[2];
779779
}
780780
}
781-
} elseif (($size = exec('stty size')) && preg_match('/(\d+)\s+(\d+)/', $size, $matches)) {
781+
} elseif (($size = exec('stty size 2>/dev/null')) && preg_match('/(\d+)\s+(\d+)/', $size, $matches)) {
782782
static::$height = (int) $matches[1];
783783
static::$width = (int) $matches[2];
784784
} else {

tests/system/CLI/CLITest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use CodeIgniter\Test\StreamFilterTrait;
2222
use PHPUnit\Framework\Attributes\DataProvider;
2323
use PHPUnit\Framework\Attributes\Group;
24+
use PHPUnit\Framework\Attributes\RequiresOperatingSystem;
2425
use ReflectionProperty;
2526

2627
/**
@@ -594,6 +595,33 @@ public function testWindow(): void
594595
$this->assertIsInt(CLI::getWidth());
595596
}
596597

598+
#[RequiresOperatingSystem('Darwin|Linux')]
599+
public function testGenerateDimensionsDoesNotLeakSttyErrorToStderr(): void
600+
{
601+
$code = <<<'PHP'
602+
require __DIR__ . '/system/Test/bootstrap.php';
603+
CodeIgniter\CLI\CLI::generateDimensions();
604+
PHP;
605+
606+
$cmd = sprintf('%s -r %s < /dev/null', PHP_BINARY, escapeshellarg($code));
607+
608+
$proc = proc_open(
609+
$cmd,
610+
[1 => ['pipe', 'w'], 2 => ['pipe', 'w']],
611+
$pipes,
612+
ROOTPATH,
613+
);
614+
$this->assertIsResource($proc);
615+
616+
stream_get_contents($pipes[1]);
617+
$stderr = stream_get_contents($pipes[2]);
618+
fclose($pipes[1]);
619+
fclose($pipes[2]);
620+
proc_close($proc);
621+
622+
$this->assertSame('', $stderr);
623+
}
624+
597625
/**
598626
* @param array $tbody
599627
* @param array $thead

user_guide_src/source/changelogs/v4.7.3.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Bugs Fixed
3737
**********
3838

3939
- **Autoloader:** Fixed a bug where ``Autoloader::unregister()`` (used during tests) silently failed to remove handlers from the SPL autoload stack, causing closures to accumulate permanently.
40+
- **CLI:** Fixed a bug where ``CLI::generateDimensions()`` leaked ``stty`` error output (e.g., ``stty: 'standard input': Inappropriate ioctl for device``) to stderr when stdin was not a TTY.
4041
- **Commands:** Fixed a bug in the ``env`` command where passing options only would cause the command to throw a ``TypeError`` instead of showing the current environment.
4142
- **Common:** Fixed a bug where the ``command()`` helper function did not properly clean up output buffers, which could lead to risky tests when exceptions were thrown.
4243
- **Validation:** Fixed a bug where ``Validation::getValidated()`` dropped fields whose validated value was explicitly ``null``.

0 commit comments

Comments
 (0)