From owner-freebsd-hackers Wed Feb 19 22:08:20 1997 Return-Path: Received: (from root@localhost) by freefall.freebsd.org (8.8.5/8.8.5) id WAA20422 for hackers-outgoing; Wed, 19 Feb 1997 22:08:20 -0800 (PST) Received: from bunyip.cc.uq.edu.au (daemon@bunyip.cc.uq.edu.au [130.102.2.1]) by freefall.freebsd.org (8.8.5/8.8.5) with ESMTP id WAA20372 for ; Wed, 19 Feb 1997 22:07:24 -0800 (PST) Received: (from daemon@localhost) by bunyip.cc.uq.edu.au (8.8.5/8.8.5) id QAA15096 for freebsd-hackers@freebsd.org; Thu, 20 Feb 1997 16:04:45 +1000 Received: by ogre.devetir.qld.gov.au (8.7.5/DEVETIR-E0.3a) id QAA06202; Thu, 20 Feb 1997 16:07:42 +1000 (EST) Date: Thu, 20 Feb 1997 16:07:42 +1000 (EST) From: Stephen McKay Message-Id: <199702200607.QAA06202@ogre.devetir.qld.gov.au> To: freebsd-hackers@freebsd.org cc: syssgm@devetir.qld.gov.au Subject: Re: hmm X-Newsreader: NN version 6.5.0 #1 (NOV) Sender: owner-hackers@freebsd.org X-Loop: FreeBSD.org Precedence: bulk Joe Greco 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 #include int main(int argc, char **argv) { execv(argv[1], NULL); perror(argv[1]); return 1; } and b.c: #include #include 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.