Skip to content

Commit f789b09

Browse files
committed
php_stream_cast() seeks whereas php_stream_can_cast() does not
1 parent 64cc092 commit f789b09

7 files changed

+25
-12
lines changed

ext/posix/posix.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,11 @@ static int php_posix_stream_get_fd(zval *zfp, zend_long *fd) /* {{{ */
426426
if (stream == NULL) {
427427
return 0;
428428
}
429-
if (
430-
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fd, 0) == FAILURE &&
431-
php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fd, 0) == FAILURE
432-
) {
429+
if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT) == SUCCESS) {
430+
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)fd, 0);
431+
} else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD) == SUCCESS) {
432+
php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)fd, 0);
433+
} else {
433434
php_error_docref(NULL, E_WARNING, "Could not use stream of type '%s'",
434435
stream->ops->label);
435436
return 0;

ext/posix/tests/posix_isatty_stream_lose_data.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ fclose($handle);
2929
http_server_kill($pid);
3030
?>
3131
--EXPECTF--
32+
Warning: posix_isatty(): %d bytes of buffered data lost during stream conversion! in %s on line %d
33+
3234
Warning: posix_isatty(): %d bytes of buffered data lost during stream conversion! in %s on line %d
3335
bool(false)
3436
string(4) "Body"

ext/posix/tests/posix_ttyname_stream_lose_data.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ fclose($handle);
2929
http_server_kill($pid);
3030
?>
3131
--EXPECTF--
32+
Warning: posix_ttyname(): %d bytes of buffered data lost during stream conversion! in %s on line %d
33+
3234
Warning: posix_ttyname(): %d bytes of buffered data lost during stream conversion! in %s on line %d
3335
bool(false)
3436
string(4) "Body"

ext/standard/streamsfuncs.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,10 +1634,11 @@ PHP_FUNCTION(stream_isatty)
16341634

16351635
php_stream_from_zval(stream, zsrc);
16361636

1637-
if (
1638-
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fileno, 0) == FAILURE &&
1639-
php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fileno, 0) == FAILURE
1640-
) {
1637+
if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT) == SUCCESS) {
1638+
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fileno, 0);
1639+
} else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD) == SUCCESS) {
1640+
php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fileno, 0);
1641+
} else {
16411642
RETURN_FALSE;
16421643
}
16431644

@@ -1674,10 +1675,12 @@ PHP_FUNCTION(sapi_windows_vt100_support)
16741675

16751676
php_stream_from_zval(stream, zsrc);
16761677

1677-
if (
1678-
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fileno, 0) == FAILURE &&
1679-
php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fileno, 0) == FAILURE
1680-
) {
1678+
1679+
if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT) == SUCCESS) {
1680+
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fileno, 0);
1681+
} else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD) == SUCCESS) {
1682+
php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fileno, 0);
1683+
} else {
16811684
if (!enable_is_null) {
16821685
php_error_docref(
16831686
NULL,

tests/output/DummyStreamWrapper.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class DummyStreamWrapper
2727

2828
public function stream_seek(int $offset, int $whence = SEEK_SET): bool
2929
{
30+
var_dump('stream_seek!');
3031
return true;
3132
}
3233

tests/output/sapi_windows_vt100_support_stream_lose_data.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ fclose($handle);
3030
http_server_kill($pid);
3131
?>
3232
--EXPECTF--
33+
Warning: sapi_windows_vt100_support(): %d bytes of buffered data lost during stream conversion! in %s on line %d
34+
3335
Warning: sapi_windows_vt100_support(): %d bytes of buffered data lost during stream conversion! in %s on line %d
3436
bool(false)
3537
string(4) "Body"

tests/output/stream_isatty_stream_lose_data.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ fclose($handle);
2828
http_server_kill($pid);
2929
?>
3030
--EXPECTF--
31+
Warning: stream_isatty(): %d bytes of buffered data lost during stream conversion! in %s on line %d
32+
3133
Warning: stream_isatty(): %d bytes of buffered data lost during stream conversion! in %s on line %d
3234
bool(false)
3335
string(4) "Body"

0 commit comments

Comments
 (0)