Skip to content

Commit

Permalink
Fix memory leak when user filter onCreate returns false
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Nov 12, 2020
1 parent 3d2819e commit 2875d0f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
24 changes: 24 additions & 0 deletions ext/standard/tests/filters/php_user_filter_onCreate_failure.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
php_user_filter onCreate() returns false
--FILE--
<?php

class my_filter extends php_user_filter {
function onCreate() {
return false;
}
}

stream_filter_register("my_filter", "my_filter");
$fp = fopen('php://memory', 'rw');
var_dump(stream_filter_append($fp, "my_filter"));
fwrite($fp, "Test");
fseek($fp, 0);
var_dump(fgets($fp));
fclose($fp);

?>
--EXPECTF--
Warning: stream_filter_append(): Unable to create or locate filter "my_filter" in %s on line %d
bool(false)
string(4) "Test"
3 changes: 2 additions & 1 deletion ext/standard/user_filters.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ static php_stream_filter *user_filter_factory_create(const char *filtername,
&retval,
0, NULL);

zval_ptr_dtor(&func_name);

if (Z_TYPE(retval) != IS_UNDEF) {
if (Z_TYPE(retval) == IS_FALSE) {
/* User reported filter creation error "return false;" */
Expand All @@ -359,7 +361,6 @@ static php_stream_filter *user_filter_factory_create(const char *filtername,
}
zval_ptr_dtor(&retval);
}
zval_ptr_dtor(&func_name);

/* set the filter property, this will be used during cleanup */
ZVAL_RES(&zfilter, zend_register_resource(filter, le_userfilters));
Expand Down

0 comments on commit 2875d0f

Please sign in to comment.