Browse Source

[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 <matthias.sohn@sap.com>
stable-4.5
Matthias Sohn 9 years ago committed by Christian Halstrick
parent
commit
6569a876a3
  1. 2
      org.eclipse.jgit/resources/org/eclipse/jgit/internal/JGitText.properties
  2. 10
      org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java
  3. 2
      org.eclipse.jgit/src/org/eclipse/jgit/internal/JGitText.java

2
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.

10
org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java

@ -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(

2
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;

Loading…
Cancel
Save