Skip to content

Commit 94678d9

Browse files
authored
uri: Fix memory safety violations when assigning $errors by reference fails (#19628)
* uri: Fix double-free when assigning `$errors` by reference fails `ZEND_TRY_ASSIGN_REF_ARR()` apparently consumes the to-be-assigned value even when it fails. * uri: Fix leak of parsed URI when assigning soft errors by reference fails This is not reproducible, because the URI object will still be referenced by Lexbor’s mraw instance and then cleanly destroyed at the end of the request. * NEWS
1 parent 097963f commit 94678d9

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ PHP NEWS
2323
. Fixed memory management of Uri\WhatWg\Url objects. (timwolla)
2424
. Fixed memory management of the internal "parse_url" URI parser.
2525
(timwolla)
26+
. Fixed double-free when assigning to $errors fails when using
27+
the Uri\WhatWg\Url parser. (timwolla)
2628
. Clean up naming of internal API. (timwolla)
2729

2830
28 Aug 2025, PHP 8.5.0beta2

ext/uri/php_uri.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,6 @@ static zend_result pass_errors_by_ref_and_free(zval *errors_zv, zval *errors)
325325

326326
ZEND_TRY_ASSIGN_REF_ARR(errors_zv, Z_ARRVAL_P(errors));
327327
if (EG(exception)) {
328-
zval_ptr_dtor(errors);
329328
return FAILURE;
330329
}
331330

@@ -360,6 +359,7 @@ ZEND_ATTRIBUTE_NONNULL_ARGS(1, 2) PHPAPI void php_uri_instantiate_uri(
360359
}
361360

362361
if (pass_errors_by_ref_and_free(errors_zv, &errors) == FAILURE) {
362+
uri_parser->free_uri(uri);
363363
RETURN_THROWS();
364364
}
365365

ext/uri/tests/057.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Test assigning errors by reference fails
3+
--EXTENSIONS--
4+
uri
5+
--FILE--
6+
<?php
7+
8+
class Foo {
9+
public string $x = '';
10+
}
11+
12+
$f = new Foo();
13+
try {
14+
Uri\WhatWg\Url::parse(" https://example.org ", errors: $f->x);
15+
} catch (Throwable $e) {
16+
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
17+
}
18+
19+
?>
20+
--EXPECT--
21+
TypeError: Cannot assign array to reference held by property Foo::$x of type string

0 commit comments

Comments
 (0)