When is it OK to pass NULLs to the 2nd and 3rd args of execve()?

Mike Frysinger vapier@gentoo.org
Fri Apr 9 22:06:37 GMT 2021


On 09 Apr 2021 11:53, Peng Yu via Libc-help wrote:
> I am wondering when (all possible legitimate cases) it is OK to pass
> NULLs to execve's 2nd and 3rd args.
> 
> #include <unistd.h>
> #include <stdio.h>
> 
> int main(int argc, char *argv[]) {
> 	if(execve(argv[1], NULL, NULL) == -1) {
> 		perror("execvp()");
> 		return 1;
> 	}
> 	return 0;
> }
> 
> 
> I tried the above program. It works when it is called with `/bin/sh`.
> My guess is that when the program being exec'ed don't use its args and
> envs, then then 2nd and 3rd args of execve() can be NULLs. Could
> anybody correct me if I am wrong?

never do this.  POSIX doesn't allow it, and no programs account for it.
if you were try to report it as such, you'd (rightly) be told no.
i recall coreutils for example segfaults when it tries to deref argv[0].

https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html
-mike


More information about the Libc-help mailing list