diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FSTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FSTest.java index 171d80c3d..51236e1cc 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FSTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/FSTest.java @@ -13,6 +13,7 @@ import static java.time.Instant.EPOCH; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeNoException; import static org.junit.Assume.assumeTrue; @@ -233,4 +234,26 @@ public void testRepoCacheRelativePathUnbornRepo() { assertFalse(RepositoryCache.FileKey .isGitRepository(new File("repo.git"), FS.DETECTED)); } + + @Test + public void testSearchPath() throws IOException { + File f1 = new File(trash, "file1"); + FileUtils.createNewFile(f1); + f1.setExecutable(true); + File f2 = new File(trash, "file2"); + FileUtils.createNewFile(f2); + assertEquals(f1, FS.searchPath(trash.getAbsolutePath(), "file1")); + assertNull(FS.searchPath(trash.getAbsolutePath(), "file2")); + } + + @Test + public void testSearchPathEmptyPath() { + assertNull(FS.searchPath("", "file1")); + assertNull(FS.searchPath(File.pathSeparator, "file1")); + assertNull(FS.searchPath(File.pathSeparator + File.pathSeparator, + "file1")); + assertNull(FS.searchPath( + " " + File.pathSeparator + " " + File.pathSeparator + " \t", + "file1")); + } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java index eadd34c3d..203669eb7 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java @@ -97,6 +97,9 @@ public abstract class FS { private static final Pattern VERSION = Pattern .compile("\\s(\\d+)\\.(\\d+)\\.(\\d+)"); //$NON-NLS-1$ + private static final Pattern EMPTY_PATH = Pattern + .compile("^[\\p{javaWhitespace}" + File.pathSeparator + "]*$"); //$NON-NLS-1$ //$NON-NLS-2$ + private volatile Boolean supportSymlinks; /** @@ -1310,8 +1313,10 @@ private File defaultUserHomeImpl() { * @return the first match found, or null * @since 3.0 */ + @SuppressWarnings("StringSplitter") protected static File searchPath(String path, String... lookFor) { - if (path == null) { + if (StringUtils.isEmptyOrNull(path) + || EMPTY_PATH.matcher(path).find()) { return null; }