Skip to content

Commit bf4e659

Browse files
rvansaPaul Hohensee
authored and
Paul Hohensee
committed
8272702: Resolving URI relative path with no / may lead to incorrect toString
Reviewed-by: phh Backport-of: 79597f1ea6844f374beeeba219719cd9d5fe7d03
1 parent da62b2b commit bf4e659

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -2135,10 +2135,12 @@ private static String resolvePath(String base, String child,
21352135
path = base.substring(0, i + 1);
21362136
} else {
21372137
StringBuilder sb = new StringBuilder(base.length() + cn);
2138-
// 5.2 (6a)
2139-
if (i >= 0)
2138+
// 5.2 (6a-b)
2139+
if (i >= 0 || !absolute) {
21402140
sb.append(base, 0, i + 1);
2141-
// 5.2 (6b)
2141+
} else {
2142+
sb.append('/');
2143+
}
21422144
sb.append(child);
21432145
path = sb.toString();
21442146
}

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

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2022, 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 6933879
27+
* 7171415 6339649 6933879 8037396 8272072
2828
* @author Mark Reinhold
2929
*/
3030

@@ -1364,6 +1364,7 @@ static void eq(URI u, URI v) throws URISyntaxException {
13641364
}
13651365

13661366
static void eq(String expected, String actual) {
1367+
testCount++;
13671368
if (expected == null && actual == null) {
13681369
return;
13691370
}
@@ -1612,9 +1613,11 @@ static void clargs(String base, String uri) {
16121613
// miscellaneous bugs/rfes that don't fit in with the test framework
16131614

16141615
static void bugs() {
1616+
header("Bugs");
16151617
b6339649();
16161618
b6933879();
16171619
b8037396();
1620+
b8272072();
16181621
}
16191622

16201623
// 6339649 - include detail message from nested exception
@@ -1626,6 +1629,7 @@ private static void b6339649() {
16261629
throw new RuntimeException ("No detail message");
16271630
}
16281631
}
1632+
testCount++;
16291633
}
16301634

16311635
// 6933879 - check that "." and "_" characters are allowed in IPv6 scope_id.
@@ -1673,6 +1677,24 @@ private static void b8037396() {
16731677
eq("a%20b[c%20d]", u.getRawFragment());
16741678
}
16751679

1680+
// 8272072 - Resolving URI relative path with no "/" may lead to incorrect toString
1681+
private static void b8272072() {
1682+
try {
1683+
URI baseURI = new URI("http://example.com");
1684+
URI relativeURI = new URI("test");
1685+
URI resolvedURI = baseURI.resolve(relativeURI);
1686+
1687+
eq(new URI("http://example.com/test"), resolvedURI);
1688+
1689+
baseURI = new URI("relativeBase");
1690+
resolvedURI = baseURI.resolve(relativeURI);
1691+
1692+
eq(new URI("test"), resolvedURI);
1693+
} catch (URISyntaxException e) {
1694+
throw new AssertionError("shouldn't ever happen", e);
1695+
}
1696+
}
1697+
16761698
public static void main(String[] args) throws Exception {
16771699
switch (args.length) {
16781700

0 commit comments

Comments
 (0)