Skip to content

Commit 7c11718

Browse files
Martin Ågrengitster
authored andcommitted
bisect: fix off-by-one error in best_bisection_sorted()
After we have sorted the `cnt`-many commits that we have selected, we place them into the commit list. We then set `p->next` to NULL, but as we do so, `p` is already pointing one beyond item number `cnt`. Indeed, we check whether `p` is NULL before dereferencing it. This only matters if there are TREESAME-commits. Since they should be skipped, they are not included in `cnt` and we will hit the situation where we set `p->next` to NULL. As a result, the list will be one longer than it should be. The last commit in the list will be one which occurs earlier, or which shouldn't be included. Do not update `p` the very last round in the loop. This ensures that after the loop, `p->next` points to the remainder of the list, and we can set it to NULL. While we're here, free that remainder to fix a memory leak. Signed-off-by: Martin Ågren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fc5c40b commit 7c11718

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

bisect.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,11 @@ static struct commit_list *best_bisection_sorted(struct commit_list *list, int n
226226
add_name_decoration(DECORATION_NONE, buf.buf, obj);
227227

228228
p->item = array[i].commit;
229-
p = p->next;
229+
if (i < cnt - 1)
230+
p = p->next;
230231
}
231-
if (p)
232-
p->next = NULL;
232+
free_commit_list(p->next);
233+
p->next = NULL;
233234
strbuf_release(&buf);
234235
free(array);
235236
return list;

0 commit comments

Comments
 (0)