return 0;
}
+void free_commit_grafts(void)
+{
+ int pos = commit_graft_nr;
+ while (pos >= 0)
+ free(commit_graft[pos--]);
+ commit_graft_nr = 0;
+}
+
struct commit_graft *read_graft_line(char *buf, int len)
{
/* The format is just "Commit Parent1 Parent2 ...\n" */
static void prepare_commit_graft(void)
{
static int commit_graft_prepared;
- char *graft_file;
+ static char *last_graft_file;
+ char *graft_file = get_graft_file();
+
+ if (last_graft_file) {
+ if (!strcmp(graft_file, last_graft_file))
+ return;
+ free_commit_grafts();
+ }
+ if (last_graft_file)
+ free(last_graft_file);
+ last_graft_file = strdup(graft_file);
- if (commit_graft_prepared)
- return;
- graft_file = get_graft_file();
read_graft_file(graft_file);
commit_graft_prepared = 1;
}
int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
{
+ char *tail = buffer;
char *bufptr = buffer;
unsigned char parent[20];
struct commit_list **pptr;
if (item->object.parsed)
return 0;
item->object.parsed = 1;
- if (memcmp(bufptr, "tree ", 5))
+ tail += size;
+ if (tail <= bufptr + 5 || memcmp(bufptr, "tree ", 5))
return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
- if (get_sha1_hex(bufptr + 5, parent) < 0)
+ if (tail <= bufptr + 45 || get_sha1_hex(bufptr + 5, parent) < 0)
return error("bad tree pointer in commit %s",
sha1_to_hex(item->object.sha1));
item->tree = lookup_tree(parent);
pptr = &item->parents;
graft = lookup_commit_graft(item->object.sha1);
- while (!memcmp(bufptr, "parent ", 7)) {
+ while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
struct commit *new_parent;
- if (get_sha1_hex(bufptr + 7, parent) || bufptr[47] != '\n')
+ if (tail <= bufptr + 48 ||
+ get_sha1_hex(bufptr + 7, parent) ||
+ bufptr[47] != '\n')
return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
bufptr += 48;
if (graft)
const char *hex = abbrev
? find_unique_abbrev(p->object.sha1, abbrev)
: sha1_to_hex(p->object.sha1);
- char *dots = (abbrev && strlen(hex) != 40) ? "..." : "";
+ const char *dots = (abbrev && strlen(hex) != 40) ? "..." : "";
parent = parent->next;
offset += sprintf(buf + offset, " %s%s", hex, dots);