Skip to content

Commit bda9443

Browse files
committed
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.
1 parent c0ff299 commit bda9443

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

z.cmd

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ if /i "%RunMode%"=="-n" (
106106
pushd !NewPath!
107107
pushd !NewPath!
108108
endlocal
109-
popd
109+
goto popdir
110110
)
111111
)
112112
) else (
@@ -115,4 +115,13 @@ if /i "%RunMode%"=="-n" (
115115

116116
:end
117117
echo.
118+
goto :eof
119+
120+
:popdir
121+
rem -- Exploits variable expansion and the pushd stack to set the current
122+
rem -- directory without leaking a pushd.
123+
popd
124+
setlocal
125+
set NewPath=%CD%
126+
endlocal & popd & cd /d "%NewPath%"
118127

0 commit comments

Comments
 (0)