Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ext/filter/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,10 @@ static void php_zval_filter(zval *value, zend_long filter, zend_long flags, zval
filter_func.name,
ZSTR_VAL(copy_for_throwing)
);
zend_string_delref(copy_for_throwing);
zend_string_release(copy_for_throwing);
return;
}
zend_string_delref(copy_for_throwing);
zend_string_release(copy_for_throwing);
copy_for_throwing = NULL;
}

Expand Down
37 changes: 37 additions & 0 deletions ext/filter/tests/filter_throw_on_failure_leak.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
filter: FILTER_THROW_ON_FAILURE does not leak the preserved input string
--EXTENSIONS--
filter
--FILE--
<?php
// php_zval_filter() copies the input string so it can be quoted in the
// exception message. A non-string scalar input (here a float / a large int)
// is turned into a fresh heap string by convert_to_string(), so the copy is
// the sole extra owner. Releasing it with zend_string_delref() decremented
// without freeing, leaking one string per call on both the failure and the
// success path. Loop and assert memory stays flat.
function leakcheck(callable $fn): bool {
$fn();
$before = memory_get_usage();
for ($i = 0; $i < 2000; $i++) {
$fn();
}
return memory_get_usage() - $before === 0;
}

// Validation fails -> exception thrown.
var_dump(leakcheck(function () {
try {
filter_var(1.5, FILTER_VALIDATE_INT, ['flags' => FILTER_THROW_ON_FAILURE]);
} catch (\Filter\FilterFailedException $e) {
}
}));

// Validation succeeds.
var_dump(leakcheck(function () {
filter_var(15, FILTER_VALIDATE_INT, ['flags' => FILTER_THROW_ON_FAILURE]);
}));
?>
--EXPECT--
bool(true)
bool(true)
Loading