git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 3/4] bisect: fix off-by-one error in `best_bisection_sorted()`

From
MÅMartin Ågren <martin.agren@gmail.com>
Date
Nov 5, 2017, 20:24 UTC
Message-ID
<9e49e4ada1d1ede05b74c63b6e09c8891eba55d5.1509906092.git.martin.agren@gmail.com>
In-Reply-To
<cover.1509906092.git.martin.agren@gmail.com>

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 <martin.agren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 bisect.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/bisect.c b/bisect.c
index 2f4321767..b1941505b 100644
--- a/bisect.c
+++ b/bisect.c
@@ -226,10 +226,11 @@ static struct commit_list *best_bisection_sorted(struct commit_list *list, int n
 		add_name_decoration(DECORATION_NONE, buf.buf, obj);
 
 		p->item = array[i].commit;
-		p = p->next;
+		if (i < cnt - 1)
+			p = p->next;
 	}
-	if (p)
-		p->next = NULL;
+	free_commit_list(p->next);
+	p->next = NULL;
 	strbuf_release(&buf);
 	free(array);
 	return list;
-- 
2.15.0.415.gac1375d7e
Previous: Martin ÅgrenNext: Martin Ågren
Message 9 of 10 in “bisect: fix memory leak and document `find_bisection()`”
  1. 1/3 bisect: fix memory leak and document `find_bisection()`Martin Ågren, Nov 1, 2017
  2. 2/3 bisect: fix off-by-one error in `best_bisection_sorted()`Martin Ågren, Nov 1, 2017
  3. 3/3 bisect: fix memory leak when returning best elementMartin Ågren, Nov 1, 2017
  4. Junio C HamanoNov 2, 2017
  5. Martin ÅgrenNov 2, 2017
  6. 0/4 bisect: assorted leak-fixesMartin Ågren, Nov 5, 2017
  7. 1/4 bisect: change calling-convention of `find_bisection()`Martin Ågren, Nov 5, 2017
  8. 2/4 bisect: fix memory leak in `find_bisection()`Martin Ågren, Nov 5, 2017
  9. 3/4 bisect: fix off-by-one error in `best_bisection_sorted()`Martin Ågren, Nov 5, 2017
  10. 4/4 bisect: fix memory leak when returning best elementMartin Ågren, Nov 5, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.