Skip to content

Commit 99db196

Browse files
committed
Plug leak (context options not freed)
Make contexts auto-registered, ensures userland contexts and C API contexts are both dealt with on request shutdown. Also brings contexts in keeping with streams which are already auto-registered.
1 parent 785bc24 commit 99db196

File tree

5 files changed

+8
-7
lines changed

5 files changed

+8
-7
lines changed

ext/standard/basic_functions.c

-5
Original file line numberDiff line numberDiff line change
@@ -1247,11 +1247,6 @@ PHP_RSHUTDOWN_FUNCTION(basic)
12471247
BG(user_filter_map) = NULL;
12481248
}
12491249

1250-
/* cleanup any default context that was created */
1251-
if (FG(default_context)) {
1252-
php_stream_context_free(FG(default_context));
1253-
}
1254-
12551250
return SUCCESS;
12561251
}
12571252

ext/standard/file.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ PHPAPI int php_le_stream_context(void)
129129

130130
static ZEND_RSRC_DTOR_FUNC(file_context_dtor)
131131
{
132-
php_stream_context_free((php_stream_context*)rsrc->ptr);
132+
php_stream_context *context = (php_stream_context*)rsrc->ptr;
133+
zval_dtor(context->options);
134+
php_stream_context_free(context);
133135
}
134136

135137
static void file_globals_ctor(php_file_globals *file_globals_p TSRMLS_DC)

ext/standard/streamsfuncs.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ PHP_FUNCTION(stream_context_create)
802802
parse_context_options(context, params);
803803
}
804804

805-
ZEND_REGISTER_RESOURCE(return_value, context, php_le_stream_context());
805+
php_stream_context_to_zval(context, return_value);
806806
}
807807
/* }}} */
808808

main/streams/php_stream_context.h

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ typedef void (*php_stream_notification_func)(php_stream_context *context,
3838
FG(default_context) ? FG(default_context) : \
3939
(FG(default_context) = php_stream_context_alloc()) )
4040

41+
#define php_stream_context_to_zval(context, zval) { ZVAL_RESOURCE(zval, (context)->rsrc_id); }
42+
4143
typedef struct _php_stream_notifier {
4244
php_stream_notification_func func;
4345
void *ptr;
@@ -48,6 +50,7 @@ typedef struct _php_stream_notifier {
4850
struct _php_stream_context {
4951
php_stream_notifier *notifier;
5052
zval *options; /* hash keyed by wrapper family or specific wrapper */
53+
int rsrc_id; /* used for auto-cleanup */
5154
};
5255

5356
PHPAPI void php_stream_context_free(php_stream_context *context);

main/streams/streams.c

+1
Original file line numberDiff line numberDiff line change
@@ -1630,6 +1630,7 @@ PHPAPI php_stream_context *php_stream_context_alloc(void)
16301630
MAKE_STD_ZVAL(context->options);
16311631
array_init(context->options);
16321632

1633+
context->rsrc_id = ZEND_REGISTER_RESOURCE(NULL, context, php_le_stream_context());
16331634
return context;
16341635
}
16351636

0 commit comments

Comments
 (0)