Skip to content

Commit a2cabb9

Browse files
adawolfanicolas-grekas
authored andcommitted
[7.2] Add missing validation to sapi_windows_vt100_support()
1 parent 86e2777 commit a2cabb9

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)