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

[PATCH 4/5] commit -S: avoid invalid pointer with empty message

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Jun 29, 2016, 14:14 UTC
Message-ID
<51b87a251ac80177ff7321ec0e182cbda6dd93a1.1467209576.git.johannes.schindelin@gmx.de>
In-Reply-To
<cover.1467209576.git.johannes.schindelin@gmx.de>
While it is not recommended, `git fsck` says that:
	Not having a body is not a crime [...]

... which means that we cannot assume that the commit buffer contains an empty line to separate header from body (essentially saying that there is only a header).

So let's tread carefully here.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 commit.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/commit.c b/commit.c
index 24d4715..0bb51a2 100644
--- a/commit.c
+++ b/commit.c
@@ -1090,11 +1090,15 @@ static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
 
 static int do_sign_commit(struct strbuf *buf, const char *keyid)
 {
+	const char *eoh = strstr(buf->buf, "\n\n");
 	struct strbuf sig = STRBUF_INIT;
 	int inspos, copypos;
 
 	/* find the end of the header */
-	inspos = strstr(buf->buf, "\n\n") - buf->buf + 1;
+	if (!eoh)
+		inspos = buf->len;
+	else
+		inspos = eoh - buf->buf + 1;
 
 	if (!keyid || !*keyid)
 		keyid = get_signing_key();
-- 
2.9.0.270.g810e421
Previous: Johannes SchindelinNext: Johannes Schindelin
Message 7 of 11 in “Use skip_blank_lines() for more commit messages”
  1. 0/5 Use skip_blank_lines() for more commit messagesJohannes Schindelin, Jun 29, 2016
  2. 1/5 commit -C: skip blank lines at the beginning of the messageJohannes Schindelin, Jun 29, 2016
  3. Junio C HamanoJun 29, 2016
  4. 2/5 sequencer: use skip_blank_lines() to find the commit subjectJohannes Schindelin, Jun 29, 2016
  5. Junio C HamanoJun 29, 2016
  6. 3/5 reset --hard: skip blank lines when reporting the commit subjectJohannes Schindelin, Jun 29, 2016
  7. 4/5 commit -S: avoid invalid pointer with empty messageJohannes Schindelin, Jun 29, 2016
  8. 5/5 Skip blank lines when matching <commit>^{/foo}Johannes Schindelin, Jun 29, 2016
  9. Junio C HamanoJun 29, 2016
  10. Johannes SchindelinJul 2, 2016
  11. Junio C HamanoJun 29, 2016

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.