Skip to content

Commit 192a3fa

Browse files
committed
Merge branch 'ab/plug-random-leaks'
Leakfix. * ab/plug-random-leaks: reflog: free() ref given to us by dwim_log() submodule--helper: fix small memory leaks clone: fix a memory leak of the "git_dir" variable grep: fix a "path_list" memory leak grep: use object_array_clear() in cmd_grep() grep: prefer "struct grep_opt" over its "void *" equivalent
2 parents 55b99fe + 3c81504 commit 192a3fa

File tree

5 files changed

+16
-8
lines changed

5 files changed

+16
-8
lines changed

builtin/clone.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,8 +1040,10 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
10401040
init_db(git_dir, real_git_dir, option_template, GIT_HASH_UNKNOWN, NULL,
10411041
INIT_DB_QUIET);
10421042

1043-
if (real_git_dir)
1043+
if (real_git_dir) {
1044+
free((char *)git_dir);
10441045
git_dir = real_git_dir;
1046+
}
10451047

10461048
/*
10471049
* additional config can be injected with -c, make sure it's included

builtin/grep.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ static void *run(void *arg)
199199
grep_source_clear_data(&w->source);
200200
work_done(w);
201201
}
202-
free_grep_patterns(arg);
203-
free(arg);
202+
free_grep_patterns(opt);
203+
free(opt);
204204

205205
return (void*) (intptr_t) hit;
206206
}
@@ -401,7 +401,7 @@ static void append_path(struct grep_opt *opt, const void *data, size_t len)
401401

402402
if (len == 1 && *(const char *)data == '\0')
403403
return;
404-
string_list_append(path_list, xstrndup(data, len));
404+
string_list_append_nodup(path_list, xstrndup(data, len));
405405
}
406406

407407
static void run_pager(struct grep_opt *opt, const char *prefix)
@@ -839,7 +839,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
839839
struct grep_opt opt;
840840
struct object_array list = OBJECT_ARRAY_INIT;
841841
struct pathspec pathspec;
842-
struct string_list path_list = STRING_LIST_INIT_NODUP;
842+
struct string_list path_list = STRING_LIST_INIT_DUP;
843843
int i;
844844
int dummy;
845845
int use_index = 1;
@@ -1159,8 +1159,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
11591159
strbuf_addf(&buf, "+/%s%s",
11601160
strcmp("less", pager) ? "" : "*",
11611161
opt.pattern_list->pattern);
1162-
string_list_append(&path_list,
1163-
strbuf_detach(&buf, NULL));
1162+
string_list_append_nodup(&path_list,
1163+
strbuf_detach(&buf, NULL));
11641164
}
11651165
}
11661166

@@ -1195,7 +1195,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
11951195
if (hit && show_in_pager)
11961196
run_pager(&opt, prefix);
11971197
clear_pathspec(&pathspec);
1198+
string_list_clear(&path_list, 0);
11981199
free_grep_patterns(&opt);
1200+
object_array_clear(&list);
11991201
free_repos();
12001202
return !hit;
12011203
}

builtin/reflog.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
653653
should_expire_reflog_ent,
654654
reflog_expiry_cleanup,
655655
&cb);
656+
free(ref);
656657
}
657658
return status;
658659
}

builtin/submodule--helper.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3220,6 +3220,7 @@ static void die_on_index_match(const char *path, int force)
32203220
}
32213221
free(ps_matched);
32223222
}
3223+
clear_pathspec(&ps);
32233224
}
32243225

32253226
static void die_on_repo_without_commits(const char *path)
@@ -3231,6 +3232,7 @@ static void die_on_repo_without_commits(const char *path)
32313232
if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
32323233
die(_("'%s' does not have a commit checked out"), path);
32333234
}
3235+
strbuf_release(&sb);
32343236
}
32353237

32363238
static int module_add(int argc, const char **argv, const char *prefix)

t/t7811-grep-open.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
test_description='git grep --open-files-in-pager
44
'
55

6+
TEST_PASSES_SANITIZE_LEAK=true
67
. ./test-lib.sh
78
. "$TEST_DIRECTORY"/lib-pager.sh
89
unset PAGER GIT_PAGER
@@ -114,8 +115,8 @@ test_expect_success 'modified file' '
114115
unrelated
115116
EOF
116117
118+
test_when_finished "git reset --hard" &&
117119
echo "enum grep_pat_token" >unrelated &&
118-
test_when_finished "git checkout HEAD unrelated" &&
119120
GIT_PAGER=./less git grep -F -O "enum grep_pat_token" >out &&
120121
test_cmp expect actual &&
121122
test_must_be_empty out

0 commit comments

Comments
 (0)