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

[PATCH v2 10/18] bisect: copy filename string obtained from git_path()

From
mhagger@alum.mit.edu <mhagger@alum.mit.edu>
Date
Apr 26, 2012, 22:26 UTC
Message-ID
<1335479227-7877-11-git-send-email-mhagger@alum.mit.edu>
In-Reply-To
<1335479227-7877-1-git-send-email-mhagger@alum.mit.edu>
From: Michael Haggerty <mhagger@alum.mit.edu>

Prevent the string from being overwritten by other callers of git_path() and friends before we are done using it.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
Otherwise, this bug will be triggered by later patches in the series.

I didn't do a careful code audit of this problem, but it seems very plausible that that check_ancestors() and/or check_merge_bases() are guilty. They certainly do a lot more than should be done while holding on to a pointer to a statically-allocated buffer.

I cursorily checked other code in the neighborhood for similar abuses, but it would be good for an expert to look it over.

 bisect.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/bisect.c b/bisect.c
index 6e186e2..48acf73 100644
--- a/bisect.c
+++ b/bisect.c
@@ -833,7 +833,7 @@ static int check_ancestors(const char *prefix)
  */
 static void check_good_are_ancestors_of_bad(const char *prefix, int no_checkout)
 {
-	const char *filename = git_path("BISECT_ANCESTORS_OK");
+	char *filename = xstrdup(git_path("BISECT_ANCESTORS_OK"));
 	struct stat st;
 	int fd;
 
@@ -842,11 +842,11 @@ static void check_good_are_ancestors_of_bad(const char *prefix, int no_checkout)
 
 	/* Check if file BISECT_ANCESTORS_OK exists. */
 	if (!stat(filename, &st) && S_ISREG(st.st_mode))
-		return;
+		goto done;
 
 	/* Bisecting with no good rev is ok. */
 	if (good_revs.nr == 0)
-		return;
+		goto done;
 
 	/* Check if all good revs are ancestor of the bad rev. */
 	if (check_ancestors(prefix))
@@ -859,6 +859,8 @@ static void check_good_are_ancestors_of_bad(const char *prefix, int no_checkout)
 			filename, strerror(errno));
 	else
 		close(fd);
+ done:
+	free(filename);
 }
 
 /*
-- 
1.7.10
Previous: mhagger@alum.mit.eduNext: mhagger@alum.mit.edu
Message 18 of 26 in “Read loose references lazily”
  1. 00/18 Read loose references lazilymhagger@alum.mit.edu, Apr 26, 2012
  2. 01/18 get_ref_dir(): return early if directory cannot be readmhagger@alum.mit.edu, Apr 26, 2012
  3. 02/18 get_ref_dir(): use a strbuf to hold refnamemhagger@alum.mit.edu, Apr 26, 2012
  4. 03/18 get_ref_dir(): rename "base" parameter to "dirname"mhagger@alum.mit.edu, Apr 26, 2012
  5. 04/18 get_ref_dir(): require that the dirname argument ends in '/'mhagger@alum.mit.edu, Apr 26, 2012
  6. 05/18 refs.c: extract function search_for_subdir()mhagger@alum.mit.edu, Apr 26, 2012
  7. Junio C HamanoMay 3, 2012
  8. Junio C HamanoMay 3, 2012
  9. Michael HaggertyMay 4, 2012
  10. 06/18 get_ref_dir(): take the containing directory as argumentmhagger@alum.mit.edu, Apr 26, 2012
  11. 07/18 do_for_each_reflog(): return early on errormhagger@alum.mit.edu, Apr 26, 2012
  12. 08/18 do_for_each_reflog(): use a strbuf to hold logfile namemhagger@alum.mit.edu, Apr 26, 2012
  13. Junio C HamanoApr 26, 2012
  14. Michael HaggertyApr 27, 2012
  15. Junio C HamanoMay 2, 2012
  16. Michael HaggertyMay 3, 2012
  17. 09/18 do_for_each_reflog(): reuse strbuf across recursive function callsmhagger@alum.mit.edu, Apr 26, 2012
  18. 10/18 bisect: copy filename string obtained from git_path()mhagger@alum.mit.edu, Apr 26, 2012
  19. 11/18 find_containing_dir(): use strbuf in implementation of this functionmhagger@alum.mit.edu, Apr 26, 2012
  20. 12/18 refs: wrap top-level ref_dirs in ref_entriesmhagger@alum.mit.edu, Apr 26, 2012
  21. 13/18 read_loose_refs(): rename function from get_ref_dir()mhagger@alum.mit.edu, Apr 26, 2012
  22. 14/18 get_ref_dir(): add function for getting a ref_dir from a ref_entrymhagger@alum.mit.edu, Apr 26, 2012
  23. 15/18 search_for_subdir(): return (ref_dir *) instead of (ref_entry *)mhagger@alum.mit.edu, Apr 26, 2012
  24. 16/18 struct ref_dir: store a reference to the enclosing ref_cachemhagger@alum.mit.edu, Apr 26, 2012
  25. 17/18 read_loose_refs(): eliminate ref_cache argumentmhagger@alum.mit.edu, Apr 26, 2012
  26. 18/18 refs: read loose references lazilymhagger@alum.mit.edu, Apr 26, 2012

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.