Skip to content

Commit

Permalink
Updates after kkolinko review
Browse files Browse the repository at this point in the history
- Correct comment
- Use correct regular expression match (that makes regular expressions an even worse option)
- Improve (roughly x2) performance of invalid filename check

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1809684 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
markt-asf committed Sep 26, 2017
1 parent c177e96 commit e650cf1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
Expand Up @@ -139,11 +139,11 @@ private boolean isInvalidWindowsFilename(String name) {
if (name.length() == 0) {
return false;
}
// For typical length file names, this is 2-3 times faster than the
// equivalent regular expression. The cut-over point is file names (not
// full paths) of ~65 characters.
char[] chars = name.toCharArray();
for (char c : chars) {
// This consistently ~10 times faster than the equivalent regular
// expression irrespective of input length.
final int len = name.length();
for (int i = 0; i < len; i++) {
char c = name.charAt(i);
if (c == '\"' || c == '<' || c == '>') {
// These characters are disallowed in Windows file names and
// there are known problems for file names with these characters
Expand All @@ -154,11 +154,11 @@ private boolean isInvalidWindowsFilename(String name) {
return true;
}
}
// Windows does allow file names to end in ' ' unless specific low level
// APIs are used to create the files that bypass various checks. File
// names that end in ' ' are known to cause problems when using
// Windows does not allow file names to end in ' ' unless specific low
// level APIs are used to create the files that bypass various checks.
// File names that end in ' ' are known to cause problems when using
// File#getCanonicalPath().
if (chars[chars.length -1] == ' ') {
if (name.charAt(len -1) == ' ') {
return true;
}
return false;
Expand Down
Expand Up @@ -27,7 +27,7 @@ public class TestAbstractFileResourceSetPerformance {
private static final int LOOPS = 10_000_000;

/*
* Checking individual characters is about 3 times faster on markt's dev
* Checking individual characters is about 10 times faster on markt's dev
* PC for typical length file names. The file names need to get to ~65
* characters before the Pattern matching is faster.
*/
Expand All @@ -36,22 +36,31 @@ public void testFileNameFiltering() {

long start = System.nanoTime();
for (int i = 0; i < LOOPS; i++) {
UNSAFE_WINDOWS_FILENAME_PATTERN.matcher("testfile.jsp ").matches();
UNSAFE_WINDOWS_FILENAME_PATTERN.matcher("testfile.jsp ").find();
}
long end = System.nanoTime();
System.out.println("Regular expression took " + (end - start) + "ns or " +
(end-start)/LOOPS + "ns per iteration");

start = System.nanoTime();
for (int i = 0; i < LOOPS; i++) {
checkForBadChars("testfile.jsp ");
checkForBadCharsArray("testfile.jsp ");
}
end = System.nanoTime();
System.out.println("char[] check took " + (end - start) + "ns or " +
(end-start)/LOOPS + "ns per iteration");

start = System.nanoTime();
for (int i = 0; i < LOOPS; i++) {
checkForBadCharsAt("testfile.jsp ");
}
end = System.nanoTime();
System.out.println("charAt() check took " + (end - start) + "ns or " +
(end-start)/LOOPS + "ns per iteration");

}

private boolean checkForBadChars(String filename) {
private boolean checkForBadCharsArray(String filename) {
char[] chars = filename.toCharArray();
for (char c : chars) {
if (c == '\"' || c == '<' || c == '>') {
Expand All @@ -63,4 +72,19 @@ private boolean checkForBadChars(String filename) {
}
return true;
}


private boolean checkForBadCharsAt(String filename) {
final int len = filename.length();
for (int i = 0; i < len; i++) {
char c = filename.charAt(i);
if (c == '\"' || c == '<' || c == '>') {
return false;
}
}
if (filename.charAt(len - 1) == ' ') {
return false;
}
return true;
}
}

0 comments on commit e650cf1

Please sign in to comment.