On Fri, Apr 09, 2021 at 11:53:39AM -0500, Peng Yu via Libc-help wrote: > Hi, > > I am wondering when (all possible legitimate cases) it is OK to pass > NULLs to execve's 2nd and 3rd args. > > #include > #include > > 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? I wouldn't count on that. It seems to depend on the operating system. I quote the Linux Programmer's Manual execve(2) man page from my box: On Linux, argv and envp can be specified as NULL. In both cases, this has the same effect as specifying the argument as a pointer to a list containing a single null pointer. **Do not take advantage of this nonstandard and nonportable misfeature!** On many other UNIX systems, specifying argv as NULL will result in an error (EFAULT). Some other UNIX systems treat the envp==NULL case the same as Linux. So... I wouldn't rely on it :-) Cheers - t