git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kristoffer Haugsbakk <code@khaugsbakk.name>
To: git@vger.kernel.org
Cc: Kristoffer Haugsbakk <code@khaugsbakk.name>
Subject: [PATCH v2 3/3] format-patch: check if header output looks valid
Date: Tue, 19 Mar 2024 19:35:38 +0100	[thread overview]
Message-ID: <c570467c8db35d96e4262857658fcc64328d810c.1710873210.git.code@khaugsbakk.name> (raw)
In-Reply-To: <cover.1710873210.git.code@khaugsbakk.name>

Implement a function based on `mailinfo.c:is_mail`.

Signed-off-by: Kristoffer Haugsbakk <code@khaugsbakk.name>
---

Notes (series):
    Isolating this for review as its own commit so that I can point out the
    provenance. May well be squashed into the main patch eventually.

 builtin/log.c           | 33 +++++++++++++++++++++++++++++++++
 t/t4014-format-patch.sh | 13 +++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/builtin/log.c b/builtin/log.c
index bc656b5e0f8..2902c2bf6fe 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -1876,6 +1876,35 @@ static void infer_range_diff_ranges(struct strbuf *r1,
 	}
 }
 
+static int is_mail(struct strbuf *sb)
+{
+	const char *header_regex = "^[!-9;-~]+:";
+	regex_t regex;
+	int ret = 1, i;
+	struct string_list list = STRING_LIST_INIT_DUP;
+
+	if (regcomp(&regex, header_regex, REG_NOSUB | REG_EXTENDED))
+		die("invalid pattern: %s", header_regex);
+	string_list_split(&list, sb->buf, '\n', -1);
+	for (i = 0; i < list.nr; i++) {
+		/* End of header */
+		if (!*list.items[i].string && i == (list.nr - 1))
+			break;
+		/* Ignore indented folded lines */
+		if (*list.items[i].string == '\t' ||
+		    *list.items[i].string == ' ')
+			continue;
+		/* It's a header if it matches header_regex */
+		if (regexec(&regex, list.items[i].string, 0, NULL, 0)) {
+			ret = 0;
+			break;
+		}
+	}
+	string_list_clear(&list, 1);
+	regfree(&regex);
+	return ret;
+}
+
 /* Returns an owned pointer */
 static char *header_cmd_output(struct rev_info *rev, const struct commit *cmit)
 {
@@ -1902,6 +1931,10 @@ static char *header_cmd_output(struct rev_info *rev, const struct commit *cmit)
 			die(_("header-cmd %s: failed with exit code %d"),
 			    header_cmd, res);
 	}
+	if (!is_mail(&output))
+		die(_("header-cmd %s: returned output which was "
+		      "not recognized as valid RFC 2822 headers"),
+		    header_cmd);
 	return strbuf_detach(&output, NULL);
 }
 
diff --git a/t/t4014-format-patch.sh b/t/t4014-format-patch.sh
index dc85c4c28fe..533a5b246e5 100755
--- a/t/t4014-format-patch.sh
+++ b/t/t4014-format-patch.sh
@@ -255,6 +255,19 @@ test_expect_success '--header-cmd with no output works' '
 	git format-patch --header-cmd=true --stdout main..side
 '
 
+test_expect_success '--header-cmd without headers-like output fails' '
+	write_script cmd <<-\EOF &&
+	printf "X-S: $GIT_FP_HEADER_CMD_HASH\n"
+	printf "\n"
+	printf "X-C: $GIT_FP_HEADER_CMD_COUNT\n"
+	EOF
+	cat > expect <<-\EOF &&
+	fatal: header-cmd ./cmd: returned output which was not recognized as valid RFC 2822 headers
+	EOF
+	test_must_fail git format-patch --header-cmd=./cmd --stdout main..side >actual 2>&1 &&
+	test_cmp expect actual
+'
+
 test_expect_success '--header-cmd reports failed command' '
 	cat > expect <<-\EOF &&
 	fatal: header-cmd false: failed with exit code 1
-- 
2.44.0.144.g29ae9861142


      parent reply	other threads:[~2024-03-19 18:37 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-07 19:59 [PATCH 0/3] format-patch: teach `--header-cmd` Kristoffer Haugsbakk
2024-03-07 19:59 ` [PATCH 1/3] log-tree: take ownership of pointer Kristoffer Haugsbakk
2024-03-12  9:29   ` Jeff King
2024-03-12 17:43     ` Kristoffer Haugsbakk
2024-03-13  6:54       ` Jeff King
2024-03-13 17:49         ` Kristoffer Haugsbakk
2024-03-07 19:59 ` [PATCH 2/3] format-patch: teach `--header-cmd` Kristoffer Haugsbakk
2024-03-08 18:30   ` Kristoffer Haugsbakk
2024-03-11 21:29   ` Jean-Noël Avila
2024-03-12  8:13     ` Kristoffer Haugsbakk
2024-03-07 19:59 ` [PATCH 3/3] format-patch: check if header output looks valid Kristoffer Haugsbakk
2024-03-19 18:35 ` [PATCH v2 0/3] format-patch: teach `--header-cmd` Kristoffer Haugsbakk
2024-03-19 18:35   ` [PATCH v2 1/3] revision: add a per-email field to rev-info Kristoffer Haugsbakk
2024-03-19 21:29     ` Jeff King
2024-03-19 21:41       ` Kristoffer Haugsbakk
2024-03-20  0:25       ` Jeff King
2024-03-20  0:27         ` [PATCH 1/6] shortlog: stop setting pp.print_email_subject Jeff King
2024-03-20  0:28         ` [PATCH 2/6] pretty: split oneline and email subject printing Jeff King
2024-03-22 22:00           ` Kristoffer Haugsbakk
2024-03-20  0:30         ` [PATCH 3/6] pretty: drop print_email_subject flag Jeff King
2024-03-20  0:31         ` [PATCH 4/6] log: do not set up extra_headers for non-email formats Jeff King
2024-03-22 22:04           ` Kristoffer Haugsbakk
2024-03-20  0:35         ` [PATCH 5/6] format-patch: return an allocated string from log_write_email_headers() Jeff King
2024-03-22 22:06           ` Kristoffer Haugsbakk
2024-03-20  0:35         ` [PATCH 6/6] format-patch: simplify after-subject MIME header handling Jeff King
2024-03-22 22:08           ` Kristoffer Haugsbakk
2024-03-20  0:43         ` [PATCH v2 1/3] revision: add a per-email field to rev-info Jeff King
2024-03-22 22:31           ` Kristoffer Haugsbakk
2024-03-22  9:59         ` [PATCH 7/6] format-patch: fix leak of empty header string Jeff King
2024-03-22 10:03           ` Kristoffer Haugsbakk
2024-03-22 16:50           ` Junio C Hamano
2024-03-22 22:16           ` Kristoffer Haugsbakk
2024-03-19 18:35   ` [PATCH v2 2/3] format-patch: teach `--header-cmd` Kristoffer Haugsbakk
2024-03-19 18:35   ` Kristoffer Haugsbakk [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c570467c8db35d96e4262857658fcc64328d810c.1710873210.git.code@khaugsbakk.name \
    --to=code@khaugsbakk.name \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).