https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=298747

--- Comment #4 from Leo Bicknell <[email protected]> ---
(In reply to Konstantin Belousov from comment #3)

Yes, that is the exact question.  And now I'm kinda sorry I went down this
rabbit hole at all, but I did...

The direct issue.  With this patch this program:

#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[], char *envp[]) { 
  clearenv(); 
  printf("envp == %p\n", (void *)envp);
  return 0; 
}

Will print out a pointer when called with something in the environment, and
will print out NULL when called with "env -i ./a.out".  I think that would
surprise many developers.  

Now, is NULL legal/valid?  Comparing with other docs:

man 7 environ says "An array of strings", and does not contain the word NULL.

man 2 execve says "The argument envp is also a pointer to a null-terminated
array of character pointers to null-terminated strings."

man 2 exec also states EFAULT is returned if "The path, argv, or envp arguments
point to an illegal address".

However, I also think that last man page is not completely accurate.  If I
point to a truly illegal address I get EFAULT as expected

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

int main(int argc, char *argv[], char *envp[]) {
  char *myargs[] = { "echo", "hello", NULL };
  char **badenv = (char **)0xAABBCCDDEEFFAABB;
  int x = execvpe("/bin/echo", myargs, badenv);
  printf("x = %d - %d %s\n", x, errno, strerror(errno));
}

Executing it on my 15.1 box it returns:

x = -1 - 14 Bad address

But if I point it to NULL:

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

int main(int argc, char *argv[], char *envp[]) {
  char *myargs[] = { "echo", "hello", NULL };
  int x = execvpe("/bin/echo", myargs, NULL);
  printf("x = %d - %d %s\n", x, errno, strerror(errno));
}

It prints "hello" as expected.  NULL is clearly not considered an illegal
address, but I would have guessed it was from the man page.

I am not clear if the right answer is "allocate zero-sized environment" in this
patch, or if the right answer is to tighten up the docs and make it clear NULL
is a valid, expected value for envp in some cases, or perhaps both?  Maybe I am
just a confused user.  I defer that call to FreeBSD team.

Apologies for opening a can of worms.

-- 
You are receiving this mail because:
You are the assignee for the bug.

Reply via email to