8272702: Resolving URI relative path with no / may lead to incorrect toString

Reviewed-by: dfuchs, michaelm
This commit is contained in:
KIRIYAMA Takuya 2022-08-22 09:22:59 +00:00 committed by Daniel Fuchs
parent 7b5f9edb59
commit 79597f1ea6
2 changed files with 32 additions and 9 deletions

View file

@ -2106,7 +2106,7 @@ public final class URI
// -- Normalization, resolution, and relativization --
// RFC2396 5.2 (6)
private static String resolvePath(String base, String child)
private static String resolvePath(String base, String child, boolean absolute)
{
int i = base.lastIndexOf('/');
int cn = child.length();
@ -2117,12 +2117,13 @@ public final class URI
if (i >= 0)
path = base.substring(0, i + 1);
} else {
// 5.2 (6a)
if (i >= 0)
// 5.2 (6a-b)
if (i >= 0 || !absolute) {
path = base.substring(0, i + 1).concat(child);
// 5.2 (6b)
else
path = child;
} else {
path = "/".concat(child);
}
}
// 5.2 (6c-f)
@ -2183,7 +2184,7 @@ public final class URI
ru.path = child.path;
} else {
// 5.2 (6): Resolve relative path
ru.path = resolvePath(base.path, cp);
ru.path = resolvePath(base.path, cp, base.isAbsolute());
}
} else {
ru.authority = child.authority;

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -24,7 +24,7 @@
/* @test
* @summary Unit test for java.net.URI
* @bug 4464135 4505046 4503239 4438319 4991359 4866303 7023363 7041800
* 7171415 6933879
* 7171415 6339649 6933879 8037396 8272072
* @author Mark Reinhold
*/
@ -1364,6 +1364,7 @@ public class Test {
}
static void eq(String expected, String actual) {
testCount++;
if (expected == null && actual == null) {
return;
}
@ -1612,9 +1613,11 @@ public class Test {
// miscellaneous bugs/rfes that don't fit in with the test framework
static void bugs() {
header("Bugs");
b6339649();
b6933879();
b8037396();
b8272072();
}
// 6339649 - include detail message from nested exception
@ -1626,6 +1629,7 @@ public class Test {
throw new RuntimeException ("No detail message");
}
}
testCount++;
}
// 6933879 - check that "." and "_" characters are allowed in IPv6 scope_id.
@ -1673,6 +1677,24 @@ public class Test {
eq("a%20b[c%20d]", u.getRawFragment());
}
// 8272072 - Resolving URI relative path with no "/" may lead to incorrect toString
private static void b8272072() {
try {
URI baseURI = new URI("http://example.com");
URI relativeURI = new URI("test");
URI resolvedURI = baseURI.resolve(relativeURI);
eq(new URI("http://example.com/test"), resolvedURI);
baseURI = new URI("relativeBase");
resolvedURI = baseURI.resolve(relativeURI);
eq(new URI("test"), resolvedURI);
} catch (URISyntaxException e) {
throw new AssertionError("shouldn't ever happen", e);
}
}
public static void main(String[] args) throws Exception {
switch (args.length) {