Skip to content

Commit

Permalink
Merge pull request #3765 from paulbalandan/cli-disabled-exec
Browse files Browse the repository at this point in the history
Enclose CLI::generateDimensions in a try-catch
  • Loading branch information
paulbalandan authored Oct 14, 2020
2 parents 51a5452 + 0f002aa commit ab17df6
Showing 1 changed file with 47 additions and 33 deletions.
80 changes: 47 additions & 33 deletions system/CLI/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

use CodeIgniter\CLI\Exceptions\CLIException;
use Config\Services;
use Throwable;

/**
* Set of static methods useful for CLI request handling.
Expand Down Expand Up @@ -727,50 +728,63 @@ public static function getHeight(int $default = 32): int
*/
public static function generateDimensions()
{
if (static::isWindows())
try
{
// Shells such as `Cygwin` and `Git bash` returns incorrect values
// when executing `mode CON`, so we use `tput` instead
// @codeCoverageIgnoreStart
if (($shell = getenv('SHELL')) && preg_match('/(?:bash|zsh)(?:\.exe)?$/', $shell) || getenv('TERM'))
{
static::$height = (int) exec('tput lines');
static::$width = (int) exec('tput cols');
}
else
if (static::isWindows())
{
$return = -1;
$output = [];
exec('mode CON', $output, $return);

if ($return === 0 && $output)
// Shells such as `Cygwin` and `Git bash` returns incorrect values
// when executing `mode CON`, so we use `tput` instead
// @codeCoverageIgnoreStart
if (($shell = getenv('SHELL')) && preg_match('/(?:bash|zsh)(?:\.exe)?$/', $shell) || getenv('TERM'))
{
// Look for the next lines ending in ": <number>"
// Searching for "Columns:" or "Lines:" will fail on non-English locales
if (preg_match('/:\s*(\d+)\n[^:]+:\s*(\d+)\n/', implode("\n", $output), $matches))
static::$height = (int) exec('tput lines');
static::$width = (int) exec('tput cols');
}
else
{
$return = -1;
$output = [];
exec('mode CON', $output, $return);

if ($return === 0 && $output)
{
static::$height = (int) $matches[1];
static::$width = (int) $matches[2];
// Look for the next lines ending in ": <number>"
// Searching for "Columns:" or "Lines:" will fail on non-English locales
if (preg_match('/:\s*(\d+)\n[^:]+:\s*(\d+)\n/', implode("\n", $output), $matches))
{
static::$height = (int) $matches[1];
static::$width = (int) $matches[2];
}
}
}
}
// @codeCoverageIgnoreEnd
}
else
{
if (($size = exec('stty size')) && preg_match('/(\d+)\s+(\d+)/', $size, $matches))
{
static::$height = (int) $matches[1];
static::$width = (int) $matches[2];
// @codeCoverageIgnoreEnd
}
else
{
// @codeCoverageIgnoreStart
static::$height = (int) exec('tput lines');
static::$width = (int) exec('tput cols');
// @codeCoverageIgnoreEnd
if (($size = exec('stty size')) && preg_match('/(\d+)\s+(\d+)/', $size, $matches))
{
static::$height = (int) $matches[1];
static::$width = (int) $matches[2];
}
else
{
// @codeCoverageIgnoreStart
static::$height = (int) exec('tput lines');
static::$width = (int) exec('tput cols');
// @codeCoverageIgnoreEnd
}
}
}
// @codeCoverageIgnoreStart
catch (Throwable $e)
{
// Reset the dimensions so that the default values will be returned later.
// Then let the developer know of the error.
static::$height = null;
static::$width = null;
log_message('error', $e->getMessage());
}
// @codeCoverageIgnoreEnd
}

//--------------------------------------------------------------------
Expand Down

1 comment on commit ab17df6

@nc03061981
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @paulbalandan

Please sign in to comment.