Skip to content

Commit 2d197e4

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2e875b6 commit 2d197e4

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

builtin/rev-parse.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,10 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
553553
strbuf_release(&sb);
554554
strvec_clear(&longnames);
555555
strvec_clear(&usage);
556-
free((char *) opts->help);
556+
for (size_t i = 0; i < opts_nr; i++) {
557+
free((char *) opts[i].help);
558+
free((char *) opts[i].argh);
559+
}
557560
free(opts);
558561
return 0;
559562
}

t/t1502-rev-parse-parseopt.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/sh
22

33
test_description='test git rev-parse --parseopt'
4+
5+
TEST_PASSES_SANITIZE_LEAK=true
46
. ./test-lib.sh
57

68
check_invalid_long_option () {

0 commit comments

Comments
 (0)