shithub: mc

Download patch

ref: 49e3f29e0cea0d9a58effe3d2aee0679eca68fdc
parent: 2cf47897e67a6d07d2b29c6afdac6275571ea57d
author: Ori Bernstein <[email protected]>
date: Wed Jan 8 20:27:48 EST 2014

Fix execv, execve, execp

--- a/libstd/execvp.myr
+++ b/libstd/execvp.myr
@@ -11,7 +11,7 @@
 ;;
 
 const execvp = {cmd, args
-	var path
+	var path, cmdlen
 	var split
 	var buf : byte[512]
 
@@ -22,8 +22,8 @@
 
 	split = strsplit(path, ":")
 	for s in split
-		bfmt(buf[:], "%s/%s", s, cmd)
-		execv(cmd, args)
+		cmdlen = bfmt(buf[:], "%s/%s", s, cmd)
+		execv(buf[:cmdlen], args)
 	;;
 	slfree(split)
 	-> -1
@@ -30,7 +30,7 @@
 }
 
 const execvpe = {cmd, args, env
-	var path
+	var path, cmdlen
 	var split
 	var buf : byte[512]
 
@@ -41,8 +41,8 @@
 
 	split = strsplit(path, ":")
 	for s in split
-		bfmt(buf[:], "%s/%s", s, cmd)
-		execve(cmd, args, env)
+		cmdlen = bfmt(buf[:], "%s/%s", s, cmd)
+		execve(buf[:cmdlen], args, env)
 	;;
 	slfree(split)
 	-> -1
--- a/libstd/start-linux.s
+++ b/libstd/start-linux.s
@@ -82,12 +82,11 @@
 	subq	%rax,%rsp
 	movq	%rsp, %rdx	/* saved args[:] */
 
-        /* store envp for some syscalls to use without converting */
-	movq	16(%rbp,%rax,8), %rbx	/* envp = argv + 8*argc + 8 */
-        movq    %rbx,std$__cenvp(%rip)
 	/* convert envp to byte[:][:] for std._environment */
 	movq	(%rbp),%rax
 	leaq	16(%rbp,%rax,8), %rbx	/* envp = argv + 8*argc + 8 */
+        /* store envp for some syscalls to use without converting */
+        movq    %rbx,std$__cenvp(%rip)
 	movq	%r9,%rax
 	movq	%rsp, %rcx
 	movq	%r9,.envlen
--- a/libstd/start-osx.s
+++ b/libstd/start-osx.s
@@ -81,12 +81,10 @@
 	subq	%rax,%rsp
 	movq	%rsp, %rdx	/* saved args[:] */
 
-        /* store envp for some syscalls to use without converting */
-	movq	16(%rbp,%rax,8), %rbx	/* envp = argv + 8*argc + 8 */
-        movq    %rbx,_std$__cenvp(%rip)
 	/* convert envp to byte[:][:] for std._environment */
 	movq	(%rbp),%rax
 	leaq	16(%rbp,%rax,8), %rbx	/* envp = argv + 8*argc + 8 */
+        movq    %rbx,_std$__cenvp(%rip)
 	movq	%r9,%rax
 	movq	%rsp, %rcx
 	movq	%r9,.envlen(%rip)
--- a/libstd/sys-linux.myr
+++ b/libstd/sys-linux.myr
@@ -507,23 +507,26 @@
 }
 
 const execv	= {cmd, args
-	var cargs, i
+	var p, cargs, i
 
 	/* of course we fucking have to duplicate this code everywhere,
 	* since we want to stack allocate... */
-	cargs = (alloca(sizeof(byte#)*(args.len + 1)) castto(byte##))[:args.len]
+	p = alloca((args.len + 1)*sizeof(byte#))
+	cargs = (p castto(byte##))[:args.len]
 	for i = 0; i < args.len; i++
 		cargs[i] = cstring(args[i])
 	;;
 	cargs[args.len] = 0 castto(byte#)
-	-> syscall(Sysexecve, cmd castto(byte#), cargs, __cenvp)
+	-> syscall(Sysexecve, cstring(cmd), p, __cenvp)
 }
 
 const execve	= {cmd, args, env
 	var cargs, cenv, i
+	var p
 
 	/* copy the args */
-	cargs = (alloca(sizeof(byte#)*(args.len + 1)) castto(byte##))[:args.len]
+	p = alloca((args.len + 1)*sizeof(byte#))
+	cargs = (p castto(byte##))[:args.len]
 	for i = 0; i < args.len; i++
 		cargs[i] = cstring(args[i])
 	;;
@@ -534,13 +537,14 @@
 	 of course we fucking have to duplicate this code everywhere,
 	 since we want to stack allocate... 
 	*/
-	cenv = (alloca(sizeof(byte#)*(env.len + 1)) castto(byte##))[:env.len]
+	p = alloca((env.len + 1)*sizeof(byte#))
+	cenv = (p castto(byte##))[:env.len]
 	for i = 0; i < env.len; i++
 		cenv[i] = cstring(env[i])
 	;;
 	cenv[env.len] = 0 castto(byte#)
 
-	-> syscall(Sysexecve, cmd castto(byte#), cargs, cenv)
+	-> syscall(Sysexecve, cstring(cmd), p, cenv)
 }
 
 /* fd manipulation */