refs: loosen the OID parsing We used to require loose references to contain only an OID (possibly after trimming the string). This is however not enough for letting us lookup FETCH_HEAD, which can have a lot of content after the initial OID. Change the parsing rules so that a loose refernce must e at least 40 bytes long and the 41st (if it's there) must be accepted by isspace(3). This makes the trim unnecessary, so only do it for symrefs. This fixes #977.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
diff --git a/src/refs.c b/src/refs.c
index 1d73b26..9e2311b 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -155,11 +155,26 @@ static int loose_parse_symbolic(git_reference *ref, git_buf *file_content)
static int loose_parse_oid(git_oid *oid, git_buf *file_content)
{
- /* File format: 40 chars (OID) */
- if (git_buf_len(file_content) == GIT_OID_HEXSZ &&
- git_oid_fromstr(oid, git_buf_cstr(file_content)) == 0)
+ size_t len;
+ const char *str;
+
+ len = git_buf_len(file_content);
+ if (len < GIT_OID_HEXSZ)
+ goto corrupted;
+
+ /* str is guranteed to be zero-terminated */
+ str = git_buf_cstr(file_content);
+
+ /* If the file is longer than 40 chars, the 41st must be a space */
+ if (git_oid_fromstr(oid, git_buf_cstr(file_content)) < 0)
+ goto corrupted;
+
+ /* If the file is longer than 40 chars, the 41st must be a space */
+ str += GIT_OID_HEXSZ;
+ if (*str == '\0' || git__isspace(*str))
return 0;
+corrupted:
giterr_set(GITERR_REFERENCE, "Corrupted loose reference file");
return -1;
}
diff --git a/tests-clar/refs/read.c b/tests-clar/refs/read.c
index 6ab6bf5..fc2d6b9 100644
--- a/tests-clar/refs/read.c
+++ b/tests-clar/refs/read.c
@@ -212,6 +212,7 @@ void test_refs_read__trailing(void)
cl_git_pass(git_reference_lookup(&test, g_repo, "refs/heads/test"));
cl_git_pass(git_reference_lookup(&trailing, g_repo, "refs/heads/trailing"));
cl_git_pass(git_oid_cmp(git_reference_oid(test), git_reference_oid(trailing)));
+ cl_git_pass(git_reference_lookup(&trailing, g_repo, "FETCH_HEAD"));
git_reference_free(test);
git_reference_free(trailing);
diff --git a/tests-clar/resources/testrepo.git/FETCH_HEAD b/tests-clar/resources/testrepo.git/FETCH_HEAD
new file mode 100644
index 0000000..4844626
--- /dev/null
+++ b/tests-clar/resources/testrepo.git/FETCH_HEAD
@@ -0,0 +1,2 @@
+a65fedf39aefe402d3bb6e24df4d4f5fe4547750 branch 'master' of git://example.com/git/testrepo.git
+258f0e2a959a364e40ed6603d5d44fbb24765b10 not-for-merge branch 'haacked' of git://example.com/git/testrepo.git