From bda94435fc647638d7c04c17022fd3a44bf2683a Mon Sep 17 00:00:00 2001 From: Chris Antos Date: Wed, 30 Dec 2020 04:42:05 -0800 Subject: [PATCH] Fix leaked pushd. `z somedir` uses double `pushd` to tunnel the new path out past the endlocal that restores the environment state. But it ended up leaving a pushd on the dir stack. This change exploits normal variable expansion to restore the dir stack to its initial state while still successfully tunneling the new path out past the endlocal. --- z.cmd | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/z.cmd b/z.cmd index b917f5f..fd86882 100644 --- a/z.cmd +++ b/z.cmd @@ -106,7 +106,7 @@ if /i "%RunMode%"=="-n" ( pushd !NewPath! pushd !NewPath! endlocal - popd + goto popdir ) ) ) else ( @@ -115,4 +115,13 @@ if /i "%RunMode%"=="-n" ( :end echo. +goto :eof + +:popdir +rem -- Exploits variable expansion and the pushd stack to set the current +rem -- directory without leaking a pushd. +popd +setlocal +set NewPath=%CD% +endlocal & popd & cd /d "%NewPath%"