Skip to content

Commit adc7477

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 7cf5b10 commit adc7477

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
@@ -3193,6 +3193,7 @@ private int parseAuthority(int start, int n)
31933193

31943194
boolean serverChars;
31953195
boolean regChars;
3196+
boolean skipParseException;
31963197

31973198
if (scan(p, n, "]") > p) {
31983199
// contains a literal IPv6 address, therefore % is allowed
@@ -3208,15 +3209,28 @@ private int parseAuthority(int start, int n)
32083209
return n;
32093210
}
32103211

3212+
// When parsing a URI, skip creating exception objects if the server-based
3213+
// authority is not required and the registry parse is successful.
3214+
//
3215+
skipParseException = (!requireServerAuthority && regChars);
32113216
if (serverChars) {
32123217
// Might be (probably is) a server-based authority, so attempt
32133218
// to parse it as such. If the attempt fails, try to treat it
32143219
// as a registry-based authority.
32153220
try {
3216-
q = parseServer(p, n);
3217-
if (q < n)
3218-
failExpecting("end of authority", q);
3219-
authority = input.substring(p, n);
3221+
q = parseServer(p, n, skipParseException);
3222+
if (q < n) {
3223+
if (skipParseException) {
3224+
userInfo = null;
3225+
host = null;
3226+
port = -1;
3227+
q = p;
3228+
} else {
3229+
failExpecting("end of authority", q);
3230+
}
3231+
} else {
3232+
authority = input.substring(p, n);
3233+
}
32203234
} catch (URISyntaxException x) {
32213235
// Undo results of failed parse
32223236
userInfo = null;
@@ -3254,7 +3268,7 @@ private int parseAuthority(int start, int n)
32543268

32553269
// [<userinfo>@]<host>[:<port>]
32563270
//
3257-
private int parseServer(int start, int n)
3271+
private int parseServer(int start, int n, boolean skipParseException)
32583272
throws URISyntaxException
32593273
{
32603274
int p = start;
@@ -3294,7 +3308,7 @@ private int parseServer(int start, int n)
32943308
} else {
32953309
q = parseIPv4Address(p, n);
32963310
if (q <= p)
3297-
q = parseHostname(p, n);
3311+
q = parseHostname(p, n, skipParseException);
32983312
p = q;
32993313
}
33003314

@@ -3311,7 +3325,10 @@ private int parseServer(int start, int n)
33113325
}
33123326
p = q;
33133327
}
3328+
} else if (p < n && skipParseException) {
3329+
return p;
33143330
}
3331+
33153332
if (p < n)
33163333
failExpecting("port number", p);
33173334

@@ -3416,7 +3433,7 @@ private int parseIPv4Address(int start, int n) {
34163433
// domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
34173434
// toplabel = alpha | alpha *( alphanum | "-" ) alphanum
34183435
//
3419-
private int parseHostname(int start, int n)
3436+
private int parseHostname(int start, int n, boolean skipParseException)
34203437
throws URISyntaxException
34213438
{
34223439
int p = start;
@@ -3444,9 +3461,12 @@ private int parseHostname(int start, int n)
34443461
p = q;
34453462
} while (p < n);
34463463

3447-
if ((p < n) && !at(p, n, ':'))
3464+
if ((p < n) && !at(p, n, ':')) {
3465+
if (skipParseException) {
3466+
return p;
3467+
}
34483468
fail("Illegal character in hostname", p);
3449-
3469+
}
34503470
if (l < 0)
34513471
failExpecting("hostname", start);
34523472

0 commit comments

Comments
 (0)