Handle return code of parse_commit in revision machinery
This fixes a crash in broken repositories where random commits suddenly disappear. Signed-off-by: Alex Riesen <raa.lkml@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>
Alex Riesen committed
May 4, 2007 at 23:54 UTC
cc0e6c5adc783bcb29954f3a0b7a6197bcc9b1b3
2 files changed
+51
-24
revision.c
+50
-23
@@ -318,7 +318,10 @@ static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
318
while ((parent = *pp) != NULL) {
319
struct commit *p = parent->item;
320
321
- parse_commit(p);
321
+ if (parse_commit(p) < 0)
322
+ die("cannot simplify commit %s (because of %s)",
323
+ sha1_to_hex(commit->object.sha1),
324
+ sha1_to_hex(p->object.sha1));
325
switch (rev_compare_tree(revs, p->tree, commit->tree)) {
326
case REV_TREE_SAME:
327
tree_same = 1;
@@ -347,7 +350,10 @@ static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
350
* IOW, we pretend this parent is a
351
* "root" commit.
352
*/
350
- parse_commit(p);
353
+ if (parse_commit(p) < 0)
354
+ die("cannot simplify commit %s (invalid %s)",
355
+ sha1_to_hex(commit->object.sha1),
356
+ sha1_to_hex(p->object.sha1));
357
p->parents = NULL;
358
}
359
/* fallthrough */
@@ -362,14 +368,14 @@ static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
368
commit->object.flags |= TREECHANGE;
369
}
370
365
-static void add_parents_to_list(struct rev_info *revs, struct commit *commit, struct commit_list **list)
371
+static int add_parents_to_list(struct rev_info *revs, struct commit *commit, struct commit_list **list)
372
{
373
struct commit_list *parent = commit->parents;
374
unsigned left_flag;
375
int add, rest;
376
377
if (commit->object.flags & ADDED)
372
- return;
378
+ return 0;
379
commit->object.flags |= ADDED;
380
381
/*
@@ -388,7 +394,8 @@ static void add_parents_to_list(struct rev_info *revs, struct commit *commit, st
394
while (parent) {
395
struct commit *p = parent->item;
396
parent = parent->next;
391
- parse_commit(p);
397
+ if (parse_commit(p) < 0)
398
+ return -1;
399
p->object.flags |= UNINTERESTING;
400
if (p->parents)
401
mark_parents_uninteresting(p);
@@ -397,7 +404,7 @@ static void add_parents_to_list(struct rev_info *revs, struct commit *commit, st
404
p->object.flags |= SEEN;
405
insert_by_date(p, list);
406
}
400
- return;
407
+ return 0;
408
}
409
410
/*
@@ -409,7 +416,7 @@ static void add_parents_to_list(struct rev_info *revs, struct commit *commit, st
416
revs->prune_fn(revs, commit);
417
418
if (revs->no_walk)
412
- return;
419
+ return 0;
420
421
left_flag = (commit->object.flags & SYMMETRIC_LEFT);
422
@@ -418,7 +425,8 @@ static void add_parents_to_list(struct rev_info *revs, struct commit *commit, st
425
struct commit *p = parent->item;
426
427
parent = parent->next;
421
- parse_commit(p);
428
+ if (parse_commit(p) < 0)
429
+ return -1;
430
p->object.flags |= left_flag;
431
if (p->object.flags & SEEN)
432
continue;
@@ -426,6 +434,7 @@ static void add_parents_to_list(struct rev_info *revs, struct commit *commit, st
434
if (add)
435
insert_by_date(p, list);
436
}
437
+ return 0;
438
}
439
440
static void cherry_pick_list(struct commit_list *list)
@@ -508,7 +517,7 @@ static void cherry_pick_list(struct commit_list *list)
517
free_patch_ids(&ids);
518
}
519
511
-static void limit_list(struct rev_info *revs)
520
+static int limit_list(struct rev_info *revs)
521
{
522
struct commit_list *list = revs->commits;
523
struct commit_list *newlist = NULL;
@@ -524,7 +533,8 @@ static void limit_list(struct rev_info *revs)
533
534
if (revs->max_age != -1 && (commit->date < revs->max_age))
535
obj->flags |= UNINTERESTING;
527
- add_parents_to_list(revs, commit, &list);
536
+ if (add_parents_to_list(revs, commit, &list) < 0)
537
+ return -1;
538
if (obj->flags & UNINTERESTING) {
539
mark_parents_uninteresting(commit);
540
if (everybody_uninteresting(list))
@@ -539,6 +549,7 @@ static void limit_list(struct rev_info *revs)
549
cherry_pick_list(newlist);
550
551
revs->commits = newlist;
552
+ return 0;
553
}
554
555
struct all_refs_cb {
@@ -1227,7 +1238,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, const ch
1238
return left;
1239
}
1240
1230
-void prepare_revision_walk(struct rev_info *revs)
1241
+int prepare_revision_walk(struct rev_info *revs)
1242
{
1243
int nr = revs->pending.nr;
1244
struct object_array_entry *e, *list;
@@ -1249,42 +1260,57 @@ void prepare_revision_walk(struct rev_info *revs)
1260
free(list);
1261
1262
if (revs->no_walk)
1252
- return;
1263
+ return 0;
1264
if (revs->limited)
1254
- limit_list(revs);
1265
+ if (limit_list(revs) < 0)
1266
+ return -1;
1267
if (revs->topo_order)
1268
sort_in_topological_order_fn(&revs->commits, revs->lifo,
1269
revs->topo_setter,
1270
revs->topo_getter);
1271
+ return 0;
1272
}
1273
1261
-static int rewrite_one(struct rev_info *revs, struct commit **pp)
1274
+enum rewrite_result {
1275
+ rewrite_one_ok,
1276
+ rewrite_one_noparents,
1277
+ rewrite_one_error,
1278
+};
1279
+
1280
+static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
1281
{
1282
for (;;) {
1283
struct commit *p = *pp;
1284
if (!revs->limited)
1266
- add_parents_to_list(revs, p, &revs->commits);
1285
+ if (add_parents_to_list(revs, p, &revs->commits) < 0)
1286
+ return rewrite_one_error;
1287
if (p->parents && p->parents->next)
1268
- return 0;
1288
+ return rewrite_one_ok;
1289
if (p->object.flags & (TREECHANGE | UNINTERESTING))
1270
- return 0;
1290
+ return rewrite_one_ok;
1291
if (!p->parents)
1272
- return -1;
1292
+ return rewrite_one_noparents;
1293
*pp = p->parents->item;
1294
}
1295
}
1296
1277
-static void rewrite_parents(struct rev_info *revs, struct commit *commit)
1297
+static int rewrite_parents(struct rev_info *revs, struct commit *commit)
1298
{
1299
struct commit_list **pp = &commit->parents;
1300
while (*pp) {
1301
struct commit_list *parent = *pp;
1282
- if (rewrite_one(revs, &parent->item) < 0) {
1302
+ switch (rewrite_one(revs, &parent->item)) {
1303
+ case rewrite_one_ok:
1304
+ break;
1305
+ case rewrite_one_noparents:
1306
*pp = parent->next;
1307
continue;
1308
+ case rewrite_one_error:
1309
+ return -1;
1310
}
1311
pp = &parent->next;
1312
}
1313
+ return 0;
1314
}
1315
1316
static int commit_match(struct commit *commit, struct rev_info *opt)
@@ -1320,7 +1346,8 @@ static struct commit *get_revision_1(struct rev_info *revs)
1346
if (revs->max_age != -1 &&
1347
(commit->date < revs->max_age))
1348
continue;
1323
- add_parents_to_list(revs, commit, &revs->commits);
1349
+ if (add_parents_to_list(revs, commit, &revs->commits) < 0)
1350
+ return NULL;
1351
}
1352
if (commit->object.flags & SHOWN)
1353
continue;
@@ -1348,8 +1375,8 @@ static struct commit *get_revision_1(struct rev_info *revs)
1375
if (!commit->parents || !commit->parents->next)
1376
continue;
1377
}
1351
- if (revs->parents)
1352
- rewrite_parents(revs, commit);
1378
+ if (revs->parents && rewrite_parents(revs, commit) < 0)
1379
+ return NULL;
1380
}
1381
return commit;
1382
} while (revs->commits);
revision.h
+1
-1
@@ -113,7 +113,7 @@ extern void init_revisions(struct rev_info *revs, const char *prefix);
113
extern int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def);
114
extern int handle_revision_arg(const char *arg, struct rev_info *revs,int flags,int cant_be_filename);
115
116
-extern void prepare_revision_walk(struct rev_info *revs);
116
+extern int prepare_revision_walk(struct rev_info *revs);
117
extern struct commit *get_revision(struct rev_info *revs);
118
119
extern void mark_parents_uninteresting(struct commit *commit);