Skip to content

Commit fb0b14d

Browse files
committed
Merge branch 'jk/range-diff-fixes'
"git range-diff" code clean-up. * jk/range-diff-fixes: range-diff: use ssize_t for parsed "len" in read_patches() range-diff: handle unterminated lines in read_patches() range-diff: drop useless "offset" variable from read_patches()
2 parents 7e3b9d1 + c4d5907 commit fb0b14d

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

range-diff.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,6 @@ struct patch_util {
2626
struct object_id oid;
2727
};
2828

29-
static size_t find_end_of_line(char *buffer, unsigned long size)
30-
{
31-
char *eol = memchr(buffer, '\n', size);
32-
33-
if (!eol)
34-
return size;
35-
36-
*eol = '\0';
37-
return eol + 1 - buffer;
38-
}
39-
4029
/*
4130
* Reads the patches into a string list, with the `util` field being populated
4231
* as struct object_id (will need to be free()d).
@@ -49,7 +38,7 @@ static int read_patches(const char *range, struct string_list *list,
4938
struct patch_util *util = NULL;
5039
int in_header = 1;
5140
char *line, *current_filename = NULL;
52-
int offset, len;
41+
ssize_t len;
5342
size_t size;
5443

5544
strvec_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
@@ -86,11 +75,18 @@ static int read_patches(const char *range, struct string_list *list,
8675

8776
line = contents.buf;
8877
size = contents.len;
89-
for (offset = 0; size > 0; offset += len, size -= len, line += len) {
78+
for (; size > 0; size -= len, line += len) {
9079
const char *p;
80+
char *eol;
81+
82+
eol = memchr(line, '\n', size);
83+
if (eol) {
84+
*eol = '\0';
85+
len = eol + 1 - line;
86+
} else {
87+
len = size;
88+
}
9189

92-
len = find_end_of_line(line, size);
93-
line[len - 1] = '\0';
9490
if (skip_prefix(line, "commit ", &p)) {
9591
if (util) {
9692
string_list_append(list, buf.buf)->util = util;
@@ -132,7 +128,8 @@ static int read_patches(const char *range, struct string_list *list,
132128
strbuf_addch(&buf, '\n');
133129
if (!util->diff_offset)
134130
util->diff_offset = buf.len;
135-
line[len - 1] = '\n';
131+
if (eol)
132+
*eol = '\n';
136133
orig_len = len;
137134
len = parse_git_diff_header(&root, &linenr, 0, line,
138135
len, size, &patch);

0 commit comments

Comments
 (0)