Navigation Menu

Skip to content

Commit

Permalink
8272702: Resolving URI relative path with no / may lead to incorrect …
Browse files Browse the repository at this point in the history
…toString

Reviewed-by: phh
Backport-of: 79597f1ea6844f374beeeba219719cd9d5fe7d03
  • Loading branch information
rvansa authored and Paul Hohensee committed Feb 24, 2023
1 parent da62b2b commit bf4e659
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/java.base/share/classes/java/net/URI.java
Expand Up @@ -2135,10 +2135,12 @@ private static String resolvePath(String base, String child,
path = base.substring(0, i + 1);
} else {
StringBuilder sb = new StringBuilder(base.length() + cn);
// 5.2 (6a)
if (i >= 0)
// 5.2 (6a-b)
if (i >= 0 || !absolute) {
sb.append(base, 0, i + 1);
// 5.2 (6b)
} else {
sb.append('/');
}
sb.append(child);
path = sb.toString();
}
Expand Down
26 changes: 24 additions & 2 deletions test/jdk/java/net/URI/Test.java
@@ -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
Expand All @@ -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
*/

Expand Down Expand Up @@ -1364,6 +1364,7 @@ static void eq(URI u, URI v) throws URISyntaxException {
}

static void eq(String expected, String actual) {
testCount++;
if (expected == null && actual == null) {
return;
}
Expand Down Expand Up @@ -1612,9 +1613,11 @@ static void clargs(String base, String uri) {
// 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
Expand All @@ -1626,6 +1629,7 @@ private static void b6339649() {
throw new RuntimeException ("No detail message");
}
}
testCount++;
}

// 6933879 - check that "." and "_" characters are allowed in IPv6 scope_id.
Expand Down Expand Up @@ -1673,6 +1677,24 @@ private static void b8037396() {
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) {

Expand Down

1 comment on commit bf4e659

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.