mailinfo: we parse fixed headers

The code was written as if we have a small room to add additional headers to be parsed to the header[] array at runtime, but that is not our intention at all. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Aug 20, 2024 at 14:09 UTC be10ac7037aa3332f87fc48a6ad54669f9844ba5
1 file changed +8 -8
mailinfo.c
+8 -8
@@ -346,10 +346,10 @@ static void cleanup_subject(struct mailinfo *mi, struct strbuf *subject)
346 strbuf_trim(subject);
347 }
348
349 -#define MAX_HDR_PARSED 10
350 -static const char *header[MAX_HDR_PARSED] = {
351 - "From","Subject","Date",
349 +static const char * const header[] = {
350 + "From", "Subject", "Date",
351 };
352 +#define MAX_HDR_PARSED (ARRAY_SIZE(header) + 1)
353
354 static inline int skip_header(const struct strbuf *line, const char *hdr,
355 const char **outval)
@@ -583,7 +583,7 @@ static int check_header(struct mailinfo *mi,
583 struct strbuf sb = STRBUF_INIT;
584
585 /* search for the interesting parts */
586 - for (i = 0; header[i]; i++) {
586 + for (i = 0; i < ARRAY_SIZE(header); i++) {
587 if ((!hdr_data[i] || overwrite) &&
588 parse_header(line, header[i], mi, &sb)) {
589 handle_header(&hdr_data[i], &sb);
@@ -625,7 +625,7 @@ static int is_inbody_header(const struct mailinfo *mi,
625 {
626 int i;
627 const char *val;
628 - for (i = 0; header[i]; i++)
628 + for (i = 0; i < ARRAY_SIZE(header); i++)
629 if (!mi->s_hdr_data[i] && skip_header(line, header[i], &val))
630 return 1;
631 return 0;
@@ -772,7 +772,7 @@ static int check_inbody_header(struct mailinfo *mi, const struct strbuf *line)
772 return is_format_patch_separator(line->buf + 1, line->len - 1);
773 if (starts_with(line->buf, "[PATCH]") && isspace(line->buf[7])) {
774 int i;
775 - for (i = 0; header[i]; i++)
775 + for (i = 0; i < ARRAY_SIZE(header); i++)
776 if (!strcmp("Subject", header[i])) {
777 handle_header(&mi->s_hdr_data[i], line);
778 return 1;
@@ -824,7 +824,7 @@ static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
824 * We may have already read "secondary headers"; purge
825 * them to give ourselves a clean restart.
826 */
827 - for (i = 0; header[i]; i++) {
827 + for (i = 0; i < ARRAY_SIZE(header); i++) {
828 if (mi->s_hdr_data[i])
829 strbuf_release(mi->s_hdr_data[i]);
830 FREE_AND_NULL(mi->s_hdr_data[i]);
@@ -1155,7 +1155,7 @@ static void handle_info(struct mailinfo *mi)
1155 struct strbuf *hdr;
1156 int i;
1157
1158 - for (i = 0; header[i]; i++) {
1158 + for (i = 0; i < ARRAY_SIZE(header); i++) {
1159 /* only print inbody headers if we output a patch file */
1160 if (mi->patch_lines && mi->s_hdr_data[i])
1161 hdr = mi->s_hdr_data[i];