about summary refs log tree commit diff
diff options
context:
space:
mode:
authorStephane Chazelas <stephane@chazelas.org>2024-02-18 18:22:37 +0000
committerStephane Chazelas <stephane@chazelas.org>2024-02-18 18:22:37 +0000
commitb3cad1c24cb588f5b0cbb617fe6f7fc0fade11af (patch)
tree88738e89ff36aac9d80c2a887c4291db79db810b
parent8c59638522d8ed06cb962d41c11d1fade27abaa9 (diff)
downloadzsh-b3cad1c24cb588f5b0cbb617fe6f7fc0fade11af.tar.gz
zsh-b3cad1c24cb588f5b0cbb617fe6f7fc0fade11af.tar.xz
zsh-b3cad1c24cb588f5b0cbb617fe6f7fc0fade11af.zip
52515: (+ tests in 52527) avoid sh errors when running shebang-less scripts with paths starting with - or +
-rw-r--r--ChangeLog5
-rw-r--r--Src/exec.c17
-rw-r--r--Test/A05execution.ztst16
3 files changed, 34 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 11cc8dd6b..df94007fe 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2024-02-18  Stephane Chazelas  <stephane@chazelas.org>
+
+	* 52515: Src/exec.c (+ tests in 52527) avoid sh errors when
+	  running shebang-less scripts with paths starting with - or +
+
 2024-02-17  Bart Schaefer  <schaefer@zsh.org>
 
 	* 52556: Src/builtin.c: fix crash when applying a type change via
diff --git a/Src/exec.c b/Src/exec.c
index 7d8135266..9c8bbb458 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -612,9 +612,22 @@ zexecve(char *pth, char **argv, char **newenvp)
                         }
                     }
 		    if (!isbinary) {
-			argv[-1] = "sh";
+		        char** args = argv;
+			if (argv[0][0] == '-' || argv[0][0] == '+') {
+			    /*
+			     * guard against +foo or -bar script paths being
+			     * taken as options. POSIX says the script path
+			     * must be passed as an *operand*. "--" would also
+			     * make sure the next argument is treated as an
+			     * operand with POSIX compliant sh implementations
+			     * but "-" is more portable (to the Bourne shell in
+			     * particular) and shorter.
+			     */
+			    *--args = "-";
+			}
+			*--args = "sh";
 			winch_unblock();
-			execve("/bin/sh", argv - 1, newenvp);
+			execve("/bin/sh", args, newenvp);
 		    }
 		}
 	    } else
diff --git a/Test/A05execution.ztst b/Test/A05execution.ztst
index bcadc6d56..07a24f9c8 100644
--- a/Test/A05execution.ztst
+++ b/Test/A05execution.ztst
@@ -2,7 +2,7 @@
 
   storepath=($path)
 
-  mkdir command.tmp command.tmp/dir1 command.tmp/dir2
+  mkdir command.tmp command.tmp/dir{1,2} command.tmp/{+,-}dir
 
   cd command.tmp
 
@@ -21,7 +21,10 @@
   print '#!xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxnyyy' >tstcmd-interp-too-long
   print "#!${sh}\necho should not execute; exit 1" >xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxn
 
-  chmod 755 tstcmd dir1/tstcmd dir2/tstcmd
+  print 'echo no shebang in -dir' > -dir/tstcmd
+  print 'echo no shebang in +dir' > +dir/tstcmd
+
+  chmod 755 tstcmd dir{1,2}/tstcmd ./{-,+}dir/tstcmd
   chmod 755 tstcmd-slashless tstcmd-arg tstcmd-interp-too-long
   chmod 755 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxn
 
@@ -422,3 +425,12 @@ F:anonymous function, and a descriptor leak when backgrounding a pipeline
  (exit 4); repeat 0 do done
 0:'repeat 0' resets lastval
 
+ -dir/tstcmd
+ +dir/tstcmd
+ PATH=-dir tstcmd
+ PATH=+dir tstcmd
+0:shebang-less scripts are to be run by sh even when their file paths start with - or + (workers/52515)
+>no shebang in -dir
+>no shebang in +dir
+>no shebang in -dir
+>no shebang in +dir