@matheusbsbs / matheus-bagliano / commits / 4394efecfa
Fork 0

make git barf when an alias changes environment variables

Aliases changing environment variables (GIT_DIR or GIT_WORK_TREE) can cause problems: git has to use GIT_DIR to read the aliases from the config. After running handle_options for the alias the options of the alias may have changed environment variables. Depending on the implementation of setenv the memory location obtained through getenv earlier may contain the old value or the new value (or even be used for something else?). To avoid these problems git errors out if an alias uses any option which changes environment variables. Signed-off-by: Matthias Lederhofer <matled@gmx.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matthias Lederhofer committed Jun 8, 2007 at 22:57 UTC 4394efecfa9f94ad14fe49bc9f499c5806aa83af
1 file changed +18 -4
git.c
+18 -4
@@ -28,7 +28,7 @@ static void prepend_to_path(const char *dir, int len)
28 free(path);
29 }
30
31 -static int handle_options(const char*** argv, int* argc)
31 +static int handle_options(const char*** argv, int* argc, int* envchanged)
32 {
33 int handled = 0;
34
@@ -64,24 +64,34 @@ static int handle_options(const char*** argv, int* argc)
64 usage(git_usage_string);
65 }
66 setenv(GIT_DIR_ENVIRONMENT, (*argv)[1], 1);
67 + if (envchanged)
68 + *envchanged = 1;
69 (*argv)++;
70 (*argc)--;
71 handled++;
72 } else if (!prefixcmp(cmd, "--git-dir=")) {
73 setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1);
74 + if (envchanged)
75 + *envchanged = 1;
76 } else if (!strcmp(cmd, "--work-tree")) {
77 if (*argc < 2) {
78 fprintf(stderr, "No directory given for --work-tree.\n" );
79 usage(git_usage_string);
80 }
81 setenv(GIT_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
82 + if (envchanged)
83 + *envchanged = 1;
84 (*argv)++;
85 (*argc)--;
86 } else if (!prefixcmp(cmd, "--work-tree=")) {
87 setenv(GIT_WORK_TREE_ENVIRONMENT, cmd + 12, 1);
88 + if (envchanged)
89 + *envchanged = 1;
90 } else if (!strcmp(cmd, "--bare")) {
91 static char git_dir[PATH_MAX+1];
92 setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 1);
93 + if (envchanged)
94 + *envchanged = 1;
95 } else {
96 fprintf(stderr, "Unknown option: %s\n", cmd);
97 usage(git_usage_string);
@@ -160,7 +170,7 @@ static int split_cmdline(char *cmdline, const char ***argv)
170
171 static int handle_alias(int *argcp, const char ***argv)
172 {
163 - int nongit = 0, ret = 0, saved_errno = errno;
173 + int nongit = 0, envchanged = 0, ret = 0, saved_errno = errno;
174 const char *subdir;
175 int count, option_count;
176 const char** new_argv;
@@ -181,7 +191,11 @@ static int handle_alias(int *argcp, const char ***argv)
191 alias_string + 1, alias_command);
192 }
193 count = split_cmdline(alias_string, &new_argv);
184 - option_count = handle_options(&new_argv, &count);
194 + option_count = handle_options(&new_argv, &count, &envchanged);
195 + if (envchanged)
196 + die("alias '%s' changes environment variables\n"
197 + "You can use '!git' in the alias to do this.",
198 + alias_command);
199 memmove(new_argv - option_count, new_argv,
200 count * sizeof(char *));
201 new_argv -= option_count;
@@ -375,7 +389,7 @@ int main(int argc, const char **argv, char **envp)
389 /* Look for flags.. */
390 argv++;
391 argc--;
378 - handle_options(&argv, &argc);
392 + handle_options(&argv, &argc, NULL);
393 if (argc > 0) {
394 if (!prefixcmp(argv[0], "--"))
395 argv[0] += 2;