Skip to content

boost::program_options::parse_command_line crashes with argc of ISO Cpp valid value of 0 #127

@Superlokkus

Description

@Superlokkus

boost::program_options::parse_command_line(argc, argv, parameters) invokes undefined behavior and crashes with a value of argc of 0 (and argv nullptr).

These values for argc and argv are perfectly valid according to

6.9.3.1 main function[basic.start.main]

The value of argc shall be non-negative. The value of argv[argc] shall be 0.

(Of course this assumes 0 is non-negative, but the cpp standard seems to assume this too as it states „If argc is nonzero these arguments shall…“).

To be fair, almost none implementation might uses 0, when writing an integration test, as I did, that surrogates argc and argv, as any regular parameter for this boost function, I would expect no UB, when not violating any API contract.

Looking at the code this is no suprise as

basic_command_line_parser(int argc, const charT* const argv[])
    : detail::cmdline(
        to_internal(std::vector<std::basic_string<charT> >(argv+1, argv+argc))),

uses the std::vector(begin,end) constructor, however with argc of 0, end will be < than begin, hence undefined behaviour and luckily an exception.

Older boost code did righteously check for argc == 0

   basic_command_line_parser(int argc, const charT* const argv[])
    : detail::cmdline(
        to_internal(std::vector<std::basic_string<charT> >(
                      // When argc == 0, we must produce a valid empty range,
                      // even with a nullptr argv.  We can ignore the
                      // obviously illegal argc < 0.
                      argc ? argv+1 : argv, argv+argc))),

The following code


#include <boost/program_options.hpp>


int main() {
    boost::program_options::options_description parameters("Parameters");
    int argc = 0; //Allowed value see https://eel.is/c++draft/basic.start.main#2.2
    char* argv[] {nullptr};
    boost::program_options::parse_command_line(argc, argv, parameters);
}

crashes, luckily with an exception from std::vector

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions