Skip to content

Commit c67088d

Browse files
authored
ext/uri: Fix the behavior for $softErrors (#23740)
Uri\WhatWg\Url::__construct() and Uri\WhatWg\UrlBuilder::build() now set $softErrors to an empty array when URL processing throws an exception, instead of preserving its value.
1 parent fdea8a9 commit c67088d

7 files changed

Lines changed: 20 additions & 34 deletions

UPGRADING

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,12 @@ PHP 8.6 UPGRADE NOTES
318318
platform's key_t range instead of passing a truncated key to the operating
319319
system.
320320

321+
- URI:
322+
. Uri\WhatWg\Url::__construct() now sets $softErrors to an empty array when
323+
URL processing throws an exception, instead of preserving its value.
324+
URL validation errors remain available in the
325+
Uri\WhatWg\InvalidUrlException::$errors property.
326+
321327
- Zip:
322328
. ZipArchive::extractTo now raises a TypeError for the files argument if one
323329
or more of the entries is not a string.

ext/uri/php_uri.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,8 @@ ZEND_ATTRIBUTE_NONNULL_ARGS(1, 2) PHPAPI void php_uri_instantiate_uri(
387387
if (UNEXPECTED(uri == NULL)) {
388388
if (should_throw) {
389389
zval_ptr_dtor(&errors);
390+
ZVAL_EMPTY_ARRAY(&errors);
391+
pass_errors_by_ref_and_free(errors_zv, &errors);
390392
RETURN_THROWS();
391393
} else {
392394
if (pass_errors_by_ref_and_free(errors_zv, &errors) == FAILURE) {
@@ -1433,6 +1435,9 @@ PHP_METHOD(Uri_WhatWg_UrlBuilder, build)
14331435
soft_errors
14341436
);
14351437
if (lexbor_url == NULL) {
1438+
zval errors;
1439+
ZVAL_EMPTY_ARRAY(&errors);
1440+
pass_errors_by_ref_and_free(soft_errors, &errors);
14361441
RETURN_THROWS();
14371442
}
14381443

ext/uri/tests/whatwg/builder/build_error_normalized_empty_host_password.phpt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@ $builder = new Uri\WhatWg\UrlBuilder();
77
$builder->setScheme("foo");
88
$builder->setHost("\t\n");
99
$builder->setPassword("pass");
10-
$softErrors = ["unchanged"];
1110

1211
try {
13-
$builder->build(softErrors: $softErrors);
12+
$builder->build();
1413
} catch (Throwable $e) {
1514
echo $e::class, ': ', $e->getMessage(), "\n";
1615
var_dump($e->errors);
1716
}
1817

19-
var_dump($softErrors);
20-
2118
?>
2219
--EXPECTF--
2320
Uri\WhatWg\InvalidUrlException: The specified URL cannot have password
@@ -33,7 +30,3 @@ array(1) {
3330
bool(false)
3431
}
3532
}
36-
array(1) {
37-
[0]=>
38-
string(9) "unchanged"
39-
}

ext/uri/tests/whatwg/builder/build_error_normalized_empty_host_port.phpt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@ $builder = new Uri\WhatWg\UrlBuilder();
77
$builder->setScheme("foo");
88
$builder->setHost("\t\n");
99
$builder->setPort(123);
10-
$softErrors = ["unchanged"];
1110

1211
try {
13-
$builder->build(softErrors: $softErrors);
12+
$builder->build();
1413
} catch (Throwable $e) {
1514
echo $e::class, ': ', $e->getMessage(), "\n";
1615
var_dump($e->errors);
1716
}
1817

19-
var_dump($softErrors);
20-
2118
?>
2219
--EXPECTF--
2320
Uri\WhatWg\InvalidUrlException: The specified URL cannot have port
@@ -33,7 +30,3 @@ array(1) {
3330
bool(false)
3431
}
3532
}
36-
array(1) {
37-
[0]=>
38-
string(9) "unchanged"
39-
}

ext/uri/tests/whatwg/builder/build_error_normalized_empty_host_username.phpt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@ $builder = new Uri\WhatWg\UrlBuilder();
77
$builder->setScheme("foo");
88
$builder->setHost("\t\n");
99
$builder->setUsername("user");
10-
$softErrors = ["unchanged"];
1110

1211
try {
13-
$builder->build(softErrors: $softErrors);
12+
$builder->build();
1413
} catch (Throwable $e) {
1514
echo $e::class, ': ', $e->getMessage(), "\n";
1615
var_dump($e->errors);
1716
}
1817

19-
var_dump($softErrors);
20-
2118
?>
2219
--EXPECTF--
2320
Uri\WhatWg\InvalidUrlException: The specified URL cannot have username
@@ -33,7 +30,3 @@ array(1) {
3330
bool(false)
3431
}
3532
}
36-
array(1) {
37-
[0]=>
38-
string(9) "unchanged"
39-
}

ext/uri/tests/whatwg/builder/build_error_soft_errors_unchanged.phpt renamed to ext/uri/tests/whatwg/builder/build_error_soft_errors_reset.phpt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
--TEST--
2-
Test Uri\WhatWg\UrlBuilder::build() - error - leaves soft errors unchanged
2+
Test Uri\WhatWg\UrlBuilder::build() - error - clears soft errors when an exception is thrown
33
--FILE--
44
<?php
55

66
$builder = new Uri\WhatWg\UrlBuilder();
77
$builder->setScheme("ht\ttps");
88
$builder->setHost(null);
9-
$softErrors = ["unchanged"];
9+
$softErrors = ["previous error"];
1010

1111
try {
1212
$builder->build(softErrors: $softErrors);
@@ -40,7 +40,5 @@ array(2) {
4040
bool(true)
4141
}
4242
}
43-
array(1) {
44-
[0]=>
45-
string(9) "unchanged"
43+
array(0) {
4644
}

ext/uri/tests/whatwg/parsing/basic_error_soft_errors_unchanged.phpt renamed to ext/uri/tests/whatwg/parsing/basic_error_soft_errors_reset.phpt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
--TEST--
2-
Test Uri\WhatWg\Url::__construct() - error - leaves soft errors unchanged
2+
Test Uri\WhatWg\Url::__construct() - error - clears soft errors when an exception is thrown
33
--FILE--
44
<?php
55

6-
$softErrors = ["unchanged"];
6+
$softErrors = ["previous error"];
77

88
try {
99
new Uri\WhatWg\Url("🐘", softErrors: $softErrors);
@@ -28,7 +28,5 @@ array(1) {
2828
bool(true)
2929
}
3030
}
31-
array(1) {
32-
[0]=>
33-
string(9) "unchanged"
31+
array(0) {
3432
}

0 commit comments

Comments
 (0)