Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 20 Feb 1997 16:07:42 +1000 (EST)
From:      Stephen McKay <syssgm@devetir.qld.gov.au>
To:        freebsd-hackers@freebsd.org
Cc:        syssgm@devetir.qld.gov.au
Subject:   Re: hmm
Message-ID:  <199702200607.QAA06202@ogre.devetir.qld.gov.au>

next in thread | raw e-mail | index | archive | help
Joe Greco <jgreco@solaria.sol.net> wrote:

>> > 	char *p;
>> > 
>> > 	if ((p = argv[0]) == NULL)
>> > 		errx(2,"test: argc is zero");
>
>Sanity check?  I can't think of any cases where this would happen, but
>maybe some kernel god would correct me.

It is convention that argv[0] contain the program name, and that argc >= 1,
but this is not enforced by the system.

Consider a.c:

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

    int main(int argc, char **argv)
	{
	execv(argv[1], NULL);
	perror(argv[1]);
	return 1;
	}

and b.c:

    #include <stdio.h>
    #include <err.h>

    int main(int argc, char **argv)
	{
	char *p;

	if ((p = argv[0]) == NULL)
	    errx(2,"test: argc is zero");

	printf("argv[0] = '%s'\n", p);
	return 0;
	}

Then run them:

	$ ./a ./b
	: test: argc is zero
	$

Read /usr/src/lib/csu/i386/crt0.c and you will find that __progname is
carefully set to a zero length string (rather than left null) when there
is no program name.  Thus errx() prints the error message rather than
dumping core. :-)  The leading ': ' is a big bogus.  Might fix that...

Stephen.

PS For the security conscious among us, passing no arguments is one of the
dirty tricks you can play on setuid programs to see if they will do anything
interesting for you.  Along with closing all file descriptors, setting
pending alarms, reducing all the resource limits to zero, etc, etc.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199702200607.QAA06202>