| 1 | //
|
|---|
| 2 | // Test program to help demonstrate lexical_cast overflow bug
|
|---|
| 3 | //
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 | #include <iostream>
|
|---|
| 7 | #include <string>
|
|---|
| 8 |
|
|---|
| 9 | #include <boost/program_options.hpp>
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 | using namespace boost;
|
|---|
| 13 | using namespace std;
|
|---|
| 14 |
|
|---|
| 15 | int main(int argc, char** argv)
|
|---|
| 16 | {
|
|---|
| 17 | int rc = 0; // assume success
|
|---|
| 18 |
|
|---|
| 19 | // ------------Set up program options ----------------------
|
|---|
| 20 |
|
|---|
| 21 | unsigned int number; // a number parameter
|
|---|
| 22 |
|
|---|
| 23 | program_options::options_description desc("Allowed options");
|
|---|
| 24 | desc.add_options()
|
|---|
| 25 | ("help", "produce this help message")
|
|---|
| 26 | ("number", program_options::value<unsigned int>(&number)->default_value(0),
|
|---|
| 27 | "a number parameter")
|
|---|
| 28 | ;
|
|---|
| 29 |
|
|---|
| 30 | try // boost throws an exception on a bad parameter
|
|---|
| 31 | {
|
|---|
| 32 | program_options::variables_map vm;
|
|---|
| 33 | program_options::store(program_options::parse_command_line(argc, argv, desc), vm);
|
|---|
| 34 | program_options::notify(vm);
|
|---|
| 35 |
|
|---|
| 36 | if ((argc <= 1) || (vm.count("help")))
|
|---|
| 37 | {
|
|---|
| 38 | cout << "Usage: options_description [options]\n";
|
|---|
| 39 | cout << desc;
|
|---|
| 40 | }
|
|---|
| 41 | else
|
|---|
| 42 | {
|
|---|
| 43 | cout << "The value this code thinks was supplied for --number was: "
|
|---|
| 44 | << number << endl;
|
|---|
| 45 | }
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | catch (const boost::program_options::error& error)
|
|---|
| 49 | {
|
|---|
| 50 | cout << "ERROR: " << error.what() << endl;
|
|---|
| 51 | cout << desc;
|
|---|
| 52 | rc = 2;
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | catch(const std::exception& error)
|
|---|
| 56 | {
|
|---|
| 57 | cout << "ERROR: " << error.what() << endl;
|
|---|
| 58 | rc = 2;
|
|---|
| 59 | }
|
|---|
| 60 | catch (...)
|
|---|
| 61 | {
|
|---|
| 62 | cout << "Unknown exception." << endl;
|
|---|
| 63 | rc = 2;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | return rc;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|