Skip to content

Commit 2e1175d

Browse files
committed
git: protect against unbalanced calls to {save,restore}_env()
We made sure that save_env_before_alias() does not skip saving the environment when asked to (which led to use-after-free of orig_cwd in restore_env() in the buggy version) with the previous step. Protect against future breakage where somebody adds new callers of these functions in an unbalanced fashion. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9d1d2b7 commit 2e1175d

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

git.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@ static const char *env_names[] = {
2626
};
2727
static char *orig_env[4];
2828
static int saved_env_before_alias;
29+
static int save_restore_env_balance;
2930

3031
static void save_env_before_alias(void)
3132
{
3233
int i;
3334
saved_env_before_alias = 1;
35+
36+
assert(save_restore_env_balance == 0);
37+
save_restore_env_balance = 1;
3438
orig_cwd = xgetcwd();
3539
for (i = 0; i < ARRAY_SIZE(env_names); i++) {
3640
orig_env[i] = getenv(env_names[i]);
@@ -42,6 +46,9 @@ static void save_env_before_alias(void)
4246
static void restore_env(int external_alias)
4347
{
4448
int i;
49+
50+
assert(save_restore_env_balance == 1);
51+
save_restore_env_balance = 0;
4552
if (!external_alias && orig_cwd && chdir(orig_cwd))
4653
die_errno("could not move to %s", orig_cwd);
4754
free(orig_cwd);

0 commit comments

Comments
 (0)