From bda94435fc647638d7c04c17022fd3a44bf2683a Mon Sep 17 00:00:00 2001 From: Chris Antos Date: Wed, 30 Dec 2020 04:42:05 -0800 Subject: [PATCH 1/3] 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%" From be90b1a5281c4c02b92de2b6916c2597c0998b2d Mon Sep 17 00:00:00 2001 From: Chris Antos Date: Wed, 30 Dec 2020 04:50:29 -0800 Subject: [PATCH 2/3] Match original output. Originally `z foo` printed a blank line after the command. My recent change accidentally changed how CMD interpreted the end of the file such that CMD stopped printing the blank line. This change adjusts the end of the file to once again cause CMD to print a blank line on finish. --- z.cmd | 1 - 1 file changed, 1 deletion(-) diff --git a/z.cmd b/z.cmd index fd86882..4ed4397 100644 --- a/z.cmd +++ b/z.cmd @@ -124,4 +124,3 @@ popd setlocal set NewPath=%CD% endlocal & popd & cd /d "%NewPath%" - From e646d5baa58b1202b9a3f906ee33d2ec29ee97a2 Mon Sep 17 00:00:00 2001 From: Chris Antos Date: Wed, 30 Dec 2020 05:00:26 -0800 Subject: [PATCH 3/3] A better fix for the final blank line. --- z.cmd | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/z.cmd b/z.cmd index 4ed4397..f632aea 100644 --- a/z.cmd +++ b/z.cmd @@ -113,9 +113,7 @@ if /i "%RunMode%"=="-n" ( call "%LuaExe%" "%LuaScript%" "%RunMode%" %MatchType% %StrictSub% %InterMode% %StripMode% %* ) -:end -echo. -goto :eof +goto end :popdir rem -- Exploits variable expansion and the pushd stack to set the current @@ -124,3 +122,7 @@ popd setlocal set NewPath=%CD% endlocal & popd & cd /d "%NewPath%" + +:end +echo. +