Skip to content

Commit 1c7284e

Browse files
committed
php_stream_cast() seeks whereas php_stream_can_cast() does not
1 parent 0478f9d commit 1c7284e

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
@@ -1645,10 +1645,11 @@ PHP_FUNCTION(stream_isatty)
16451645

16461646
php_stream_from_zval(stream, zsrc);
16471647

1648-
if (
1649-
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fileno, 0) == FAILURE &&
1650-
php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fileno, 0) == FAILURE
1651-
) {
1648+
if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT) == SUCCESS) {
1649+
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fileno, 0);
1650+
} else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD) == SUCCESS) {
1651+
php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fileno, 0);
1652+
} else {
16521653
RETURN_FALSE;
16531654
}
16541655

@@ -1685,10 +1686,12 @@ PHP_FUNCTION(sapi_windows_vt100_support)
16851686

16861687
php_stream_from_zval(stream, zsrc);
16871688

1688-
if (
1689-
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fileno, 0) == FAILURE &&
1690-
php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fileno, 0) == FAILURE
1691-
) {
1689+
1690+
if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT) == SUCCESS) {
1691+
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fileno, 0);
1692+
} else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD) == SUCCESS) {
1693+
php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fileno, 0);
1694+
} else {
16921695
if (!enable_is_null) {
16931696
php_error_docref(
16941697
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)