Skip to content

Commit 79366ad

Browse files
pks-tgitster
authored andcommittedNov 20, 2024
bisect: fix leaking good/bad terms when reading multipe times
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 65a1b7e commit 79366ad

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed
 

‎bisect.c

+9-5
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ static struct oid_array skipped_revs;
2828

2929
static struct object_id *current_bad_oid;
3030

31-
static const char *term_bad;
32-
static const char *term_good;
31+
static char *term_bad;
32+
static char *term_good;
3333

3434
/* Remember to update object flag allocation in object.h */
3535
#define COUNTED (1u<<16)
@@ -985,24 +985,28 @@ static void show_commit(struct commit *commit)
985985
* We read them and store them to adapt the messages accordingly.
986986
* Default is bad/good.
987987
*/
988-
void read_bisect_terms(const char **read_bad, const char **read_good)
988+
void read_bisect_terms(char **read_bad, char **read_good)
989989
{
990990
struct strbuf str = STRBUF_INIT;
991991
const char *filename = git_path_bisect_terms();
992992
FILE *fp = fopen(filename, "r");
993993

994994
if (!fp) {
995995
if (errno == ENOENT) {
996-
*read_bad = "bad";
997-
*read_good = "good";
996+
free(*read_bad);
997+
*read_bad = xstrdup("bad");
998+
free(*read_good);
999+
*read_good = xstrdup("good");
9981000
return;
9991001
} else {
10001002
die_errno(_("could not read file '%s'"), filename);
10011003
}
10021004
} else {
10031005
strbuf_getline_lf(&str, fp);
1006+
free(*read_bad);
10041007
*read_bad = strbuf_detach(&str, NULL);
10051008
strbuf_getline_lf(&str, fp);
1009+
free(*read_good);
10061010
*read_good = strbuf_detach(&str, NULL);
10071011
}
10081012
strbuf_release(&str);

‎bisect.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix);
7575

7676
int estimate_bisect_steps(int all);
7777

78-
void read_bisect_terms(const char **bad, const char **good);
78+
void read_bisect_terms(char **bad, char **good);
7979

8080
int bisect_clean_state(void);
8181

‎revision.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151

5252
volatile show_early_output_fn_t show_early_output;
5353

54-
static const char *term_bad;
55-
static const char *term_good;
54+
static char *term_bad;
55+
static char *term_good;
5656

5757
implement_shared_commit_slab(revision_sources, char *);
5858

0 commit comments

Comments
 (0)
Please sign in to comment.