Skip to content

Commit 884851f

Browse files
bug #114 [7.2] Add missing validation to sapi_windows_vt100_support() (adawolfa)
This PR was squashed before being merged into the 1.8-dev branch (closes #114). Discussion ---------- [7.2] Add missing validation to sapi_windows_vt100_support() While investigating [this issue](symfony/symfony#26210), I've noticed that polyfill implementation of `sapi_windows_vt100_support()` is simplified. As per PHP source code, native function: - requires the first argument to be a resource, emits a warning otherwise, - requires the stream to be a file descriptor, emits an unanalyzable stream warning otherwise, - checks whether the stream is a TTY after validations above. This PR aligns the polyfill with the native function by: - validating `$stream` argument to be a resource, - checking if the stream type is a `STDIO`, returns `FALSE` otherwise, - if stream is not `STDIO`, attempts to call `stream_select()` and emits the _unanalyzable stream_ warning when a `FALSE` gets returned in advance, - performing validations above before `$enable` check and detections (including TTY). I've compared the behavior with output tests ([test](https://github.com/php/php-src/blob/33301d5bae4964f74fd1fc8c4fc485abfde0378e/tests/output/sapi_windows_vt100_support.inc), [output](https://github.com/php/php-src/blob/f9959ee7c2004919675d9cdc7c82f886f099f15f/tests/output/sapi_windows_vt100_support_winko_err.phpt)) used in PHP source and enhanced unit tests accordingly. Commits ------- a2cabb9 [7.2] Add missing validation to sapi_windows_vt100_support()
2 parents af78fb5 + a2cabb9 commit 884851f

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/Php72/Php72.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,25 @@ public static function spl_object_id($object)
9797

9898
public static function sapi_windows_vt100_support($stream, $enable = null)
9999
{
100+
if (!is_resource($stream)) {
101+
trigger_error('sapi_windows_vt100_support() expects parameter 1 to be resource, '.gettype($stream).' given', E_USER_WARNING);
102+
return false;
103+
}
104+
105+
$meta = stream_get_meta_data($stream);
106+
107+
if ('STDIO' !== $meta['stream_type']) {
108+
trigger_error('sapi_windows_vt100_support() was not able to analyze the specified stream', E_USER_WARNING);
109+
return false;
110+
}
111+
100112
// We cannot actually disable vt100 support if it is set
101113
if (false === $enable || !self::stream_isatty($stream)) {
102114
return false;
103115
}
104116

105117
// The native function does not apply to stdin
106-
$meta = array_map('strtolower', stream_get_meta_data($stream));
118+
$meta = array_map('strtolower', $meta);
107119
$stdin = 'php://stdin' === $meta['uri'] || 'php://fd/0' === $meta['uri'];
108120

109121
return !$stdin
@@ -114,6 +126,11 @@ public static function sapi_windows_vt100_support($stream, $enable = null)
114126

115127
public static function stream_isatty($stream)
116128
{
129+
if (!is_resource($stream)) {
130+
trigger_error('stream_isatty() expects parameter 1 to be resource, '.gettype($stream).' given', E_USER_WARNING);
131+
return false;
132+
}
133+
117134
if ('\\' === DIRECTORY_SEPARATOR) {
118135
$stat = @fstat($stream);
119136
// Check if formatted mode is S_IFCHR

tests/Php72/Php72Test.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,32 @@ public function testSapiWindowsVt100Support()
7878
$this->assertFalse(sapi_windows_vt100_support(STDIN, true));
7979
}
8080

81+
/**
82+
* @covers Symfony\Polyfill\Php72\Php72::sapi_windows_vt100_support
83+
*/
84+
public function testSapiWindowsVt100SupportWarnsOnInvalidInputType()
85+
{
86+
if ('\\' !== DIRECTORY_SEPARATOR) {
87+
$this->markTestSkipped('Windows only test');
88+
}
89+
90+
$this->setExpectedException('PHPUnit_Framework_Error_Warning', 'expects parameter 1 to be resource');
91+
sapi_windows_vt100_support('foo', true);
92+
}
93+
94+
/**
95+
* @covers Symfony\Polyfill\Php72\Php72::sapi_windows_vt100_support
96+
*/
97+
public function testSapiWindowsVt100SupportWarnsOnInvalidStream()
98+
{
99+
if ('\\' !== DIRECTORY_SEPARATOR) {
100+
$this->markTestSkipped('Windows only test');
101+
}
102+
103+
$this->setExpectedException('PHPUnit_Framework_Error_Warning', 'was not able to analyze the specified stream');
104+
sapi_windows_vt100_support(fopen('php://memory', 'wb'), true);
105+
}
106+
81107
/**
82108
* @covers Symfony\Polyfill\Php72\Php72::stream_isatty
83109
*/
@@ -87,4 +113,13 @@ public function testStreamIsatty()
87113
$this->assertFalse(stream_isatty($fp));
88114
fclose($fp);
89115
}
116+
117+
/**
118+
* @covers Symfony\Polyfill\Php72\Php72::stream_isatty
119+
*/
120+
public function testStreamIsattyWarnsOnInvalidInputType()
121+
{
122+
$this->setExpectedException('PHPUnit_Framework_Error_Warning', 'expects parameter 1 to be resource');
123+
stream_isatty('foo');
124+
}
90125
}

0 commit comments

Comments
 (0)