diff options
| author | Nikita Popov <nikita.ppv@gmail.com> | 2019-07-18 15:25:59 +0200 |
|---|---|---|
| committer | Nikita Popov <nikita.ppv@gmail.com> | 2019-07-22 17:17:28 +0200 |
| commit | d59aac58b3e7da7ad01a194fe9840d89725ea229 (patch) | |
| tree | 5cfc4509f8aa6f9cb0c49df3530fb82c5b0456df /main/streams/userspace.c | |
| parent | c817b8020c8a835946681ca94b9257e78e64dad3 (diff) | |
| download | php-git-d59aac58b3e7da7ad01a194fe9840d89725ea229.tar.gz | |
Report errors from stream read and write operations
The php_stream_read() and php_stream_write() functions now return
an ssize_t value, with negative results indicating failure. Functions
like fread() and fwrite() will return false in that case.
As a special case, EWOULDBLOCK and EAGAIN on non-blocking streams
should not be regarded as error conditions, and be reported as
successful zero-length reads/writes instead. The handling of EINTR
remains unclear and is internally inconsistent (e.g. some code-paths
will automatically retry on EINTR, while some won't).
I'm landing this now to make sure the stream wrapper ops API changes
make it into 7.4 -- however, if the user-facing changes turn out to
be problematic we have the option of clamping negative returns to
zero in php_stream_read() and php_stream_write() to restore the
old behavior in a relatively non-intrusive manner.
Diffstat (limited to 'main/streams/userspace.c')
| -rw-r--r-- | main/streams/userspace.c | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/main/streams/userspace.c b/main/streams/userspace.c index bdd9b1b48e..de3e8591e6 100644 --- a/main/streams/userspace.c +++ b/main/streams/userspace.c @@ -588,14 +588,14 @@ PHP_FUNCTION(stream_wrapper_restore) } /* }}} */ -static size_t php_userstreamop_write(php_stream *stream, const char *buf, size_t count) +static ssize_t php_userstreamop_write(php_stream *stream, const char *buf, size_t count) { zval func_name; zval retval; int call_result; php_userstream_data_t *us = (php_userstream_data_t *)stream->abstract; zval args[1]; - size_t didwrite = 0; + ssize_t didwrite; assert(us != NULL); @@ -612,18 +612,21 @@ static size_t php_userstreamop_write(php_stream *stream, const char *buf, size_t zval_ptr_dtor(&args[0]); zval_ptr_dtor(&func_name); - didwrite = 0; - if (EG(exception)) { - return 0; + return -1; } if (call_result == SUCCESS && Z_TYPE(retval) != IS_UNDEF) { - convert_to_long(&retval); - didwrite = Z_LVAL(retval); - } else if (call_result == FAILURE) { + if (Z_TYPE(retval) == IS_FALSE) { + didwrite = -1; + } else { + convert_to_long(&retval); + didwrite = Z_LVAL(retval); + } + } else { php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_WRITE " is not implemented!", us->wrapper->classname); + didwrite = -1; } /* don't allow strange buffer overruns due to bogus return */ @@ -639,7 +642,7 @@ static size_t php_userstreamop_write(php_stream *stream, const char *buf, size_t return didwrite; } -static size_t php_userstreamop_read(php_stream *stream, char *buf, size_t count) +static ssize_t php_userstreamop_read(php_stream *stream, char *buf, size_t count) { zval func_name; zval retval; @@ -669,6 +672,10 @@ static size_t php_userstreamop_read(php_stream *stream, char *buf, size_t count) } if (call_result == SUCCESS && Z_TYPE(retval) != IS_UNDEF) { + if (Z_TYPE(retval) == IS_FALSE) { + return -1; + } + if (!try_convert_to_string(&retval)) { return -1; } @@ -1401,7 +1408,7 @@ static int user_wrapper_stat_url(php_stream_wrapper *wrapper, const char *url, i } -static size_t php_userstreamop_readdir(php_stream *stream, char *buf, size_t count) +static ssize_t php_userstreamop_readdir(php_stream *stream, char *buf, size_t count) { zval func_name; zval retval; @@ -1412,7 +1419,7 @@ static size_t php_userstreamop_readdir(php_stream *stream, char *buf, size_t cou /* avoid problems if someone mis-uses the stream */ if (count != sizeof(php_stream_dirent)) - return 0; + return -1; ZVAL_STRINGL(&func_name, USERSTREAM_DIR_READ, sizeof(USERSTREAM_DIR_READ)-1); |
