From 35f366273fe005bef246c84652b7f9c1206fecb0 Mon Sep 17 00:00:00 2001 From: Antony Dovgal Date: Wed, 6 Aug 2008 09:03:36 +0000 Subject: [PATCH] increase context refcount before open() to avoid crash when open() fails and destroys the context --- main/streams/streams.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/main/streams/streams.c b/main/streams/streams.c index f1b74a49d5a..3d7d251ee3d 100755 --- a/main/streams/streams.c +++ b/main/streams/streams.c @@ -2414,14 +2414,22 @@ 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); - } - /* increase context refcount only if the context is really used */ - if (stream && stream->context) { - zend_list_addref(stream->context->rsrc_id); + /* 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