receive-pack.c: consolidate find header logic

There are two functions that have very similar logic of finding a header value. find_commit_header, and find_header. We can conslidate the logic by introducing a new function find_header_mem, which is equivalent to find_commit_header except it takes a len parameter that determines how many bytes will be read. find_commit_header and find_header can then both call find_header_mem. This reduces duplicate logic, as the logic for finding header values can now all live in one place. Signed-off-by: John Cai <johncai86@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

John Cai committed Jan 6, 2022 at 20:07 UTC cfc5cf428bcc8ff31748bba97baee31f529a30ea
3 files changed +29 -25
builtin/receive-pack.c
+10 -23
@@ -581,32 +581,19 @@ static char *prepare_push_cert_nonce(const char *path, timestamp_t stamp)
581 return strbuf_detach(&buf, NULL);
582 }
583
584 -/*
585 - * NEEDSWORK: reuse find_commit_header() from jk/commit-author-parsing
586 - * after dropping "_commit" from its name and possibly moving it out
587 - * of commit.c
588 - */
584 static char *find_header(const char *msg, size_t len, const char *key,
585 const char **next_line)
586 {
592 - int key_len = strlen(key);
593 - const char *line = msg;
594 -
595 - while (line && line < msg + len) {
596 - const char *eol = strchrnul(line, '\n');
597 -
598 - if ((msg + len <= eol) || line == eol)
599 - return NULL;
600 - if (line + key_len < eol &&
601 - !memcmp(line, key, key_len) && line[key_len] == ' ') {
602 - int offset = key_len + 1;
603 - if (next_line)
604 - *next_line = *eol ? eol + 1 : eol;
605 - return xmemdupz(line + offset, (eol - line) - offset);
606 - }
607 - line = *eol ? eol + 1 : NULL;
608 - }
609 - return NULL;
587 + size_t out_len;
588 + const char *val = find_header_mem(msg, len, key, &out_len);
589 +
590 + if (!val)
591 + return NULL;
592 +
593 + if (next_line)
594 + *next_line = val + out_len + 1;
595 +
596 + return xmemdupz(val, out_len);
597 }
598
599 /*
commit.c
+14 -2
@@ -1631,12 +1631,20 @@ struct commit_list **commit_list_append(struct commit *commit,
1631 return &new_commit->next;
1632 }
1633
1634 -const char *find_commit_header(const char *msg, const char *key, size_t *out_len)
1634 +const char *find_header_mem(const char *msg, size_t len,
1635 + const char *key, size_t *out_len)
1636 {
1637 int key_len = strlen(key);
1638 const char *line = msg;
1639
1639 - while (line) {
1640 + /*
1641 + * NEEDSWORK: It's possible for strchrnul() to scan beyond the range
1642 + * given by len. However, current callers are safe because they compute
1643 + * len by scanning a NUL-terminated block of memory starting at msg.
1644 + * Nonetheless, it would be better to ensure the function does not look
1645 + * at msg beyond the len provided by the caller.
1646 + */
1647 + while (line && line < msg + len) {
1648 const char *eol = strchrnul(line, '\n');
1649
1650 if (line == eol)
@@ -1653,6 +1661,10 @@ const char *find_commit_header(const char *msg, const char *key, size_t *out_len
1661 return NULL;
1662 }
1663
1664 +const char *find_commit_header(const char *msg, const char *key, size_t *out_len)
1665 +{
1666 + return find_header_mem(msg, strlen(msg), key, out_len);
1667 +}
1668 /*
1669 * Inspect the given string and determine the true "end" of the log message, in
1670 * order to find where to put a new Signed-off-by trailer. Ignored are
commit.h
+5
@@ -290,12 +290,17 @@ void free_commit_extra_headers(struct commit_extra_header *extra);
290
291 /*
292 * Search the commit object contents given by "msg" for the header "key".
293 + * Reads up to "len" bytes of "msg".
294 * Returns a pointer to the start of the header contents, or NULL. The length
295 * of the header, up to the first newline, is returned via out_len.
296 *
297 * Note that some headers (like mergetag) may be multi-line. It is the caller's
298 * responsibility to parse further in this case!
299 */
300 +const char *find_header_mem(const char *msg, size_t len,
301 + const char *key,
302 + size_t *out_len);
303 +
304 const char *find_commit_header(const char *msg, const char *key,
305 size_t *out_len);
306