Skip to content

Commit 492261a

Browse files
committed
Merge branch 'jc/find-header'
Code clean-up. * jc/find-header: receive-pack.c: consolidate find header logic
2 parents 7a9ae6d + cfc5cf4 commit 492261a

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

builtin/receive-pack.c

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -581,32 +581,19 @@ static char *prepare_push_cert_nonce(const char *path, timestamp_t stamp)
581581
return strbuf_detach(&buf, NULL);
582582
}
583583

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-
*/
589584
static char *find_header(const char *msg, size_t len, const char *key,
590585
const char **next_line)
591586
{
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);
610597
}
611598

612599
/*

commit.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,12 +1631,20 @@ struct commit_list **commit_list_append(struct commit *commit,
16311631
return &new_commit->next;
16321632
}
16331633

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)
16351636
{
16361637
int key_len = strlen(key);
16371638
const char *line = msg;
16381639

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) {
16401648
const char *eol = strchrnul(line, '\n');
16411649

16421650
if (line == eol)
@@ -1653,6 +1661,10 @@ const char *find_commit_header(const char *msg, const char *key, size_t *out_len
16531661
return NULL;
16541662
}
16551663

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+
}
16561668
/*
16571669
* Inspect the given string and determine the true "end" of the log message, in
16581670
* order to find where to put a new Signed-off-by trailer. Ignored are

commit.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,17 @@ void free_commit_extra_headers(struct commit_extra_header *extra);
290290

291291
/*
292292
* Search the commit object contents given by "msg" for the header "key".
293+
* Reads up to "len" bytes of "msg".
293294
* Returns a pointer to the start of the header contents, or NULL. The length
294295
* of the header, up to the first newline, is returned via out_len.
295296
*
296297
* Note that some headers (like mergetag) may be multi-line. It is the caller's
297298
* responsibility to parse further in this case!
298299
*/
300+
const char *find_header_mem(const char *msg, size_t len,
301+
const char *key,
302+
size_t *out_len);
303+
299304
const char *find_commit_header(const char *msg, const char *key,
300305
size_t *out_len);
301306

0 commit comments

Comments
 (0)