Skip to content

Commit b6679e7

Browse files
sunshinecogitster
authored andcommitted
range-set: fix sort_and_merge_range_set() corner case bug
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 <[email protected]> Acked-by: Thomas Rast <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3755b53 commit b6679e7

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

line-log.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ static void range_set_check_invariants(struct range_set *rs)
110110
static void sort_and_merge_range_set(struct range_set *rs)
111111
{
112112
int i;
113-
int o = 1; /* output cursor */
113+
int o = 0; /* output cursor */
114114

115115
qsort(rs->ranges, rs->nr, sizeof(struct range), range_cmp);
116116

117-
for (i = 1; i < rs->nr; i++) {
118-
if (rs->ranges[i].start <= rs->ranges[o-1].end) {
117+
for (i = 0; i < rs->nr; i++) {
118+
if (o > 0 && rs->ranges[i].start <= rs->ranges[o-1].end) {
119119
if (rs->ranges[o-1].end < rs->ranges[i].end)
120120
rs->ranges[o-1].end = rs->ranges[i].end;
121121
} else {

0 commit comments

Comments
 (0)