]> source.dussan.org Git - jgit.git/commitdiff
[findBugs] Prevent potential NPE in CloneCommand.init() 68/73168/3
authorMatthias Sohn <matthias.sohn@sap.com>
Thu, 19 May 2016 13:25:02 +0000 (15:25 +0200)
committerChristian Halstrick <christian.halstrick@sap.com>
Mon, 30 May 2016 14:59:48 +0000 (10:59 -0400)
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 <matthias.sohn@sap.com>
org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties
org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java

index f7d9374bd7fa6ca351cafe0faf0b877e54a33859..b71e48b52661fbc896cb30bf27748dadd8ab527c 100644 (file)
@@ -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.
index 2ac8729507a5f1c6fcc6e96dd3da95a7a0b6c27f..ff15fd00c6bdf4eae5dadd7ff6a91614ac0a1c74 100644 (file)
@@ -155,6 +155,7 @@ public class CloneCommand extends TransportCommand<CloneCommand, Git> {
                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<CloneCommand, Git> {
        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(
index d07c001b6081ee92a2cfd3c81ed6d4e2c99add26..7c101780a742a55b24b00abf5b36d60fb49fd8c4 100644 (file)
@@ -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;