Re: [Boost-bugs] [Boost C++ Libraries] #11935: boost::program_options::error_with_option_name.what() throws std::out_of_range when an empty option is used in the command input

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #11935: boost::program_options::error_with_option_name.what() throws std::out_of_range when an empty option is used in the command input
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2016-01-29 01:47:44


#11935: boost::program_options::error_with_option_name.what() throws
std::out_of_range when an empty option is used in the command input
-------------------------------+-----------------------------
  Reporter: mark.incley@… | Owner: vladimir_prus
      Type: Bugs | Status: new
 Milestone: To Be Determined | Component: program_options
   Version: Boost 1.60.0 | Severity: Problem
Resolution: | Keywords: program_options
-------------------------------+-----------------------------

Comment (by ghickey@…):

 I just ran into this problem too, though in my case I didn't pass in an
 empty option. I was testing a parameter with a length validator:

 {{{
 add_options()
         ("name", validated<string>(&m_name, length_max(64))->required(),
 "thing name")
 }}}

 When passed a value longer than the limit the code threw a
 boost::program_options::error_with_option_name, as expected. Calling that
 exception's what() method crashed the program in the same way as the
 original reporter of this bug described, except that in this case the
 "original_token" value in the error's m_substitutions map was empty. When
 passed an empty string, strip_prefixes() throws the same out_of_range
 exception as above.

 The following patch will fix the problem:

 {{{
 --- a/boost/program_options/errors.hpp 2013-12-04 00:17:17.000000000
 -0500
 +++ b/boost/program_options/errors.hpp 2016-01-28 20:03:12.994847938
 -0500
 @@ -26,7 +26,10 @@
      inline std::string strip_prefixes(const std::string& text)
      {
          // "--foo-bar" -> "foo-bar"
 - return text.substr(text.find_first_not_of("-/"));
 + size_t pos = text.find_first_not_of("-/");
 + if (pos == std::string::npos)
 + return std::string();
 + return text.substr(pos);
      }

      /** Base class for all errors in the library. */
 }}}

 I'm using boost 1.58 on Linux.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/11935#comment:1>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:19 UTC