Skip to content

Commit 84458ec

Browse files
Rohitash KumarMichael-Mc-Mahon
authored andcommitted
8353013: java.net.URI.create(String) may have low performance to scan the host/domain name from URI string when the hostname starts with number
Reviewed-by: michaelm, xpeng
1 parent c3de94c commit 84458ec

File tree

3 files changed

+112
-3
lines changed

3 files changed

+112
-3
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -3426,6 +3426,19 @@ private int scanByte(int start, int n)
34263426
int p = start;
34273427
int q = scan(p, n, L_DIGIT, H_DIGIT);
34283428
if (q <= p) return q;
3429+
3430+
// Handle leading zeros
3431+
int i = p, j;
3432+
while ((j = scan(i, q, '0')) > i) i = j;
3433+
3434+
// Calculate the number of significant digits (after leading zeros)
3435+
int significantDigitsNum = q - i;
3436+
3437+
if (significantDigitsNum < 3) return q; // definitely < 255
3438+
3439+
// If more than 3 significant digits, it's definitely > 255
3440+
if (significantDigitsNum > 3) return p;
3441+
34293442
if (Integer.parseInt(input, p, q, 10) > 255) return p;
34303443
return q;
34313444
}

test/jdk/java/net/URI/Test.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,7 +24,7 @@
2424
/* @test
2525
* @summary Unit test for java.net.URI
2626
* @bug 4464135 4505046 4503239 4438319 4991359 4866303 7023363 7041800
27-
* 7171415 6339649 6933879 8037396 8272072 8051627 8297687
27+
* 7171415 6339649 6933879 8037396 8272072 8051627 8297687 8353013
2828
* @author Mark Reinhold
2929
*/
3030

@@ -1620,6 +1620,7 @@ static void bugs() {
16201620
b8051627();
16211621
b8272072();
16221622
b8297687();
1623+
b8353013();
16231624
}
16241625

16251626
private static void b8297687() {
@@ -1786,6 +1787,39 @@ private static void b8272072() {
17861787
}
17871788
}
17881789

1790+
// 8353013 - Increase test coverage for cases where the authority component of a hierarchical
1791+
// URI has a host component that starts with a number.
1792+
private static void b8353013() {
1793+
testCreate("https://0.0.0.1").s("https").h("0.0.0.1").p("").z();
1794+
testCreate("https://00.0.0.2").s("https").h("00.0.0.2").p("").z();
1795+
testCreate("https://000.0.0.3").s("https").h("000.0.0.3").p("").z();
1796+
testCreate("https://0000.0.0.4").s("https").h("0000.0.0.4").p("").z();
1797+
1798+
testCreate("https://00000.0.0.5").s("https").h("00000.0.0.5").p("").z();
1799+
testCreate("https://00001.0.0.6").s("https").h("00001.0.0.6").p("").z();
1800+
1801+
testCreate("https://01.0.0.1").s("https").h("01.0.0.1").p("").z();
1802+
1803+
testCreate("https://111111.2.3.com").s("https").h("111111.2.3.com").p("").z();
1804+
1805+
testCreate("https://1.example.com").s("https").h("1.example.com").p("").z();
1806+
testCreate("https://12.example.com").s("https").h("12.example.com").p("").z();
1807+
testCreate("https://123.example.com").s("https").h("123.example.com").p("").z();
1808+
testCreate("https://1234.example.com").s("https").h("1234.example.com").p("").z();
1809+
testCreate("https://12345.example.com").s("https").h("12345.example.com").p("").z();
1810+
1811+
testCreate("https://98765432101.example.com").s("https").h("98765432101.example.com").p("").z();
1812+
testCreate("https://98765432101.www.example.com/").s("https").h("98765432101.www.example.com").p("/").z();
1813+
testCreate("https://98765432101.www.example.com").s("https").h("98765432101.www.example.com").p("").z();
1814+
1815+
testCreate("https://9223372036854775808.example.com").s("https").h("9223372036854775808.example.com").p("").z();
1816+
testCreate("https://9223372036854775808.www.example.com").s("https").h("9223372036854775808.www.example.com").p("").z();
1817+
testCreate("https://9223372036854775808.xyz.abc.com").s("https").h("9223372036854775808.xyz.abc.com").p("").z();
1818+
testCreate("https://9223372036854775808.xyz.abc.pqr.com").s("https").h("9223372036854775808.xyz.abc.pqr.com").p("").z();
1819+
1820+
testCreate("https://256.example.com").s("https").h("256.example.com").p("").z();
1821+
}
1822+
17891823
public static void main(String[] args) throws Exception {
17901824
switch (args.length) {
17911825

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package org.openjdk.bench.java.net;
24+
25+
import org.openjdk.jmh.annotations.Fork;
26+
import org.openjdk.jmh.annotations.Measurement;
27+
import org.openjdk.jmh.annotations.Param;
28+
import org.openjdk.jmh.annotations.State;
29+
import org.openjdk.jmh.annotations.Scope;
30+
import org.openjdk.jmh.annotations.Benchmark;
31+
import org.openjdk.jmh.annotations.BenchmarkMode;
32+
import org.openjdk.jmh.annotations.Mode;
33+
import org.openjdk.jmh.annotations.OutputTimeUnit;
34+
import org.openjdk.jmh.annotations.Warmup;
35+
import org.openjdk.jmh.infra.Blackhole;
36+
37+
import java.net.URI;
38+
import java.util.concurrent.TimeUnit;
39+
40+
/**
41+
* Tests Java.net.URI.create performance on various URI types.
42+
*/
43+
@BenchmarkMode(Mode.AverageTime)
44+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
45+
@State(Scope.Benchmark)
46+
@Warmup(iterations = 5, time = 1)
47+
@Measurement(iterations = 5, time = 1)
48+
@Fork(value = 3)
49+
public class URIAuthorityParsingBenchmark {
50+
51+
@Param({
52+
"https://98765432101.abc.xyz.com",
53+
"https://ABCDEFGHIJK.abc.xyz.com"
54+
})
55+
private String uri;
56+
57+
@Benchmark
58+
public void create(Blackhole blackhole) {
59+
blackhole.consume(URI.create(uri));
60+
}
61+
62+
}

0 commit comments

Comments
 (0)