builtin/rev-parse: fix memory leak with --parseopt
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>
This commit is contained in:

committed by
Junio C Hamano

parent
2e875b6cb4
commit
2d197e4a0f
@ -553,7 +553,10 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
|
|||||||
strbuf_release(&sb);
|
strbuf_release(&sb);
|
||||||
strvec_clear(&longnames);
|
strvec_clear(&longnames);
|
||||||
strvec_clear(&usage);
|
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);
|
free(opts);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
test_description='test git rev-parse --parseopt'
|
test_description='test git rev-parse --parseopt'
|
||||||
|
|
||||||
|
TEST_PASSES_SANITIZE_LEAK=true
|
||||||
. ./test-lib.sh
|
. ./test-lib.sh
|
||||||
|
|
||||||
check_invalid_long_option () {
|
check_invalid_long_option () {
|
||||||
|
Reference in New Issue
Block a user