]> git.ipfire.org Git - thirdparty/git.git/commitdiff
mv: move src_dir cleanup to end of cmd_mv()
authorJeff King <peff@peff.net>
Thu, 30 May 2024 06:44:22 +0000 (02:44 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 30 May 2024 15:55:29 +0000 (08:55 -0700)
Commit b6f51e3db9 (mv: cleanup empty WORKING_DIRECTORY, 2022-08-09)
added an auxiliary array where we store directory arguments that we see
while processing the incoming arguments. After actually moving things,
we then use that array to remove now-empty directories, and then
immediately free the array.

But if the actual move queues any errors in only_match_skip_worktree,
that can cause us to jump straight to the "out" label to clean up,
skipping the free() and leaking the array.

Let's push the free() down past the "out" label so that we always clean
up (the array is initialized to NULL, so this is always safe). We'll
hold on to the memory a little longer than necessary, but clarity is
more important than micro-optimizing here.

Note that the adjacent "a_src_dir" strbuf does not suffer the same
problem; it is only allocated during the removal step.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/mv.c

index 81ca910de6fc35f4243619e6995ddc50696fcdff..852b4e92c1f360801e4a77931c9adc65db903607 100644 (file)
@@ -556,7 +556,6 @@ remove_entry:
        }
 
        strbuf_release(&a_src_dir);
-       free(src_dir);
 
        if (dirty_paths.nr)
                advise_on_moving_dirty_path(&dirty_paths);
@@ -571,6 +570,7 @@ remove_entry:
        ret = 0;
 
 out:
+       free(src_dir);
        free(dst_w_slash);
        string_list_clear(&src_for_dst, 0);
        string_list_clear(&dirty_paths, 0);