Skip to content

Commit 2bb73ae

Browse files
rscharfegitster
authored andcommitted
patch-id: use starts_with() and skip_prefix()
Get rid of magic numbers and avoid running over the end of a NUL terminated string by using starts_with() and skip_prefix() instead of memcmp(). Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 7654286 commit 2bb73ae

1 file changed

Lines changed: 10 additions & 13 deletions

File tree

builtin/patch-id.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,13 @@ static int get_one_patchid(unsigned char *next_sha1, unsigned char *result,
8181

8282
while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) {
8383
char *line = line_buf->buf;
84-
char *p = line;
84+
const char *p = line;
8585
int len;
8686

87-
if (!memcmp(line, "diff-tree ", 10))
88-
p += 10;
89-
else if (!memcmp(line, "commit ", 7))
90-
p += 7;
91-
else if (!memcmp(line, "From ", 5))
92-
p += 5;
93-
else if (!memcmp(line, "\\ ", 2) && 12 < strlen(line))
87+
if (!skip_prefix(line, "diff-tree ", &p) &&
88+
!skip_prefix(line, "commit ", &p) &&
89+
!skip_prefix(line, "From ", &p) &&
90+
starts_with(line, "\\ ") && 12 < strlen(line))
9491
continue;
9592

9693
if (!get_sha1_hex(p, next_sha1)) {
@@ -99,29 +96,29 @@ static int get_one_patchid(unsigned char *next_sha1, unsigned char *result,
9996
}
10097

10198
/* Ignore commit comments */
102-
if (!patchlen && memcmp(line, "diff ", 5))
99+
if (!patchlen && !starts_with(line, "diff "))
103100
continue;
104101

105102
/* Parsing diff header? */
106103
if (before == -1) {
107-
if (!memcmp(line, "index ", 6))
104+
if (starts_with(line, "index "))
108105
continue;
109-
else if (!memcmp(line, "--- ", 4))
106+
else if (starts_with(line, "--- "))
110107
before = after = 1;
111108
else if (!isalpha(line[0]))
112109
break;
113110
}
114111

115112
/* Looking for a valid hunk header? */
116113
if (before == 0 && after == 0) {
117-
if (!memcmp(line, "@@ -", 4)) {
114+
if (starts_with(line, "@@ -")) {
118115
/* Parse next hunk, but ignore line numbers. */
119116
scan_hunk_header(line, &before, &after);
120117
continue;
121118
}
122119

123120
/* Split at the end of the patch. */
124-
if (memcmp(line, "diff ", 5))
121+
if (!starts_with(line, "diff "))
125122
break;
126123

127124
/* Else we're parsing another header. */

0 commit comments

Comments
 (0)