Skip to content

Commit 8b36f0b

Browse files
committed
Merge branch 'po/read-graft-line'
Conversion from uchar[20] to struct object_id continues; this is to ensure that we do not assume sizeof(struct object_id) is the same as the length of SHA-1 hash (or length of longest hash we support). * po/read-graft-line: commit: rewrite read_graft_line commit: allocate array using object_id size commit: replace the raw buffer with strbuf in read_graft_line sha1_file: fix definition of null_sha1
2 parents 1fb77b3 + cfa5bf1 commit 8b36f0b

File tree

4 files changed

+29
-23
lines changed

4 files changed

+29
-23
lines changed

builtin/blame.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ static int read_ancestry(const char *graft_file)
488488
return -1;
489489
while (!strbuf_getwholeline(&buf, fp, '\n')) {
490490
/* The format is just "Commit Parent1 Parent2 ...\n" */
491-
struct commit_graft *graft = read_graft_line(buf.buf, buf.len);
491+
struct commit_graft *graft = read_graft_line(&buf);
492492
if (graft)
493493
register_commit_graft(graft, 0);
494494
}

commit.c

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -134,35 +134,41 @@ int register_commit_graft(struct commit_graft *graft, int ignore_dups)
134134
return 0;
135135
}
136136

137-
struct commit_graft *read_graft_line(char *buf, int len)
137+
struct commit_graft *read_graft_line(struct strbuf *line)
138138
{
139139
/* The format is just "Commit Parent1 Parent2 ...\n" */
140-
int i;
140+
int i, phase;
141+
const char *tail = NULL;
141142
struct commit_graft *graft = NULL;
142-
const int entry_size = GIT_SHA1_HEXSZ + 1;
143+
struct object_id dummy_oid, *oid;
143144

144-
while (len && isspace(buf[len-1]))
145-
buf[--len] = '\0';
146-
if (buf[0] == '#' || buf[0] == '\0')
145+
strbuf_rtrim(line);
146+
if (!line->len || line->buf[0] == '#')
147147
return NULL;
148-
if ((len + 1) % entry_size)
149-
goto bad_graft_data;
150-
i = (len + 1) / entry_size - 1;
151-
graft = xmalloc(st_add(sizeof(*graft), st_mult(GIT_SHA1_RAWSZ, i)));
152-
graft->nr_parent = i;
153-
if (get_oid_hex(buf, &graft->oid))
154-
goto bad_graft_data;
155-
for (i = GIT_SHA1_HEXSZ; i < len; i += entry_size) {
156-
if (buf[i] != ' ')
157-
goto bad_graft_data;
158-
if (get_sha1_hex(buf + i + 1, graft->parent[i/entry_size].hash))
148+
/*
149+
* phase 0 verifies line, counts hashes in line and allocates graft
150+
* phase 1 fills graft
151+
*/
152+
for (phase = 0; phase < 2; phase++) {
153+
oid = graft ? &graft->oid : &dummy_oid;
154+
if (parse_oid_hex(line->buf, oid, &tail))
159155
goto bad_graft_data;
156+
for (i = 0; *tail != '\0'; i++) {
157+
oid = graft ? &graft->parent[i] : &dummy_oid;
158+
if (!isspace(*tail++) || parse_oid_hex(tail, oid, &tail))
159+
goto bad_graft_data;
160+
}
161+
if (!graft) {
162+
graft = xmalloc(st_add(sizeof(*graft),
163+
st_mult(sizeof(struct object_id), i)));
164+
graft->nr_parent = i;
165+
}
160166
}
161167
return graft;
162168

163169
bad_graft_data:
164-
error("bad graft data: %s", buf);
165-
free(graft);
170+
error("bad graft data: %s", line->buf);
171+
assert(!graft);
166172
return NULL;
167173
}
168174

@@ -174,7 +180,7 @@ static int read_graft_file(const char *graft_file)
174180
return -1;
175181
while (!strbuf_getwholeline(&buf, fp, '\n')) {
176182
/* The format is just "Commit Parent1 Parent2 ...\n" */
177-
struct commit_graft *graft = read_graft_line(buf.buf, buf.len);
183+
struct commit_graft *graft = read_graft_line(&buf);
178184
if (!graft)
179185
continue;
180186
if (register_commit_graft(graft, 1))

commit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ struct commit_graft {
247247
};
248248
typedef int (*each_commit_graft_fn)(const struct commit_graft *, void *);
249249

250-
struct commit_graft *read_graft_line(char *buf, int len);
250+
struct commit_graft *read_graft_line(struct strbuf *line);
251251
int register_commit_graft(struct commit_graft *, int);
252252
struct commit_graft *lookup_commit_graft(const struct object_id *oid);
253253

sha1_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "quote.h"
3131
#include "packfile.h"
3232

33-
const unsigned char null_sha1[20];
33+
const unsigned char null_sha1[GIT_MAX_RAWSZ];
3434
const struct object_id null_oid;
3535
const struct object_id empty_tree_oid = {
3636
EMPTY_TREE_SHA1_BIN_LITERAL

0 commit comments

Comments
 (0)