Skip to content

Commit

Permalink
util: do not crash on jar: relative URIs of less than 4 characters,…
Browse files Browse the repository at this point in the history
… avoid string creation
  • Loading branch information
carlosame committed Sep 23, 2024
1 parent b262f2a commit 77bb079
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
* Protocol Handler for the 'jar' protocol. This appears to have the format:
* jar:<URL for jar file>!<path in jar file>
*
* @author <a href="mailto:[email protected]">Thomas DeWeese</a>
* @author For later modifications, see Git history.
* <p>
* Original author: <a href="mailto:[email protected]">Thomas DeWeese</a>
* For later modifications, see Git history.
* </p>
* @version $Id$
*/
public class ParsedURLJarProtocolHandler extends ParsedURLDefaultProtocolHandler {
Expand All @@ -42,10 +44,8 @@ public ParsedURLJarProtocolHandler() {
// is an absolute URL.
@Override
public ParsedURLData parseURL(ParsedURL baseURL, String urlStr) {
String start = urlStr.substring(0, JAR.length() + 1).toLowerCase();

// urlStr is absolute...
if (start.equals(JAR + ":"))
// Check whether urlStr is absolute...
if (startsWithJarColon(urlStr))
return parseURL(urlStr);

// It's relative so base it off baseURL.
Expand All @@ -58,4 +58,11 @@ public ParsedURLData parseURL(ParsedURL baseURL, String urlStr) {
}
}

private boolean startsWithJarColon(String url) {
char c;
return url.length() > 3 && ((c = url.charAt(0)) == 'j' || c == 'J')
&& ((c = url.charAt(1)) == 'a' || c == 'A')
&& ((c = url.charAt(2)) == 'r' || c == 'R') && url.charAt(3) == ':';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ public void testJar() {
"jar:file:dir/file.jar!/p/a/t/c/h/new.svg");
runTest("jar:file:dir/file.jar!/p/a/t/h/init.svg", "../c/h/new.svg#foo",
"jar:file:dir/file.jar!/p/a/t/c/h/new.svg#foo");
runTest("jar:file:dir/file.jar!/p/a/t/h/init.svg", "#id",
"jar:file:dir/file.jar!/p/a/t/h/init.svg#id");
runTest("JAR:file:dir/file.jar!/p/a/t/h/init.svg", "#id",
"jar:file:dir/file.jar!/p/a/t/h/init.svg#id");
runTest("jaR:file:dir/file.jar!/p/a/t/h/init.svg", "#id",
"jar:file:dir/file.jar!/p/a/t/h/init.svg#id");
runTest("JAr:file:dir/file.jar!/p/a/t/h/init.svg", "#",
"jar:file:dir/file.jar!/p/a/t/h/init.svg");
}

@Test
Expand Down

0 comments on commit 77bb079

Please sign in to comment.