Opened 7 years ago

#11891 new Bugs

boost::program_options::validation_error throws std::out_of_range when original_token argument is empty

Reported by: ce.andrade@… Owned by: Vladimir Prus
Milestone: To Be Determined Component: program_options
Version: Boost 1.60.0 Severity: Problem
Keywords: Cc: vladimir.prus@…

Description

The code below presents some bugs.

#include <iostream>
#include <stdexcept>
#include <boost/program_options.hpp>

using namespace std;
namespace po = boost::program_options;

int main() {
    try {
        // Works OK!
        //throw po::validation_error(po::validation_error::invalid_option_value);

        // Doesn't work and throw std::out_of_range exception
        //throw po::validation_error(po::validation_error::invalid_option_value,
                                   //"--test");

        // Doesn't work as above: std::out_of_range exception
        //throw po::validation_error(po::validation_error::invalid_option_value,
                                   //"--test", "");

        // Doesn't work properly. Although it doesn't throw out_of_range
        // exception, the value "abc" is no shown. The output is:
        // "Error: the argument for option 'test' is invalid"
        throw po::validation_error(po::validation_error::invalid_option_value,
                                   "--test", "abc");
    }
    catch(std::exception& e) {
        cerr << "Error: " << e.what() << "\n";
        return -1;
    }
    catch(...) {
        cerr << "Unknown error!" << "\n";
        return -1;
    }

    return 0;
}

The first instantiation of validation_error works fine. The second and third version throws an std::out_of_range exception:

terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr: __pos (which is 18446744073709551615) > this->size() (which is 0)
Abort trap: 6

The third version throws the correct exception, but doesn't show argument original_token (3rd argument in the validation_error constructor). The output is:

Error: the argument for option 'test' is invalid

where should be

Error: the argument ('abc') for option 'test' is invalid

Since the last bug is different from the first and second, may I open another ticket?

This code has been tested using GCC 5.2.0 (GNU/Linux) and GCC 5.3.0 (Mac OS X), C++11, Boost 1.59, using the following command-line:

g++ -std=c++11 -ggdb boost_program_options.cpp -o test -L/opt/local/lib -lboost_program_options

I appreciate your help!

Attachments (1)

boost_program_options.cpp (1.3 KB ) - added by ce.andrade@… 7 years ago.

Download all attachments as: .zip

Change History (1)

by ce.andrade@…, 7 years ago

Attachment: boost_program_options.cpp added
Note: See TracTickets for help on using tickets.