Skip to content

Commit c0a66a1

Browse files
committed
Suppress erronous warning for stream casts that do not read buffer
1 parent 1c7284e commit c0a66a1

6 files changed

+33
-33
lines changed

ext/posix/posix.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,15 @@ static int php_posix_stream_get_fd(zval *zfp, zend_long *fd) /* {{{ */
426426
if (stream == NULL) {
427427
return 0;
428428
}
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);
429+
430+
/* get the fd.
431+
* NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag when casting.
432+
* It is only used here so that the buffered data warning is not displayed.
433+
*/
434+
if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL) == SUCCESS) {
435+
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&fd, 0);
436+
} else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL) == SUCCESS) {
437+
php_stream_cast(stream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL, (void*)&fd, 0);
433438
} else {
434439
php_error_docref(NULL, E_WARNING, "Could not use stream of type '%s'",
435440
stream->ops->label);

ext/posix/tests/posix_isatty_stream_lose_data.phpt renamed to ext/posix/tests/posix_isatty_no_warning_on_stream_cast.phpt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
posix_isatty(): casting stream emits data loss
2+
posix_isatty(): casting stream does not emit data loss and should not emit warnings
33
--EXTENSIONS--
44
posix
55
--SKIPIF--
@@ -28,9 +28,6 @@ fclose($handle);
2828

2929
http_server_kill($pid);
3030
?>
31-
--EXPECTF--
32-
Warning: posix_isatty(): %d bytes of buffered data lost during stream conversion! in %s on line %d
33-
34-
Warning: posix_isatty(): %d bytes of buffered data lost during stream conversion! in %s on line %d
31+
--EXPECT--
3532
bool(false)
3633
string(4) "Body"

ext/posix/tests/posix_ttyname_stream_lose_data.phpt renamed to ext/posix/tests/posix_ttyname_no_warning_on_cast.phpt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
posix_ttyname(): casting stream emits data loss
2+
posix_ttyname(): casting stream does not emit data loss and should not emit warnings
33
--EXTENSIONS--
44
posix
55
--SKIPIF--
@@ -28,9 +28,6 @@ fclose($handle);
2828

2929
http_server_kill($pid);
3030
?>
31-
--EXPECTF--
32-
Warning: posix_ttyname(): %d bytes of buffered data lost during stream conversion! in %s on line %d
33-
34-
Warning: posix_ttyname(): %d bytes of buffered data lost during stream conversion! in %s on line %d
31+
--EXPECT--
3532
bool(false)
3633
string(4) "Body"

ext/standard/streamsfuncs.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,10 +1645,14 @@ PHP_FUNCTION(stream_isatty)
16451645

16461646
php_stream_from_zval(stream, zsrc);
16471647

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);
1648+
/* get the fd.
1649+
* NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag when casting.
1650+
* It is only used here so that the buffered data warning is not displayed.
1651+
*/
1652+
if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL) == SUCCESS) {
1653+
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&fileno, 0);
1654+
} else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL) == SUCCESS) {
1655+
php_stream_cast(stream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL, (void*)&fileno, 0);
16521656
} else {
16531657
RETURN_FALSE;
16541658
}
@@ -1686,11 +1690,14 @@ PHP_FUNCTION(sapi_windows_vt100_support)
16861690

16871691
php_stream_from_zval(stream, zsrc);
16881692

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);
1693+
/* get the fd.
1694+
* NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag when casting.
1695+
* It is only used here so that the buffered data warning is not displayed.
1696+
*/
1697+
if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL) == SUCCESS) {
1698+
php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&fileno, 0);
1699+
} else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL) == SUCCESS) {
1700+
php_stream_cast(stream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL, (void*)&fileno, 0);
16941701
} else {
16951702
if (!enable_is_null) {
16961703
php_error_docref(

tests/output/sapi_windows_vt100_support_stream_lose_data.phpt renamed to tests/output/sapi_windows_vt100_support_no_warning_on_cast.phpt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
sapi_windows_vt100_support(): casting stream emits data loss
2+
sapi_windows_vt100_support(): casting stream does not emit data loss and should not emit warnings
33
--SKIPIF--
44
<?php
55
if (PHP_OS_FAMILY !== "Windows") {
@@ -29,9 +29,6 @@ fclose($handle);
2929

3030
http_server_kill($pid);
3131
?>
32-
--EXPECTF--
33-
Warning: sapi_windows_vt100_support(): %d bytes of buffered data lost during stream conversion! in %s on line %d
34-
35-
Warning: sapi_windows_vt100_support(): %d bytes of buffered data lost during stream conversion! in %s on line %d
32+
--EXPECT--
3633
bool(false)
3734
string(4) "Body"

tests/output/stream_isatty_stream_lose_data.phpt renamed to tests/output/stream_isatty_no_warning_on_cast.phpt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
stream_isatty(): casting stream emits data loss
2+
stream_isatty(): casting stream does not emit data loss and should not emit warnings
33
Bug GH-10092 (Internal stream casting should not emit lost bytes warning twice)
44
--SKIPIF--
55
<?php
@@ -27,9 +27,6 @@ fclose($handle);
2727

2828
http_server_kill($pid);
2929
?>
30-
--EXPECTF--
31-
Warning: stream_isatty(): %d bytes of buffered data lost during stream conversion! in %s on line %d
32-
33-
Warning: stream_isatty(): %d bytes of buffered data lost during stream conversion! in %s on line %d
30+
--EXPECT--
3431
bool(false)
3532
string(4) "Body"

0 commit comments

Comments
 (0)