OSDN Git Service

range-set: fix sort_and_merge_range_set() corner case bug
authorEric Sunshine <sunshine@sunshineco.com>
Tue, 23 Jul 2013 14:28:04 +0000 (10:28 -0400)
committerJunio C Hamano <gitster@pobox.com>
Tue, 23 Jul 2013 18:37:46 +0000 (11:37 -0700)
When handed an empty range_set (range_set.nr == 0),
sort_and_merge_range_set() incorrectly sets range_set.nr to 1 at exit.
Subsequent range_set functions then access the bogus range at element
zero and crash or throw an assertion failure. Fix this bug.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Acked-by: Thomas Rast <trast@inf.ethz.ch>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
line-log.c

index 8cc29a0..5234879 100644 (file)
@@ -110,12 +110,12 @@ static void range_set_check_invariants(struct range_set *rs)
 static void sort_and_merge_range_set(struct range_set *rs)
 {
        int i;
-       int o = 1; /* output cursor */
+       int o = 0; /* output cursor */
 
        qsort(rs->ranges, rs->nr, sizeof(struct range), range_cmp);
 
-       for (i = 1; i < rs->nr; i++) {
-               if (rs->ranges[i].start <= rs->ranges[o-1].end) {
+       for (i = 0; i < rs->nr; i++) {
+               if (o > 0 && rs->ranges[i].start <= rs->ranges[o-1].end) {
                        if (rs->ranges[o-1].end < rs->ranges[i].end)
                                rs->ranges[o-1].end = rs->ranges[i].end;
                } else {