From: Nikita Popov Date: Thu, 10 Oct 2019 09:12:17 +0000 (+0200) Subject: Report error if stream_read is not implemented X-Git-Tag: php-7.4.0RC4~23 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6878c583b02b75aeab84c493d548700fb1de5976;p=php Report error if stream_read is not implemented We need to return -1 in this case. Slightly restructure the code to avoid unnecessary conditions. --- diff --git a/Zend/zend_language_scanner.l b/Zend/zend_language_scanner.l index 13248b2420..fbcdd2c9b6 100644 --- a/Zend/zend_language_scanner.l +++ b/Zend/zend_language_scanner.l @@ -523,6 +523,7 @@ ZEND_API int open_file_for_scanning(zend_file_handle *file_handle) return FAILURE; } + ZEND_ASSERT(!EG(exception) && "stream_fixup() should have failed"); zend_llist_add_element(&CG(open_files), file_handle); if (file_handle->handle.stream.handle >= (void*)file_handle && file_handle->handle.stream.handle <= (void*)(file_handle+1)) { zend_file_handle *fh = (zend_file_handle*)zend_llist_get_last(&CG(open_files)); diff --git a/main/streams/userspace.c b/main/streams/userspace.c index de3e8591e6..af7f0b4fa2 100644 --- a/main/streams/userspace.c +++ b/main/streams/userspace.c @@ -671,27 +671,28 @@ static ssize_t php_userstreamop_read(php_stream *stream, char *buf, size_t count return -1; } - 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; - } - - didread = Z_STRLEN(retval); - if (didread > count) { - php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_READ " - read " ZEND_LONG_FMT " bytes more data than requested (" ZEND_LONG_FMT " read, " ZEND_LONG_FMT " max) - excess data will be lost", - us->wrapper->classname, (zend_long)(didread - count), (zend_long)didread, (zend_long)count); - didread = count; - } - if (didread > 0) - memcpy(buf, Z_STRVAL(retval), didread); - } else if (call_result == FAILURE) { + if (call_result == FAILURE) { php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_READ " is not implemented!", us->wrapper->classname); + return -1; + } + + if (Z_TYPE(retval) == IS_FALSE) { + return -1; + } + + if (!try_convert_to_string(&retval)) { + return -1; + } + + didread = Z_STRLEN(retval); + if (didread > count) { + php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_READ " - read " ZEND_LONG_FMT " bytes more data than requested (" ZEND_LONG_FMT " read, " ZEND_LONG_FMT " max) - excess data will be lost", + us->wrapper->classname, (zend_long)(didread - count), (zend_long)didread, (zend_long)count); + didread = count; } + if (didread > 0) + memcpy(buf, Z_STRVAL(retval), didread); zval_ptr_dtor(&retval); ZVAL_UNDEF(&retval);