Skip to content

Commit 2e875b6

Browse files
pks-tgitster
authored andcommitted
builtin/stash: fix various trivial memory leaks
There are multiple trivial memory leaks in git-stash(1). Fix those. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fc68633 commit 2e875b6

File tree

6 files changed

+22
-2
lines changed

6 files changed

+22
-2
lines changed

builtin/stash.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
15211521
struct strbuf patch = STRBUF_INIT;
15221522
struct strbuf stash_msg_buf = STRBUF_INIT;
15231523
struct strbuf untracked_files = STRBUF_INIT;
1524+
struct strbuf out = STRBUF_INIT;
15241525

15251526
if (patch_mode && keep_index == -1)
15261527
keep_index = 1;
@@ -1626,7 +1627,6 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
16261627
struct child_process cp_add = CHILD_PROCESS_INIT;
16271628
struct child_process cp_diff = CHILD_PROCESS_INIT;
16281629
struct child_process cp_apply = CHILD_PROCESS_INIT;
1629-
struct strbuf out = STRBUF_INIT;
16301630

16311631
cp_add.git_cmd = 1;
16321632
strvec_push(&cp_add.args, "add");
@@ -1718,6 +1718,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
17181718

17191719
done:
17201720
strbuf_release(&patch);
1721+
strbuf_release(&out);
17211722
free_stash_info(&info);
17221723
strbuf_release(&stash_msg_buf);
17231724
strbuf_release(&untracked_files);
@@ -1869,6 +1870,8 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
18691870
OPT_SUBCOMMAND_F("save", &fn, save_stash, PARSE_OPT_NOCOMPLETE),
18701871
OPT_END()
18711872
};
1873+
const char **args_copy;
1874+
int ret;
18721875

18731876
git_config(git_stash_config, NULL);
18741877

@@ -1892,5 +1895,16 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
18921895
/* Assume 'stash push' */
18931896
strvec_push(&args, "push");
18941897
strvec_pushv(&args, argv);
1895-
return !!push_stash(args.nr, args.v, prefix, 1);
1898+
1899+
/*
1900+
* `push_stash()` ends up modifying the array, which causes memory
1901+
* leaks if we didn't copy the array here.
1902+
*/
1903+
DUP_ARRAY(args_copy, args.v, args.nr);
1904+
1905+
ret = !!push_stash(args.nr, args_copy, prefix, 1);
1906+
1907+
strvec_clear(&args);
1908+
free(args_copy);
1909+
return ret;
18961910
}

t/t2501-cwd-empty.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
test_description='Test handling of the current working directory becoming empty'
44

5+
TEST_PASSES_SANITIZE_LEAK=true
56
. ./test-lib.sh
67

78
test_expect_success setup '

t/t3903-stash.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ test_description='Test git stash'
88
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
99
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
1010

11+
TEST_PASSES_SANITIZE_LEAK=true
1112
. ./test-lib.sh
1213
. "$TEST_DIRECTORY"/lib-unique-files.sh
1314

t/t3904-stash-patch.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/sh
22

33
test_description='stash -p'
4+
5+
TEST_PASSES_SANITIZE_LEAK=true
46
. ./lib-patch-mode.sh
57

68
test_expect_success 'setup' '

t/t3905-stash-include-untracked.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
test_description='Test git stash --include-untracked'
77

8+
TEST_PASSES_SANITIZE_LEAK=true
89
. ./test-lib.sh
910

1011
test_expect_success 'stash save --include-untracked some dirty working directory' '

t/t7064-wtstatus-pv2.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ test_description='git status --porcelain=v2
44
55
This test exercises porcelain V2 output for git status.'
66

7+
TEST_PASSES_SANITIZE_LEAK=true
78
. ./test-lib.sh
89

910

0 commit comments

Comments
 (0)