Skip to content

Commit 310115a

Browse files
Dhamoder NallaRealCLanger
authored andcommitted
8305763: Parsing a URI with an underscore goes through a silent exception, negatively impacting performance
Backport-of: 749d4801937ac145f945765f0ba0980bbccf384f
1 parent e61bb2e commit 310115a

File tree

1 file changed

+29
-9
lines changed
  • src/java.base/share/classes/java/net

1 file changed

+29
-9
lines changed

src/java.base/share/classes/java/net/URI.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3256,6 +3256,7 @@ private int parseAuthority(int start, int n)
32563256

32573257
boolean serverChars;
32583258
boolean regChars;
3259+
boolean skipParseException;
32593260

32603261
if (scan(p, n, "]") > p) {
32613262
// contains a literal IPv6 address, therefore % is allowed
@@ -3271,15 +3272,28 @@ private int parseAuthority(int start, int n)
32713272
return n;
32723273
}
32733274

3275+
// When parsing a URI, skip creating exception objects if the server-based
3276+
// authority is not required and the registry parse is successful.
3277+
//
3278+
skipParseException = (!requireServerAuthority && regChars);
32743279
if (serverChars) {
32753280
// Might be (probably is) a server-based authority, so attempt
32763281
// to parse it as such. If the attempt fails, try to treat it
32773282
// as a registry-based authority.
32783283
try {
3279-
q = parseServer(p, n);
3280-
if (q < n)
3281-
failExpecting("end of authority", q);
3282-
authority = input.substring(p, n);
3284+
q = parseServer(p, n, skipParseException);
3285+
if (q < n) {
3286+
if (skipParseException) {
3287+
userInfo = null;
3288+
host = null;
3289+
port = -1;
3290+
q = p;
3291+
} else {
3292+
failExpecting("end of authority", q);
3293+
}
3294+
} else {
3295+
authority = input.substring(p, n);
3296+
}
32833297
} catch (URISyntaxException x) {
32843298
// Undo results of failed parse
32853299
userInfo = null;
@@ -3317,7 +3331,7 @@ private int parseAuthority(int start, int n)
33173331

33183332
// [<userinfo>@]<host>[:<port>]
33193333
//
3320-
private int parseServer(int start, int n)
3334+
private int parseServer(int start, int n, boolean skipParseException)
33213335
throws URISyntaxException
33223336
{
33233337
int p = start;
@@ -3357,7 +3371,7 @@ private int parseServer(int start, int n)
33573371
} else {
33583372
q = parseIPv4Address(p, n);
33593373
if (q <= p)
3360-
q = parseHostname(p, n);
3374+
q = parseHostname(p, n, skipParseException);
33613375
p = q;
33623376
}
33633377

@@ -3374,7 +3388,10 @@ private int parseServer(int start, int n)
33743388
}
33753389
p = q;
33763390
}
3391+
} else if (p < n && skipParseException) {
3392+
return p;
33773393
}
3394+
33783395
if (p < n)
33793396
failExpecting("port number", p);
33803397

@@ -3479,7 +3496,7 @@ private int parseIPv4Address(int start, int n) {
34793496
// domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
34803497
// toplabel = alpha | alpha *( alphanum | "-" ) alphanum
34813498
//
3482-
private int parseHostname(int start, int n)
3499+
private int parseHostname(int start, int n, boolean skipParseException)
34833500
throws URISyntaxException
34843501
{
34853502
int p = start;
@@ -3507,9 +3524,12 @@ private int parseHostname(int start, int n)
35073524
p = q;
35083525
} while (p < n);
35093526

3510-
if ((p < n) && !at(p, n, ':'))
3527+
if ((p < n) && !at(p, n, ':')) {
3528+
if (skipParseException) {
3529+
return p;
3530+
}
35113531
fail("Illegal character in hostname", p);
3512-
3532+
}
35133533
if (l < 0)
35143534
failExpecting("hostname", start);
35153535

0 commit comments

Comments
 (0)