From d3849fc38c9f75ada6704a6f4240fce3c2a44c7b Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 15 Nov 2009 20:30:57 +0000 Subject: [PATCH] - Fixed bug #49936 (crash with ftp stream in php_stream_context_get_option()) (patch by Pierrick) --- ext/curl/streams.c | 2 +- ext/standard/ftp_fopen_wrapper.c | 6 +++--- ext/standard/http_fopen_wrapper.c | 2 +- ext/standard/tests/streams/bug49936.phpt | 21 +++++++++++++++++++++ main/streams/php_stream_context.h | 2 +- main/streams/streams.c | 24 ++++++++++-------------- main/streams/transports.c | 2 +- 7 files changed, 38 insertions(+), 21 deletions(-) create mode 100644 ext/standard/tests/streams/bug49936.phpt diff --git a/ext/curl/streams.c b/ext/curl/streams.c index 51d540dbe8..cb27598026 100644 --- a/ext/curl/streams.c +++ b/ext/curl/streams.c @@ -270,7 +270,7 @@ php_stream *php_curl_stream_opener(php_stream_wrapper *wrapper, char *filename, memset(curlstream, 0, sizeof(php_curl_stream)); stream = php_stream_alloc(&php_curl_stream_ops, curlstream, 0, mode); - php_stream_context_set(stream, context); + php_stream_context_set(stream, context TSRMLS_CC); curlstream->curl = curl_easy_init(); curlstream->multi = curl_multi_init(); diff --git a/ext/standard/ftp_fopen_wrapper.c b/ext/standard/ftp_fopen_wrapper.c index 68e9b069c7..e85673f48d 100644 --- a/ext/standard/ftp_fopen_wrapper.c +++ b/ext/standard/ftp_fopen_wrapper.c @@ -143,7 +143,7 @@ static php_stream *php_ftp_fopen_connect(php_stream_wrapper *wrapper, char *path goto connect_errexit; } - php_stream_context_set(stream, context); + php_stream_context_set(stream, context TSRMLS_CC); php_stream_notify_info(context, PHP_STREAM_NOTIFY_CONNECT, NULL, 0); /* Start talking to ftp server */ @@ -551,7 +551,7 @@ php_stream * php_stream_url_wrap_ftp(php_stream_wrapper *wrapper, char *path, ch goto errexit; } - php_stream_context_set(datastream, context); + php_stream_context_set(datastream, context TSRMLS_CC); php_stream_notify_progress_init(context, 0, file_size); if (use_ssl_on_data && (php_stream_xport_crypto_setup(datastream, @@ -716,7 +716,7 @@ php_stream * php_stream_ftp_opendir(php_stream_wrapper *wrapper, char *path, cha goto opendir_errexit; } - php_stream_context_set(datastream, context); + php_stream_context_set(datastream, context TSRMLS_CC); if (use_ssl_on_data && (php_stream_xport_crypto_setup(stream, STREAM_CRYPTO_METHOD_SSLv23_CLIENT, NULL TSRMLS_CC) < 0 || diff --git a/ext/standard/http_fopen_wrapper.c b/ext/standard/http_fopen_wrapper.c index ed9185a520..630976b90a 100644 --- a/ext/standard/http_fopen_wrapper.c +++ b/ext/standard/http_fopen_wrapper.c @@ -288,7 +288,7 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper, char *path, eol_detect = stream->flags & (PHP_STREAM_FLAG_DETECT_EOL | PHP_STREAM_FLAG_EOL_MAC); stream->flags &= ~(PHP_STREAM_FLAG_DETECT_EOL | PHP_STREAM_FLAG_EOL_MAC); - php_stream_context_set(stream, context); + php_stream_context_set(stream, context TSRMLS_CC); php_stream_notify_info(context, PHP_STREAM_NOTIFY_CONNECT, NULL, 0); diff --git a/ext/standard/tests/streams/bug49936.phpt b/ext/standard/tests/streams/bug49936.phpt new file mode 100644 index 0000000000..7b089a7889 --- /dev/null +++ b/ext/standard/tests/streams/bug49936.phpt @@ -0,0 +1,21 @@ +--TEST-- +Bug #49936 (crash with ftp stream in php_stream_context_get_option()) +--FILE-- + +--EXPECTF-- +Warning: opendir(): connect() failed: Connection refused in %s on line %d + +Warning: opendir(ftp://...@localhost/): failed to open dir: operation failed in %s on line %d +bool(false) + +Warning: opendir(): connect() failed: Connection refused in %s on line %d + +Warning: opendir(ftp://...@localhost/): failed to open dir: operation failed in %s on line %d +bool(false) diff --git a/main/streams/php_stream_context.h b/main/streams/php_stream_context.h index 316de2983e..79c2e53520 100644 --- a/main/streams/php_stream_context.h +++ b/main/streams/php_stream_context.h @@ -98,7 +98,7 @@ END_EXTERN_C() BEGIN_EXTERN_C() PHPAPI void php_stream_notification_notify(php_stream_context *context, int notifycode, int severity, char *xmsg, int xcode, size_t bytes_sofar, size_t bytes_max, void * ptr TSRMLS_DC); -PHPAPI php_stream_context *php_stream_context_set(php_stream *stream, php_stream_context *context); +PHPAPI php_stream_context *php_stream_context_set(php_stream *stream, php_stream_context *context TSRMLS_DC); END_EXTERN_C() #define php_stream_notify_info(context, code, xmsg, xcode) do { if ((context) && (context)->notifier) { \ diff --git a/main/streams/streams.c b/main/streams/streams.c index ca628a16f6..5990d79b0c 100755 --- a/main/streams/streams.c +++ b/main/streams/streams.c @@ -2422,22 +2422,9 @@ PHPAPI php_stream *_php_stream_open_wrapper_ex(char *path, char *mode, int optio php_stream_wrapper_log_error(wrapper, options ^ REPORT_ERRORS TSRMLS_CC, "wrapper does not support stream open"); } else { - /* refcount++ to make sure the context doesn't get destroyed - * if open() fails and stream is closed */ - if (context) { - zend_list_addref(context->rsrc_id); - } - stream = wrapper->wops->stream_opener(wrapper, path_to_open, implicit_mode, options ^ REPORT_ERRORS, opened_path, context STREAMS_REL_CC TSRMLS_CC); - - /* if open() succeeded and context was not used, do refcount-- - * XXX if a wrapper didn't actually use context (no way to know that) - * and open() failed, refcount will stay increased */ - if (context && stream && !stream->context) { - zend_list_delete(context->rsrc_id); - } } /* if the caller asked for a persistent stream but the wrapper did not @@ -2595,10 +2582,19 @@ PHPAPI php_stream *_php_stream_u_open_wrapper(zend_uchar type, zstr path, int pa /* }}} */ /* {{{ context API */ -PHPAPI php_stream_context *php_stream_context_set(php_stream *stream, php_stream_context *context) +PHPAPI php_stream_context *php_stream_context_set(php_stream *stream, php_stream_context *context TSRMLS_DC) { php_stream_context *oldcontext = stream->context; + stream->context = context; + + if (context) { + zend_list_addref(context->rsrc_id); + } + if (oldcontext) { + zend_list_delete(oldcontext->rsrc_id); + } + return oldcontext; } diff --git a/main/streams/transports.c b/main/streams/transports.c index 9aad836d20..bb295d7088 100644 --- a/main/streams/transports.c +++ b/main/streams/transports.c @@ -134,7 +134,7 @@ PHPAPI php_stream *_php_stream_xport_create(const char *name, long namelen, int context STREAMS_REL_CC TSRMLS_CC); if (stream) { - stream->context = context; + php_stream_context_set(stream, context TSRMLS_CC); if ((flags & STREAM_XPORT_SERVER) == 0) { /* client */ -- 2.40.0