Commit 8958fad77078643211c2eb14ea1d94ce4780a197

Russell Belfer 2013-01-22T16:02:43

Merge pull request #1270 from libgit2/packed-peeled-objects-fix Allow peeled references without trailing newline at end of file

diff --git a/src/refs.c b/src/refs.c
index 4934a03..52e0ada 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -328,7 +328,7 @@ static int packed_parse_peel(
 	if (git__prefixcmp(tag_ref->name, GIT_REFS_TAGS_DIR) != 0)
 		goto corrupt;
 
-	if (buffer + GIT_OID_HEXSZ >= buffer_end)
+	if (buffer + GIT_OID_HEXSZ > buffer_end)
 		goto corrupt;
 
 	/* Is this a valid object id? */
@@ -339,10 +339,14 @@ static int packed_parse_peel(
 	if (*buffer == '\r')
 		buffer++;
 
-	if (*buffer != '\n')
-		goto corrupt;
+	if (buffer != buffer_end) {
+		if (*buffer == '\n')
+			buffer++;
+		else
+			goto corrupt;
+	}
 
-	*buffer_out = buffer + 1;
+	*buffer_out = buffer;
 	return 0;
 
 corrupt:
diff --git a/tests-clar/object/tag/read.c b/tests-clar/object/tag/read.c
index 53272e9..16e3e63 100644
--- a/tests-clar/object/tag/read.c
+++ b/tests-clar/object/tag/read.c
@@ -81,6 +81,7 @@ void test_object_tag_read__parse_without_tagger(void)
 
    cl_assert(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0);
 
+
    git_tag_free(bad_tag);
    git_commit_free(commit);
    git_repository_free(bad_tag_repo);
diff --git a/tests-clar/refs/listall.c b/tests-clar/refs/listall.c
index 7f1de74..8f4c374 100644
--- a/tests-clar/refs/listall.c
+++ b/tests-clar/refs/listall.c
@@ -34,3 +34,14 @@ void test_refs_listall__from_repository_opened_through_gitdir_path(void)
 {
 	ensure_no_refname_starts_with_a_forward_slash(cl_fixture("testrepo.git"));
 }
+
+void test_refs_listall__from_repository_with_no_trailing_newline(void)
+{
+	cl_git_pass(git_repository_open(&repo, cl_fixture("bad_tag.git")));
+	cl_git_pass(git_reference_list(&ref_list, repo, GIT_REF_LISTALL));
+
+	cl_assert(ref_list.count > 0);
+
+	git_strarray_free(&ref_list);
+	git_repository_free(repo);
+}
diff --git a/tests-clar/resources/bad_tag.git/packed-refs b/tests-clar/resources/bad_tag.git/packed-refs
index f9fd2fd..9da1645 100644
--- a/tests-clar/resources/bad_tag.git/packed-refs
+++ b/tests-clar/resources/bad_tag.git/packed-refs
@@ -1,3 +1,5 @@
 # pack-refs with: peeled 
 eda9f45a2a98d4c17a09d681d88569fa4ea91755 refs/tags/e90810b
 ^e90810b8df3e80c413d903f631643c716887138d
+d3bacb8d3ff25876a961b1963b6515170d0151ab refs/tags/hello
+^6dcf9bf7541ee10456529833502442f385010c3d
\ No newline at end of file