Browse Source

DirCache: Refuse to read files with invalid paths

If the DirCache contains a path that is known to be invalid, refuse to
read the DirCache into memory.  This avoids confusing errors later if
an invalid path read from the DirCache were to be passed into a new
DirCacheEntry constructor.

Change-Id: Ic033d81e23a5fbd554cc4dff80a232504562ffa8
stable-3.4
Shawn Pearce 10 years ago committed by Matthias Sohn
parent
commit
8d36fa343c
  1. 44
      org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheBasicTest.java
  2. 11
      org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheEntry.java

44
org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheBasicTest.java vendored

@ -47,12 +47,19 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
import java.text.MessageFormat;
import org.eclipse.jgit.errors.CorruptObjectException;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.util.SystemReader;
import org.junit.Test;
public class DirCacheBasicTest extends RepositoryTestCase {
@ -234,4 +241,41 @@ public class DirCacheBasicTest extends RepositoryTestCase {
final byte[] path = Constants.encode("a");
assertEquals(-1, dc.findEntry(path, path.length));
}
@Test
public void testRejectInvalidWindowsPaths() throws Exception {
SystemReader.setInstance(new MockSystemReader() {
{
setUnix();
}
});
String path = "src/con.txt";
DirCache dc = db.lockDirCache();
DirCacheBuilder b = dc.builder();
DirCacheEntry e = new DirCacheEntry(path);
e.setFileMode(FileMode.REGULAR_FILE);
e.setObjectId(new ObjectInserter.Formatter().idFor(
Constants.OBJ_BLOB,
Constants.encode(path)));
b.add(e);
b.commit();
db.readDirCache();
SystemReader.setInstance(new MockSystemReader() {
{
setWindows();
}
});
try {
db.readDirCache();
fail("should have rejected " + path);
} catch (CorruptObjectException err) {
assertEquals(MessageFormat.format(JGitText.get().invalidPath, path),
err.getMessage());
assertNotNull(err.getCause());
assertEquals("invalid name 'CON'", err.getCause().getMessage());
}
}
}

11
org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheEntry.java vendored

@ -56,6 +56,7 @@ import java.security.MessageDigest;
import java.text.MessageFormat;
import java.util.Arrays;
import org.eclipse.jgit.errors.CorruptObjectException;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Constants;
@ -189,6 +190,16 @@ public class DirCacheEntry {
md.update((byte) 0);
}
try {
DirCacheCheckout.checkValidPath(toString(path));
} catch (InvalidPathException e) {
CorruptObjectException p =
new CorruptObjectException(e.getMessage());
if (e.getCause() != null)
p.initCause(e.getCause());
throw p;
}
// Index records are padded out to the next 8 byte alignment
// for historical reasons related to how C Git read the files.
//

Loading…
Cancel
Save