Skip to content

Commit

Permalink
8305763: Parsing a URI with an underscore goes through a silent excep…
Browse files Browse the repository at this point in the history
…tion, negatively impacting performance

Backport-of: 749d4801937ac145f945765f0ba0980bbccf384f
  • Loading branch information
Dhamoder Nalla authored and RealCLanger committed Aug 1, 2023
1 parent 7cf5b10 commit adc7477
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions src/java.base/share/classes/java/net/URI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3193,6 +3193,7 @@ private int parseAuthority(int start, int n)

boolean serverChars;
boolean regChars;
boolean skipParseException;

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

// When parsing a URI, skip creating exception objects if the server-based
// authority is not required and the registry parse is successful.
//
skipParseException = (!requireServerAuthority && regChars);
if (serverChars) {
// Might be (probably is) a server-based authority, so attempt
// to parse it as such. If the attempt fails, try to treat it
// as a registry-based authority.
try {
q = parseServer(p, n);
if (q < n)
failExpecting("end of authority", q);
authority = input.substring(p, n);
q = parseServer(p, n, skipParseException);
if (q < n) {
if (skipParseException) {
userInfo = null;
host = null;
port = -1;
q = p;
} else {
failExpecting("end of authority", q);
}
} else {
authority = input.substring(p, n);
}
} catch (URISyntaxException x) {
// Undo results of failed parse
userInfo = null;
Expand Down Expand Up @@ -3254,7 +3268,7 @@ private int parseAuthority(int start, int n)

// [<userinfo>@]<host>[:<port>]
//
private int parseServer(int start, int n)
private int parseServer(int start, int n, boolean skipParseException)
throws URISyntaxException
{
int p = start;
Expand Down Expand Up @@ -3294,7 +3308,7 @@ private int parseServer(int start, int n)
} else {
q = parseIPv4Address(p, n);
if (q <= p)
q = parseHostname(p, n);
q = parseHostname(p, n, skipParseException);
p = q;
}

Expand All @@ -3311,7 +3325,10 @@ private int parseServer(int start, int n)
}
p = q;
}
} else if (p < n && skipParseException) {
return p;
}

if (p < n)
failExpecting("port number", p);

Expand Down Expand Up @@ -3416,7 +3433,7 @@ private int parseIPv4Address(int start, int n) {
// domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
// toplabel = alpha | alpha *( alphanum | "-" ) alphanum
//
private int parseHostname(int start, int n)
private int parseHostname(int start, int n, boolean skipParseException)
throws URISyntaxException
{
int p = start;
Expand Down Expand Up @@ -3444,9 +3461,12 @@ private int parseHostname(int start, int n)
p = q;
} while (p < n);

if ((p < n) && !at(p, n, ':'))
if ((p < n) && !at(p, n, ':')) {
if (skipParseException) {
return p;
}
fail("Illegal character in hostname", p);

}
if (l < 0)
failExpecting("hostname", start);

Expand Down

0 comments on commit adc7477

Please sign in to comment.