Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf804d4

Browse files
committed
commit: fix extraction of single-line signatures
The function to extract signatures suffers from a similar bug to the header field finding one by having an unecessary line feed check as a break condition of its loop. Fix that and add a test for this single-line signature situation.
1 parent de143ef commit bf804d4

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ int git_commit_extract_signature(git_buf *signature, git_buf *signed_data, git_r
726726

727727
buf = git_odb_object_data(obj);
728728

729-
while ((h = strchr(buf, '\n')) && h[1] != '\0' && h[1] != '\n') {
729+
while ((h = strchr(buf, '\n')) && h[1] != '\0') {
730730
h++;
731731
if (git__prefixcmp(buf, field)) {
732732
if (git_buf_put(signed_data, buf, h - buf) < 0)

tests/commit/parse.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,21 @@ committer Ben Burkert <[email protected]> 1358451456 -0800\n\
498498
\n\
499499
a simple commit which works\n";
500500

501+
const char *oneline_signature = "tree 51832e6397b30309c8bcad9c55fa6ae67778f378\n\
502+
parent a1b6decaaac768b5e01e1b5dbf5b2cc081bed1eb\n\
503+
author Some User <[email protected]> 1454537944 -0700\n\
504+
committer Some User <[email protected]> 1454537944 -0700\n\
505+
gpgsig bad\n\
506+
\n\
507+
corrupt signature\n";
508+
509+
const char *oneline_data = "tree 51832e6397b30309c8bcad9c55fa6ae67778f378\n\
510+
parent a1b6decaaac768b5e01e1b5dbf5b2cc081bed1eb\n\
511+
author Some User <[email protected]> 1454537944 -0700\n\
512+
committer Some User <[email protected]> 1454537944 -0700\n\
513+
\n\
514+
corrupt signature\n";
515+
501516

502517
cl_git_pass(git_repository_odb__weakptr(&odb, g_repo));
503518
cl_git_pass(git_odb_write(&commit_id, odb, passing_commit_cases[4], strlen(passing_commit_cases[4]), GIT_OBJ_COMMIT));
@@ -523,6 +538,15 @@ a simple commit which works\n";
523538
cl_git_fail_with(GIT_ENOTFOUND, git_commit_extract_signature(&signature, &signed_data, g_repo, &commit_id, NULL));
524539
cl_assert_equal_i(GITERR_OBJECT, giterr_last()->klass);
525540

541+
/* Parse the commit with a single-line signature */
542+
git_buf_clear(&signature);
543+
git_buf_clear(&signed_data);
544+
cl_git_pass(git_odb_write(&commit_id, odb, oneline_signature, strlen(oneline_signature), GIT_OBJ_COMMIT));
545+
cl_git_pass(git_commit_extract_signature(&signature, &signed_data, g_repo, &commit_id, NULL));
546+
cl_assert_equal_s("bad", signature.ptr);
547+
cl_assert_equal_s(oneline_data, signed_data.ptr);
548+
549+
526550
git_buf_free(&signature);
527551
git_buf_free(&signed_data);
528552

0 commit comments

Comments
 (0)