Boost logo

Boost :

From: C. K. Jester-Young (cky944_at_[hidden])
Date: 2008-02-10 00:01:29


In the program_options library, command-line parsing functions that take
(argc, argv) arguments do not check for a zero argc. Thus, when argc is
zero, the iterators passed to the vector constructor have first > last,
which results in undefined behaviour. Patches are attached which address
this (minimally, simply by treating argc as 1 when it's 0, since argv[0]
is never used).

According to the execve reference in Open Group Base Specifications
Issue 6:

    Early proposals required that the value of argc passed to main()
    be "one or greater". This was driven by the same requirement in
    drafts of the ISO C standard. In fact, historical implementations
    have passed a value of zero when no arguments are supplied to the
    caller of the exec functions. This requirement was removed from
    the ISO C standard and subsequently removed from this volume of
    IEEE Std 1003.1-2001 as well.

In practice, some operating systems, notably Linux, still do this, as
aptly demonstrated with the following program:

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

int
main(int argc, char **argv)
{
    char *empty = (char *) 0;
    if (argc != 2) {
        fprintf(stderr, "usage: %s program\n", argc ? argv[0] : "(null)");
        return 1;
    }
    execve(argv[1], &empty, &empty);
    fprintf(stderr, "Cannot run %s: %s\n", argv[1], strerror(errno));
    return 1;
}

Then you can test with your favourite :-) programs to see how they
behave (I've called the program zeroargc in the examples below):

    $ ./zeroargc
    usage: ./zeroargc program
    $ ./zeroargc ./zeroargc
    usage: (null) program

Cheers,
Chris.





Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk