Skip to content

Commit 556c714

Browse files
committed
fix: suppress tput stderr leak when TERM is not present
1 parent 6c4acef commit 556c714

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

system/CLI/CLI.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,8 +782,8 @@ public static function generateDimensions()
782782
static::$height = (int) $matches[1];
783783
static::$width = (int) $matches[2];
784784
} else {
785-
static::$height = (int) exec('tput lines');
786-
static::$width = (int) exec('tput cols');
785+
static::$height = (int) exec('tput lines 2>/dev/null');
786+
static::$width = (int) exec('tput cols 2>/dev/null');
787787
}
788788
} catch (Throwable $e) {
789789
// Reset the dimensions so that the default values will be returned later.

tests/system/CLI/CLITest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,28 @@ public function testWindow(): void
597597

598598
#[RequiresOperatingSystem('Darwin|Linux')]
599599
public function testGenerateDimensionsDoesNotLeakSttyErrorToStderr(): void
600+
{
601+
$this->assertSame('', $this->captureGenerateDimensionsStderr());
602+
}
603+
604+
#[RequiresOperatingSystem('Darwin|Linux')]
605+
public function testGenerateDimensionsDoesNotLeakTputErrorToStderrWhenTermIsUnset(): void
606+
{
607+
$env = getenv();
608+
unset($env['TERM']);
609+
610+
$this->assertSame('', $this->captureGenerateDimensionsStderr($env));
611+
}
612+
613+
/**
614+
* Spawns a child PHP process that calls `CLI::generateDimensions()` with
615+
* `STDIN` pointed at `/dev/null` (forcing the non-TTY code path), and
616+
* returns whatever it wrote to stderr.
617+
*
618+
* @param array<string, string>|null $env Environment for the child process.
619+
* `null` inherits the parent env.
620+
*/
621+
private function captureGenerateDimensionsStderr(?array $env = null): string
600622
{
601623
$code = <<<'PHP'
602624
require __DIR__ . '/system/Test/bootstrap.php';
@@ -610,6 +632,7 @@ public function testGenerateDimensionsDoesNotLeakSttyErrorToStderr(): void
610632
[1 => ['pipe', 'w'], 2 => ['pipe', 'w']],
611633
$pipes,
612634
ROOTPATH,
635+
$env,
613636
);
614637
$this->assertIsResource($proc);
615638

@@ -619,7 +642,7 @@ public function testGenerateDimensionsDoesNotLeakSttyErrorToStderr(): void
619642
fclose($pipes[2]);
620643
proc_close($proc);
621644

622-
$this->assertSame('', $stderr);
645+
return $stderr;
623646
}
624647

625648
/**

user_guide_src/source/changelogs/v4.7.3.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Bugs Fixed
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.
4040
- **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.
41+
- **CLI:** Fixed a bug where ``CLI::generateDimensions()`` leaked ``tput`` error output (``tput: No value for $TERM and no -T specified``) to stderr when the ``stty`` fallback was reached and the ``TERM`` environment variable was not set.
4142
- **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.
4243
- **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.
4344
- **Database:** Fixed a bug where the SQLSRV driver's decrement method was adding instead of subtracting the decrement value when ``$castTextToInt`` was false.

0 commit comments

Comments
 (0)