From 6569a876a3ce8031d1ff89e8a80a4727f89036cf Mon Sep 17 00:00:00 2001 From: Matthias Sohn Date: Thu, 19 May 2016 15:25:02 +0200 Subject: [PATCH] [findBugs] Prevent potential NPE in CloneCommand.init() File.listFiles() returns null if the File is not a directory, improve validation of directory and gitDir to fix this. Change-Id: I763d08835faf96a0beb8e706992df0908526bd2c Signed-off-by: Matthias Sohn --- .../org/eclipse/jgit/internal/JGitText.properties | 2 ++ .../src/org/eclipse/jgit/api/CloneCommand.java | 10 ++++++++++ .../src/org/eclipse/jgit/internal/JGitText.java | 2 ++ 3 files changed, 14 insertions(+) diff --git a/org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties b/org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties index f7d9374bd7..b71e48b526 100644 --- a/org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties +++ b/org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties @@ -314,6 +314,8 @@ indexFileIsTooLargeForJgit=Index file is too large for jgit indexSignatureIsInvalid=Index signature is invalid: {0} indexWriteException=Modified index could not be written initFailedBareRepoDifferentDirs=When initializing a bare repo with directory {0} and separate git-dir {1} specified both folders must point to the same location +initFailedDirIsNoDirectory=Cannot set directory to ''{0}'' which is not a directory +initFailedGitDirIsNoDirectory=Cannot set git-dir to ''{0}'' which is not a directory initFailedNonBareRepoSameDirs=When initializing a non-bare repo with directory {0} and separate git-dir {1} specified both folders should not point to the same location inMemoryBufferLimitExceeded=In-memory buffer limit exceeded inputDidntMatchLength=Input did not match supplied length. {0} bytes are missing. diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java index 2ac8729507..ff15fd00c6 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java @@ -155,6 +155,7 @@ public class CloneCommand extends TransportCommand { command.setBare(bare); if (directory == null && gitDir == null) directory = new File(u.getHumanishName(), Constants.DOT_GIT); + validateDirs(directory, gitDir, bare); if (directory != null && directory.exists() && directory.listFiles().length != 0) throw new JGitInternalException(MessageFormat.format( @@ -511,6 +512,15 @@ public class CloneCommand extends TransportCommand { private static void validateDirs(File directory, File gitDir, boolean bare) throws IllegalStateException { if (directory != null) { + if (directory.exists() && !directory.isDirectory()) { + throw new IllegalStateException(MessageFormat.format( + JGitText.get().initFailedDirIsNoDirectory, directory)); + } + if (gitDir != null && gitDir.exists() && !gitDir.isDirectory()) { + throw new IllegalStateException(MessageFormat.format( + JGitText.get().initFailedGitDirIsNoDirectory, + gitDir)); + } if (bare) { if (gitDir != null && !gitDir.equals(directory)) throw new IllegalStateException(MessageFormat.format( diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java index d07c001b60..7c101780a7 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java @@ -373,6 +373,8 @@ public class JGitText extends TranslationBundle { /***/ public String indexSignatureIsInvalid; /***/ public String indexWriteException; /***/ public String initFailedBareRepoDifferentDirs; + /***/ public String initFailedDirIsNoDirectory; + /***/ public String initFailedGitDirIsNoDirectory; /***/ public String initFailedNonBareRepoSameDirs; /***/ public String inMemoryBufferLimitExceeded; /***/ public String inputDidntMatchLength; -- 2.39.5