read_index_from(): catch out of order entries when reading an index file

Signed-off-by: Jaime Soriano Pastor <jsorianopastor@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jaime Soriano Pastor committed Aug 29, 2014 at 10:54 UTC 15999d0be8179fb7a2e6eafb931d25ed65df50aa
1 file changed +18
read-cache.c
+18
@@ -1438,6 +1438,21 @@ static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk,
1438 return ce;
1439 }
1440
1441 +static void check_ce_order(struct cache_entry *ce, struct cache_entry *next_ce)
1442 +{
1443 + int name_compare = strcmp(ce->name, next_ce->name);
1444 + if (0 < name_compare)
1445 + die("unordered stage entries in index");
1446 + if (!name_compare) {
1447 + if (!ce_stage(ce))
1448 + die("multiple stage entries for merged file '%s'",
1449 + ce->name);
1450 + if (ce_stage(ce) > ce_stage(next_ce))
1451 + die("unordered stage entries for '%s'",
1452 + ce->name);
1453 + }
1454 +}
1455 +
1456 /* remember to discard_cache() before reading a different cache! */
1457 int read_index_from(struct index_state *istate, const char *path)
1458 {
@@ -1499,6 +1514,9 @@ int read_index_from(struct index_state *istate, const char *path)
1514 ce = create_from_disk(disk_ce, &consumed, previous_name);
1515 set_index_entry(istate, i, ce);
1516
1517 + if (i > 0)
1518 + check_ce_order(istate->cache[i - 1], ce);
1519 +
1520 src_offset += consumed;
1521 }
1522 strbuf_release(&previous_name_buf);