]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/rev-parse: fix memory leak with `--parseopt`
authorPatrick Steinhardt <ps@pks.im>
Thu, 1 Aug 2024 10:40:21 +0000 (12:40 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 1 Aug 2024 15:47:36 +0000 (08:47 -0700)
The `--parseopt` mode allows shell scripts to have the same option
parsing mode as we have in C builtins. It soaks up a set of option
descriptions via stdin and massages them into proper `struct option`s
that we can then use to parse a set of arguments.

We only partially free those options when done though, creating a memory
leak. Interestingly, we only end up free'ing the first option's help,
which is of course wrong.

Fix this by freeing all option's help fields as well as their `argh`
fields to plug this memory leak.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/rev-parse.c
t/t1502-rev-parse-parseopt.sh

index 2e64f5bda70bc2e41a7477d919548a13ba68bd3f..5845d3f59b934d516c564be0d52ef5f0ea7b2f5c 100644 (file)
@@ -553,7 +553,10 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
        strbuf_release(&sb);
        strvec_clear(&longnames);
        strvec_clear(&usage);
-       free((char *) opts->help);
+       for (size_t i = 0; i < opts_nr; i++) {
+               free((char *) opts[i].help);
+               free((char *) opts[i].argh);
+       }
        free(opts);
        return 0;
 }
index b754b9fd74bd17e7a969026a93cd6e70c8771cb8..5eaa6428c43917515f95baed1c1cf4ad2cb6110b 100755 (executable)
@@ -1,6 +1,8 @@
 #!/bin/sh
 
 test_description='test git rev-parse --parseopt'
+
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 check_invalid_long_option () {