Skip to content
Permalink
Browse files
8242258: (jrtfs) Path::toUri throws AssertionError for malformed input
Reviewed-by: alanb
  • Loading branch information
sundararajana committed Dec 8, 2020
1 parent 52ab721 commit d2b66196b4509a32613f476d7247f207289a75e3
Showing with 213 additions and 12 deletions.
  1. +139 −12 src/java.base/share/classes/jdk/internal/jrtfs/JrtPath.java
  2. +74 −0 test/jdk/jdk/internal/jrtfs/UriTests.java
@@ -170,20 +170,16 @@ public final JrtPath toAbsolutePath() {

@Override
public final URI toUri() {
try {
String p = toAbsolutePath().path;
if (!p.startsWith("/modules") || p.contains("..")) {
throw new IOError(new RuntimeException(p + " cannot be represented as URI"));
}
String p = toAbsolutePath().path;
if (!p.startsWith("/modules") || p.contains("..")) {
throw new IOError(new RuntimeException(p + " cannot be represented as URI"));
}

p = p.substring("/modules".length());
if (p.isEmpty()) {
p = "/";
}
return new URI("jrt", p, null);
} catch (URISyntaxException ex) {
throw new AssertionError(ex);
p = p.substring("/modules".length());
if (p.isEmpty()) {
p = "/";
}
return toUri(p);
}

private boolean equalsNameAt(JrtPath other, int index) {
@@ -825,4 +821,135 @@ private void copyToTarget(JrtPath target, CopyOption... options)
}
}
}

// adopted from sun.nio.fs.UnixUriUtils
private static URI toUri(String str) {
char[] path = str.toCharArray();
assert path[0] == '/';
StringBuilder sb = new StringBuilder();
sb.append(path[0]);
for (int i = 1; i < path.length; i++) {
char c = (char)(path[i] & 0xff);
if (match(c, L_PATH, H_PATH)) {
sb.append(c);
} else {
sb.append('%');
sb.append(hexDigits[(c >> 4) & 0x0f]);
sb.append(hexDigits[(c) & 0x0f]);
}
}

try {
return new URI("jrt:" + sb.toString());
} catch (URISyntaxException x) {
throw new AssertionError(x); // should not happen
}
}

// The following is copied from java.net.URI

// Compute the low-order mask for the characters in the given string
private static long lowMask(String chars) {
int n = chars.length();
long m = 0;
for (int i = 0; i < n; i++) {
char c = chars.charAt(i);
if (c < 64)
m |= (1L << c);
}
return m;
}

// Compute the high-order mask for the characters in the given string
private static long highMask(String chars) {
int n = chars.length();
long m = 0;
for (int i = 0; i < n; i++) {
char c = chars.charAt(i);
if ((c >= 64) && (c < 128))
m |= (1L << (c - 64));
}
return m;
}

// Compute a low-order mask for the characters
// between first and last, inclusive
private static long lowMask(char first, char last) {
long m = 0;
int f = Math.max(Math.min(first, 63), 0);
int l = Math.max(Math.min(last, 63), 0);
for (int i = f; i <= l; i++)
m |= 1L << i;
return m;
}

// Compute a high-order mask for the characters
// between first and last, inclusive
private static long highMask(char first, char last) {
long m = 0;
int f = Math.max(Math.min(first, 127), 64) - 64;
int l = Math.max(Math.min(last, 127), 64) - 64;
for (int i = f; i <= l; i++)
m |= 1L << i;
return m;
}

// Tell whether the given character is permitted by the given mask pair
private static boolean match(char c, long lowMask, long highMask) {
if (c < 64)
return ((1L << c) & lowMask) != 0;
if (c < 128)
return ((1L << (c - 64)) & highMask) != 0;
return false;
}

// digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
// "8" | "9"
private static final long L_DIGIT = lowMask('0', '9');
private static final long H_DIGIT = 0L;

// upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" |
// "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" |
// "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"
private static final long L_UPALPHA = 0L;
private static final long H_UPALPHA = highMask('A', 'Z');

// lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" |
// "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" |
// "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
private static final long L_LOWALPHA = 0L;
private static final long H_LOWALPHA = highMask('a', 'z');

// alpha = lowalpha | upalpha
private static final long L_ALPHA = L_LOWALPHA | L_UPALPHA;
private static final long H_ALPHA = H_LOWALPHA | H_UPALPHA;

// alphanum = alpha | digit
private static final long L_ALPHANUM = L_DIGIT | L_ALPHA;
private static final long H_ALPHANUM = H_DIGIT | H_ALPHA;

// mark = "-" | "_" | "." | "!" | "~" | "*" | "'" |
// "(" | ")"
private static final long L_MARK = lowMask("-_.!~*'()");
private static final long H_MARK = highMask("-_.!~*'()");

// unreserved = alphanum | mark
private static final long L_UNRESERVED = L_ALPHANUM | L_MARK;
private static final long H_UNRESERVED = H_ALPHANUM | H_MARK;

// pchar = unreserved | escaped |
// ":" | "@" | "&" | "=" | "+" | "$" | ","
private static final long L_PCHAR
= L_UNRESERVED | lowMask(":@&=+$,");
private static final long H_PCHAR
= H_UNRESERVED | highMask(":@&=+$,");

// All valid path characters
private static final long L_PATH = L_PCHAR | lowMask(";/");
private static final long H_PATH = H_PCHAR | highMask(";/");

private static final char[] hexDigits = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
}
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2020, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8242258
* @summary (jrtfs) Path::toUri throws AssertionError for malformed input
* @run testng UriTests
*/

import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.net.URI;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;

public class UriTests {
private FileSystem theFileSystem;

@BeforeClass
public void setup() {
theFileSystem = FileSystems.getFileSystem(URI.create("jrt:/"));
}

@DataProvider(name = "problemStrings")
private Object[][] problemStrings() {
return new Object[][] {
{ "[", "jrt:/%5B" },
{ "]", "jrt:/%5D" },
{ "{", "jrt:/%7B" },
{ "}", "jrt:/%7D" },
{ "`", "jrt:/%60" },
{ "%", "jrt:/%25" },
{ " xyz", "jrt:/%20xyz" },
{ "xyz ", "jrt:/xyz%20" },
{ "xy z", "jrt:/xy%20z" },
};
}

@Test(dataProvider = "problemStrings")
public void testPathToURI(String pathSuffix, String uriStr) {
URI uri = theFileSystem.getPath("/modules/" + pathSuffix).toUri();
assertEquals(uri.toString(), uriStr);
}

@Test(dataProvider = "problemStrings")
public void testURIToPath(String pathSuffix, String uriStr) {
Path path = theFileSystem.provider().getPath(URI.create(uriStr));
assertEquals(path.toString(), "/modules/" + pathSuffix);
}
}

1 comment on commit d2b6619

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on d2b6619 Dec 8, 2020

Please sign in to comment.