git: remove an early return from save_env_before_alias()

When help.autocorrect is in effect, an attempt to auto-execute an uniquely corrected result of a misspelt alias will result in an irrelevant error message. The codepath that causes this calls save_env_before_alias() and restore_env() in handle_alias(), and that happens twice. A global variable orig_cwd is allocated to hold the return value of getcwd() in save_env_before_alias(), which is then used in restore_env() to go back to that directory and finally free(3)'d there. However, save_env_before_alias() is not prepared to be called twice. It returns early when it knows it has already been called, leaving orig_cwd undefined, which is then checked in the second call to restore_env(), and by that time, the memory that used to hold the contents of orig_cwd is either freed or reused to hold something else, and this is fed to chdir(2), causing it to fail. Even if it did not fail (i.e. reading of the already free'd piece of memory yielded a directory path that we can chdir(2) to), it then gets free(3)'d. Fix this by making sure save_env() does do the saving when called. While at it, add a minimal test for help.autocorrect facility. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jan 26, 2016 at 11:46 UTC 9d1d2b7fad9bec6320a2058c625787c835864960
2 files changed +52 -2
git.c
-2
@@ -30,8 +30,6 @@ static int saved_env_before_alias;
30 static void save_env_before_alias(void)
31 {
32 int i;
33 - if (saved_env_before_alias)
34 - return;
33 saved_env_before_alias = 1;
34 orig_cwd = xgetcwd();
35 for (i = 0; i < ARRAY_SIZE(env_names); i++) {
t/t9003-help-autocorrect.sh new
+52
@@ -0,0 +1,52 @@
1 +#!/bin/sh
2 +
3 +test_description='help.autocorrect finding a match'
4 +. ./test-lib.sh
5 +
6 +test_expect_success 'setup' '
7 + # An alias
8 + git config alias.lgf "log --format=%s --first-parent" &&
9 +
10 + # A random user-defined command
11 + write_script git-distimdistim <<-EOF &&
12 + echo distimdistim was called
13 + EOF
14 +
15 + PATH="$PATH:." &&
16 + export PATH &&
17 +
18 + git commit --allow-empty -m "a single log entry" &&
19 +
20 + # Sanity check
21 + git lgf >actual &&
22 + echo "a single log entry" >expect &&
23 + test_cmp expect actual &&
24 +
25 + git distimdistim >actual &&
26 + echo "distimdistim was called" >expect &&
27 + test_cmp expect actual
28 +'
29 +
30 +test_expect_success 'autocorrect showing candidates' '
31 + git config help.autocorrect 0 &&
32 +
33 + test_must_fail git lfg 2>actual &&
34 + sed -e "1,/^Did you mean this/d" actual | grep lgf &&
35 +
36 + test_must_fail git distimdist 2>actual &&
37 + sed -e "1,/^Did you mean this/d" actual | grep distimdistim
38 +'
39 +
40 +test_expect_success 'autocorrect running commands' '
41 + git config help.autocorrect -1 &&
42 +
43 + git lfg >actual &&
44 + echo "a single log entry" >expect &&
45 + test_cmp expect actual &&
46 +
47 + git distimdist >actual &&
48 + echo "distimdistim was called" >expect &&
49 + test_cmp expect actual
50 +'
51 +
52 +test_done