Skip to content

Commit d3a14c8

Browse files
author
Sjoerd Langkemper
committed
Count number of filters already on the stream
Instead of counting iterations on the loop Related to: php#10453 php#16699
1 parent 7731252 commit d3a14c8

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

ext/standard/php_fopen_wrapper.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,21 @@ static void php_stream_apply_filter_list(php_stream *stream, char *filterlist, i
164164
php_url_decode(p, strlen(p));
165165
if (read_chain) {
166166
if ((temp_filter = php_stream_filter_create(p, NULL, php_stream_is_persistent(stream)))) {
167+
if (php_stream_filter_count(&stream->readfilters) > max_stream_filters) {
168+
zend_throw_exception_ex(NULL, 0, "Unable to apply read filter, maximum number (%d) reached", max_stream_filters);
169+
return;
170+
}
167171
php_stream_filter_append(&stream->readfilters, temp_filter);
168172
} else {
169173
php_error_docref(NULL, E_WARNING, "Unable to create filter (%s)", p);
170174
}
171175
}
172176
if (write_chain) {
173177
if ((temp_filter = php_stream_filter_create(p, NULL, php_stream_is_persistent(stream)))) {
178+
if (php_stream_filter_count(&stream->writefilters) > max_stream_filters) {
179+
zend_throw_exception_ex(NULL, 0, "Unable to apply write filter, maximum number (%d) reached", max_stream_filters);
180+
return;
181+
}
174182
php_stream_filter_append(&stream->writefilters, temp_filter);
175183
} else {
176184
php_error_docref(NULL, E_WARNING, "Unable to create filter (%s)", p);

main/streams/filter.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,20 @@ PHPAPI void _php_stream_filter_append(php_stream_filter_chain *chain, php_stream
447447
}
448448
}
449449

450+
PHPAPI int php_stream_filter_count(php_stream_filter_chain *chain) {
451+
if (chain->head == NULL) {
452+
return 0;
453+
}
454+
455+
int count = 1;
456+
php_stream_filter *node = chain->head;
457+
while (node != chain->tail) {
458+
count += 1;
459+
node = node->next;
460+
}
461+
return count;
462+
}
463+
450464
PHPAPI zend_result _php_stream_filter_flush(php_stream_filter *filter, bool finish)
451465
{
452466
php_stream_bucket_brigade brig_a = { NULL, NULL }, brig_b = { NULL, NULL }, *inp = &brig_a, *outp = &brig_b, *brig_temp;

main/streams/php_stream_filter_api.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ PHPAPI void _php_stream_filter_prepend(php_stream_filter_chain *chain, php_strea
138138
PHPAPI void php_stream_filter_prepend_ex(php_stream_filter_chain *chain, php_stream_filter *filter);
139139
PHPAPI void _php_stream_filter_append(php_stream_filter_chain *chain, php_stream_filter *filter);
140140
PHPAPI zend_result php_stream_filter_append_ex(php_stream_filter_chain *chain, php_stream_filter *filter);
141+
PHPAPI int php_stream_filter_count(php_stream_filter_chain *chain);
141142
PHPAPI zend_result _php_stream_filter_flush(php_stream_filter *filter, bool finish);
142143
PHPAPI php_stream_filter *php_stream_filter_remove(php_stream_filter *filter, bool call_dtor);
143144
PHPAPI void php_stream_filter_free(php_stream_filter *filter);

0 commit comments

Comments
 (0)