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
This commit is contained in:
Shawn Pearce 2014-11-25 11:43:32 -08:00 committed by Matthias Sohn
parent d547d0c44b
commit 8d36fa343c
2 changed files with 55 additions and 0 deletions

View File

@ -47,12 +47,19 @@
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 void testFindOnEmpty() throws Exception {
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());
}
}
}

View File

@ -56,6 +56,7 @@
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.
//