Skip to content

Commit f8d6c3d

Browse files
committed
php_stream_cast() seeks whereas php_stream_can_cast() does not
1 parent 9dfe67a commit f8d6c3d

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

16381638
php_stream_from_zval(stream, zsrc);
16391639

1640-
if (
1641-
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fileno, 0) == FAILURE &&
1642-
php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fileno, 0) == FAILURE
1643-
) {
1640+
if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT) == SUCCESS) {
1641+
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fileno, 0);
1642+
} else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD) == SUCCESS) {
1643+
php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fileno, 0);
1644+
} else {
16441645
RETURN_FALSE;
16451646
}
16461647

@@ -1677,10 +1678,12 @@ PHP_FUNCTION(sapi_windows_vt100_support)
16771678

16781679
php_stream_from_zval(stream, zsrc);
16791680

1680-
if (
1681-
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fileno, 0) == FAILURE &&
1682-
php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fileno, 0) == FAILURE
1683-
) {
1681+
1682+
if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT) == SUCCESS) {
1683+
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fileno, 0);
1684+
} else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD) == SUCCESS) {
1685+
php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fileno, 0);
1686+
} else {
16841687
if (!enable_is_null) {
16851688
php_error_docref(
16861689
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)