summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorCarsten Pfeiffer2011-03-16 11:57:35 (EDT)
committer Chris Aniszczyk2011-04-01 21:26:15 (EDT)
commit11e2e746c13cc94cabc96e708f3c6a2b159b5995 (patch)
tree7893a3d1d4b62a8242506b673575905a804be075
parent1e03d52f33bbe8398ca0f19612d8f908d4f2a3ec (diff)
downloadjgit-11e2e746c13cc94cabc96e708f3c6a2b159b5995.zip
jgit-11e2e746c13cc94cabc96e708f3c6a2b159b5995.tar.gz
jgit-11e2e746c13cc94cabc96e708f3c6a2b159b5995.tar.bz2
Support reading first SHA-1 from large FETCH_HEAD filesrefs/changes/39/2739/3
When reading refs, avoid reading huge files that were put there accidentally, but still read the top of e.g. FETCH_HEAD, which may be longer than our limit. We're only interested in the first line anyway. Bug: 340880 Change-Id: I11029b9b443f22019bf80bd3dd942b48b531bc11 Signed-off-by: Carsten Pfieffer <carsten.pfeiffer@gebit.de> Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/storage/file/RefDirectory.java6
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java41
2 files changed, 46 insertions, 1 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/RefDirectory.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/RefDirectory.java
index c056d5a..5a36a71 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/RefDirectory.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/RefDirectory.java
@@ -850,9 +850,10 @@ public class RefDirectory extends RefDatabase {
} else if (modified == 0)
return null;
+ final int limit = 4096;
final byte[] buf;
try {
- buf = IO.readFully(path, 4096);
+ buf = IO.readSome(path, limit);
} catch (FileNotFoundException noFile) {
return null; // doesn't exist; not a reference.
}
@@ -862,6 +863,9 @@ public class RefDirectory extends RefDatabase {
return null; // empty file; not a reference.
if (isSymRef(buf, n)) {
+ if (n == limit)
+ return null; // possibly truncated ref
+
// trim trailing whitespace
while (0 < n && Character.isWhitespace(buf[n - 1]))
n--;
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java
index a9c3853..4b9c572 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java
@@ -78,6 +78,47 @@ public class IO {
}
/**
+ * Read at most limit bytes from the local file into memory as a byte array.
+ *
+ * @param path
+ * location of the file to read.
+ * @param limit
+ * maximum number of bytes to read, if the file is larger than
+ * only the first limit number of bytes are returned
+ * @return complete contents of the requested local file. If the contents
+ * exceeds the limit, then only the limit is returned.
+ * @throws FileNotFoundException
+ * the file does not exist.
+ * @throws IOException
+ * the file exists, but its contents cannot be read.
+ */
+ public static final byte[] readSome(final File path, final int limit)
+ throws FileNotFoundException, IOException {
+ FileInputStream in = new FileInputStream(path);
+ try {
+ byte[] buf = new byte[limit];
+ int cnt = 0;
+ for (;;) {
+ int n = in.read(buf, cnt, buf.length - cnt);
+ if (n <= 0)
+ break;
+ cnt += n;
+ }
+ if (cnt == buf.length)
+ return buf;
+ byte[] res = new byte[cnt];
+ System.arraycopy(buf, 0, res, 0, cnt);
+ return res;
+ } finally {
+ try {
+ in.close();
+ } catch (IOException ignored) {
+ // do nothing
+ }
+ }
+ }
+
+ /**
* Read an entire local file into memory as a byte array.
*
* @param path