diff mbox series

[v2,02/27] bisect: fix leaking good/bad terms when reading multipe times

Message ID 20241111-b4-pks-leak-fixes-pt10-v2-2-6154bf91f0b0@pks.im (mailing list archive)
State New
Headers show
Series Memory leak fixes (pt.10, final) | expand

Commit Message

Patrick Steinhardt Nov. 11, 2024, 10:38 a.m. UTC
Even though `read_bisect_terms()` is declared as assigning string
constants, it in fact assigns allocated strings to the `read_bad` and
`read_good` out parameters. The only callers of this function assign the
result to global variables and thus don't have to free them in order to
be leak-free. But that changes when executing the function multiple
times because we'd then overwrite the previous value and thus make it
unreachable.

Fix the function signature and free the previous values. This leak is
exposed by t0630, but plugging it does not make the whole test suite
pass.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 bisect.c   | 14 +++++++++-----
 bisect.h   |  2 +-
 revision.c |  4 ++--
 3 files changed, 12 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/bisect.c b/bisect.c
index 4713226fad4e16ebbda4ba2e8d13c6eaf7d4bfb2..9c52241050c14ecbf3f7950a0c5e7b962fcaa541 100644
--- a/bisect.c
+++ b/bisect.c
@@ -28,8 +28,8 @@  static struct oid_array skipped_revs;
 
 static struct object_id *current_bad_oid;
 
-static const char *term_bad;
-static const char *term_good;
+static char *term_bad;
+static char *term_good;
 
 /* Remember to update object flag allocation in object.h */
 #define COUNTED		(1u<<16)
@@ -985,7 +985,7 @@  static void show_commit(struct commit *commit)
  * We read them and store them to adapt the messages accordingly.
  * Default is bad/good.
  */
-void read_bisect_terms(const char **read_bad, const char **read_good)
+void read_bisect_terms(char **read_bad, char **read_good)
 {
 	struct strbuf str = STRBUF_INIT;
 	const char *filename = git_path_bisect_terms();
@@ -993,16 +993,20 @@  void read_bisect_terms(const char **read_bad, const char **read_good)
 
 	if (!fp) {
 		if (errno == ENOENT) {
-			*read_bad = "bad";
-			*read_good = "good";
+			free(*read_bad);
+			*read_bad = xstrdup("bad");
+			free(*read_good);
+			*read_good = xstrdup("good");
 			return;
 		} else {
 			die_errno(_("could not read file '%s'"), filename);
 		}
 	} else {
 		strbuf_getline_lf(&str, fp);
+		free(*read_bad);
 		*read_bad = strbuf_detach(&str, NULL);
 		strbuf_getline_lf(&str, fp);
+		free(*read_good);
 		*read_good = strbuf_detach(&str, NULL);
 	}
 	strbuf_release(&str);
diff --git a/bisect.h b/bisect.h
index ee3fd65f3bdb89611d7575239ee79e8c50bcd703..944439bfac729cf092d6c5962c9edff2321002e4 100644
--- a/bisect.h
+++ b/bisect.h
@@ -75,7 +75,7 @@  enum bisect_error bisect_next_all(struct repository *r, const char *prefix);
 
 int estimate_bisect_steps(int all);
 
-void read_bisect_terms(const char **bad, const char **good);
+void read_bisect_terms(char **bad, char **good);
 
 int bisect_clean_state(void);
 
diff --git a/revision.c b/revision.c
index 8df75b82249b36e7e3ee9a4a151f085c189acc45..347dabf7f9a1a87765150ec998deae9b6709b5f6 100644
--- a/revision.c
+++ b/revision.c
@@ -51,8 +51,8 @@ 
 
 volatile show_early_output_fn_t show_early_output;
 
-static const char *term_bad;
-static const char *term_good;
+static char *term_bad;
+static char *term_good;
 
 implement_shared_commit_slab(revision_sources, char *);