File tree Expand file tree Collapse file tree
include/boost/program_options Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -26,7 +26,12 @@ namespace boost { namespace program_options {
2626 inline std::string strip_prefixes (const std::string& text)
2727 {
2828 // "--foo-bar" -> "foo-bar"
29- return text.substr (text.find_first_not_of (" -/" ));
29+ std::string::size_type i = text.find_first_not_of (" -/" );
30+ if (i == std::string::npos) {
31+ return text;
32+ } else {
33+ return text.substr (i);
34+ }
3035 }
3136
3237 /* * Base class for all errors in the library. */
Original file line number Diff line number Diff line change @@ -620,7 +620,27 @@ void test_invalid_command_line_style_exception_msg()
620620 }
621621}
622622
623+ void test_empty_value_inner (options_description &opts, variables_map& vm) {
624+ positional_options_description popts;
625+ opts.add_options ()(" foo" , value<uint32_t >()->value_name (" <time>" )->required ());
626+ popts.add (" foo" , 1 );
627+ vector<string> tokens{" " };
628+ parsed_options parsed = command_line_parser (tokens)
629+ .style (command_line_style::default_style & ~command_line_style::allow_guessing)
630+ .options (opts)
631+ .positional (popts)
632+ .run ();
633+ store (parsed, vm);
634+ }
623635
636+ void test_empty_value () {
637+ // Test that passing empty token for an option that requires integer does not result
638+ // in out-of-range error in error reporting code.
639+ test_exception<invalid_option_value>(
640+ " test_empty_value" ,
641+ " the argument for option '--foo' is invalid" ,
642+ test_empty_value_inner);
643+ }
624644
625645int main (int /* ac*/ , char ** /* av*/ )
626646{
@@ -633,6 +653,7 @@ int main(int /*ac*/, char** /*av*/)
633653 test_multiple_values_not_allowed_exception_msg ();
634654 test_required_option_exception_msg ();
635655 test_at_least_one_value_required_exception_msg ();
656+ test_empty_value ();
636657
637658 string test_name;
638659 string expected_message;
You can’t perform that action at this time.
0 commit comments