gpg-interface: find the last gpg signature line

A signed tag has a detached signature like this: object ... [...more header...] This is the tag body. -----BEGIN PGP SIGNATURE----- [opaque gpg data] -----END PGP SIGNATURE----- Our parser finds the _first_ line that appears to start a PGP signature block, meaning we may be confused by a signature (or a signature-like line) in the actual body. Let's keep parsing and always find the final block, which should be the detached signature over all of the preceding content. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Ben Toews <mastahyeti@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Apr 13, 2018 at 15:18 UTC 8b44b2be89bf59c0fada6095bdfea66ff53c6074
2 files changed +20 -3
gpg-interface.c
+9 -3
@@ -110,11 +110,17 @@ static int is_gpg_start(const char *line)
110 size_t parse_signature(const char *buf, size_t size)
111 {
112 size_t len = 0;
113 - while (len < size && !is_gpg_start(buf + len)) {
114 - const char *eol = memchr(buf + len, '\n', size - len);
113 + size_t match = size;
114 + while (len < size) {
115 + const char *eol;
116 +
117 + if (is_gpg_start(buf + len))
118 + match = len;
119 +
120 + eol = memchr(buf + len, '\n', size - len);
121 len += eol ? eol - (buf + len) + 1 : size - len;
122 }
117 - return len;
123 + return match;
124 }
125
126 void set_signing_key(const char *key)
t/t7004-tag.sh
+11
@@ -1059,6 +1059,17 @@ test_expect_success GPG \
1059 git tag -v blanknonlfile-signed-tag
1060 '
1061
1062 +test_expect_success GPG 'signed tag with embedded PGP message' '
1063 + cat >msg <<-\EOF &&
1064 + -----BEGIN PGP MESSAGE-----
1065 +
1066 + this is not a real PGP message
1067 + -----END PGP MESSAGE-----
1068 + EOF
1069 + git tag -s -F msg confusing-pgp-message &&
1070 + git tag -v confusing-pgp-message
1071 +'
1072 +
1073 # messages with commented lines for signed tags:
1074
1075 cat >sigcommentsfile <<EOF