Skip to content

Commit 844d190

Browse files
pks-tgitster
authored andcommitted
revision: always store allocated strings in output encoding
The `git_log_output_encoding` variable can be set via the `--encoding=` option. When doing so, we conditionally either assign it to the passed value, or if the value is "none" we assign it the empty string. Depending on which of the both code paths we pick though, the variable may end up being assigned either an allocated string or a string constant. This is somewhat risky and may easily lead to bugs when a different code path may want to reassign a new value to it, freeing the previous value. We already to this when parsing the "i18n.logoutputencoding" config in `git_default_i18n_config()`. But because the config is typically parsed before we parse command line options this has been fine so far. Regardless of that, safeguard the code such that the variable always contains an allocated string. While at it, also free the old value in case there was any to plug a potential memory leak. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a3da694 commit 844d190

File tree

3 files changed

+4
-1
lines changed

3 files changed

+4
-1
lines changed

revision.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2650,10 +2650,11 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
26502650
} else if (!strcmp(arg, "--invert-grep")) {
26512651
revs->grep_filter.no_body_match = 1;
26522652
} else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2653+
free(git_log_output_encoding);
26532654
if (strcmp(optarg, "none"))
26542655
git_log_output_encoding = xstrdup(optarg);
26552656
else
2656-
git_log_output_encoding = "";
2657+
git_log_output_encoding = xstrdup("");
26572658
return argcount;
26582659
} else if (!strcmp(arg, "--reverse")) {
26592660
revs->reverse ^= 1;

t/t3900-i18n-commit.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
test_description='commit and log output encodings'
77

8+
TEST_PASSES_SANITIZE_LEAK=true
89
. ./test-lib.sh
910

1011
compare_with () {

t/t3901-i18n-patch.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ test_description='i18n settings and format-patch | am pipe'
88
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
99
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
1010

11+
TEST_PASSES_SANITIZE_LEAK=true
1112
. ./test-lib.sh
1213

1314
check_encoding () {

0 commit comments

Comments
 (0)