git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 5/5] pretty: avoid reading past end-of-string with "%G"

From
Jeff King <peff@peff.net>
Date
Jun 17, 2014, 00:07 UTC
Message-ID
<20140617000707.GE17110@sigill.intra.peff.net>
In-Reply-To
<20140616235917.GA19499@sigill.intra.peff.net>

If the user asks for --format=%G with nothing else, we correctly realize that "%G" is not a valid placeholder (it should be "%G?", "%GK", etc). But we still tell the strbuf_expand code that we consumed 2 characters, causing it to jump over the trailing NUL and output garbage.

This also fixes the case where "%GX" would be consumed (and produce no output). In other cases, we pass unrecognized placeholders through to the final string.

Signed-off-by: Jeff King <peff@peff.net>
---
Same as before, but with the test included along with the fix, rather
than in another commit.
 pretty.c                 | 2 ++
 t/t7510-signed-commit.sh | 6 ++++++
 2 files changed, 8 insertions(+)
diff --git a/pretty.c b/pretty.c
index e1e2cad..70d8776 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1267,6 +1267,8 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 			if (c->signature_check.key)
 				strbuf_addstr(sb, c->signature_check.key);
 			break;
+		default:
+			return 0;
 		}
 		return 2;
 	}
diff --git a/t/t7510-signed-commit.sh b/t/t7510-signed-commit.sh
index e97477a..9810242 100755
--- a/t/t7510-signed-commit.sh
+++ b/t/t7510-signed-commit.sh
@@ -147,4 +147,10 @@ test_expect_success GPG 'show lack of signature with custom format' '
 	test_cmp expect actual
 '
 
+test_expect_success 'unused %G placeholders are passed through' '
+	echo "%GX %G" >expect &&
+	git log -1 --format="%GX %G" >actual &&
+	test_cmp expect actual
+'
+
 test_done
-- 
2.0.0.566.gfe3e6b2
Previous: Jeff King
Message 10 of 10 in “pretty: avoid reading past end-of-string with "%G"”
  1. pretty: avoid reading past end-of-string with "%G"Jeff King, Jun 16, 2014
  2. t7510: check %G* pretty-format outputJeff King, Jun 16, 2014
  3. Eric SunshineJun 16, 2014
  4. Jeff KingJun 16, 2014
  5. 0/5 --format=%G tests and fixesJeff King, Jun 16, 2014
  6. 1/5 t7510: stop referring to master in later testsJeff King, Jun 16, 2014
  7. 2/5 t7510: use consistent &&-chains in loopJeff King, Jun 17, 2014
  8. 3/5 t7510: test a commit signed by an unknown keyJeff King, Jun 17, 2014
  9. 4/5 t7510: check %G* pretty-format outputJeff King, Jun 17, 2014
  10. 5/5 pretty: avoid reading past end-of-string with "%G"Jeff King, Jun 17, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.