]> git.ipfire.org Git - thirdparty/git.git/commitdiff
config: fix a leak in git_config_copy_or_rename_section_in_file
authorRubén Justo <rjusto@gmail.com>
Sat, 17 Jun 2023 06:41:40 +0000 (08:41 +0200)
committerJunio C Hamano <gitster@pobox.com>
Sat, 17 Jun 2023 16:02:48 +0000 (09:02 -0700)
A branch can have its configuration spread over several configuration
sections.  This situation was already foreseen in 52d59cc645 (branch:
add a --copy (-c) option to go with --move (-m), 2017-06-18) when
"branch -c" was introduced.

Unfortunately, a leak was also introduced:

   $ git branch foo
   $ cat >> .git/config <<EOF
   [branch "foo"]
    some-key-a = a value
   [branch "foo"]
    some-key-b = b value
   [branch "foo"]
    some-key-c = c value
   EOF
   $ git branch -c foo bar

   Direct leak of 130 byte(s) in 2 object(s) allocated from:
       ... in xrealloc wrapper.c
       ... in strbuf_grow strbuf.c
       ... in strbuf_vaddf strbuf.c
       ... in strbuf_addf strbuf.c
       ... in store_create_section config.c
       ... in git_config_copy_or_rename_section_in_file config.c
       ... in git_config_copy_section_in_file config.c
       ... in git_config_copy_section config.c
       ... in copy_or_rename_branch builtin/branch.c
       ... in cmd_branch builtin/branch.c
       ... in run_builtin git.c

Let's fix it.

Signed-off-by: Rubén Justo <rjusto@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
config.c

index 39a7d7422c2061c66ff2db0803c811598d3d06f8..207e4394a3c9837f4c00c691a17e12fef8d46550 100644 (file)
--- a/config.c
+++ b/config.c
@@ -3833,6 +3833,7 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
                                                output[0] = '\t';
                                        }
                                } else {
+                                       strbuf_release(&copystr);
                                        copystr = store_create_section(new_name, &store);
                                }
                        }