mv: free the with_slash in check_dir_in_index()
with_slash may be a malloc'd pointer, and when it is, free it. Helped-by: Derrick Stolee <derrickstolee@github.com> Helped-by: Victoria Dye <vdye@github.com> Signed-off-by: Shaoxuan Yuan <shaoxuan.yuan02@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Shaoxuan Yuan committed
Aug 9, 2022 at 20:09 UTC
d57690a9c82c8888be6bb8ae17be231a2b2802e6
1 file changed
+9
-4
builtin/mv.c
+9
-4
@@ -133,6 +133,7 @@ static int index_range_of_same_dir(const char *src, int length,
133
*/
134
static int empty_dir_has_sparse_contents(const char *name)
135
{
136
+ int ret = 0;
137
const char *with_slash = add_slash(name);
138
int length = strlen(with_slash);
139
@@ -142,14 +143,18 @@ static int empty_dir_has_sparse_contents(const char *name)
143
if (pos < 0) {
144
pos = -pos - 1;
145
if (pos >= the_index.cache_nr)
145
- return 0;
146
+ goto free_return;
147
ce = active_cache[pos];
148
if (strncmp(with_slash, ce->name, length))
148
- return 0;
149
+ goto free_return;
150
if (ce_skip_worktree(ce))
150
- return 1;
151
+ ret = 1;
152
}
152
- return 0;
153
+
154
+free_return:
155
+ if (with_slash != name)
156
+ free((char *)with_slash);
157
+ return ret;
158
}
159
160
int cmd_mv(int argc, const char **argv, const char *prefix)