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
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ PHP NEWS
. Fixed bug GH-22265 (Another tailcall vm_interrupt bug). (Levi Morrison)
. Fixed bug GH-20469 (Unsafe inheritance cache replay with reentrant
autoloading). (Levi Morrison)
. Fixed bug GH-21972 (Corrupted variable type when a typed by-value return
contains a reference wrapper). (Weilin Du)

- Phar:
. Fixed a bypass of the magic ".phar" directory protection in
Expand Down
5 changes: 4 additions & 1 deletion Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -4454,7 +4454,7 @@ ZEND_VM_COLD_CONST_HANDLER(124, ZEND_VERIFY_RETURN_TYPE, CONST|TMP|VAR|UNUSED|CV
ZVAL_DEREF(retval_ptr);
}

if (EXPECTED(ZEND_TYPE_CONTAINS_CODE(ret_info->type, Z_TYPE_P(retval_ptr)))) {
if (EXPECTED(ZEND_TYPE_CONTAINS_CODE(ret_info->type, Z_TYPE_P(retval_ref)))) {
ZEND_VM_NEXT_OPCODE();
}

Expand Down Expand Up @@ -4483,6 +4483,9 @@ ZEND_VM_COLD_CONST_HANDLER(124, ZEND_VERIFY_RETURN_TYPE, CONST|TMP|VAR|UNUSED|CV
}
retval_ptr = retval_ref;
}
if (EXPECTED(ZEND_TYPE_CONTAINS_CODE(ret_info->type, Z_TYPE_P(retval_ptr)))) {
ZEND_VM_NEXT_OPCODE();
}
}

SAVE_OPLINE();
Expand Down
25 changes: 20 additions & 5 deletions Zend/zend_vm_execute.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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)
39 changes: 39 additions & 0 deletions ext/opcache/tests/opt/gh21972.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
GH-21972: Typed by-value return must not leak reference wrapper
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.optimization_level=-1
--EXTENSIONS--
opcache
--FILE--
<?php
declare(strict_types=1);

enum ValueType {
case BOOL;
case MIXED;
}

function applyDefinition(
bool &$lazy = false,
ValueType &$type = ValueType::MIXED,
int &$flags = 0,
?string &$default = null,
): void {
}

function getTypedValue(string $default, bool $lazy, ValueType $type): string {
applyDefinition($lazy, $type, default: $default);
return $default;
}

$value = getTypedValue('false', false, ValueType::BOOL);
var_dump(gettype($value));
var_dump(strtolower($value));
var_dump(strtolower(getTypedValue('FALSE', false, ValueType::BOOL)));
?>
--EXPECT--
string(6) "string"
string(5) "false"
string(5) "false"