Boost C++ Libraries: Ticket #11935: boost::program_options::error_with_option_name.what() throws std::out_of_range when an empty option is used in the command input https://svn.boost.org/trac10/ticket/11935 <p> Hi, </p> <p> After accidentally typing "-v . -" (without the quotes) into a Windows command line app written with boost::po, it crashed. The app's catch block (code below) calls boost::po::error_with_option_name.what() and this subsequently throws std::out_of_range(). </p> <p> It appears as though the empty option "-" confuses boost::program_options::strip_prefixes(const std::string&amp; text) which is called with text value of "-", resulting in text.substr being called with a value of npos: </p> <pre class="wiki"> inline std::string strip_prefixes(const std::string&amp; text) { // "--foo-bar" -&gt; "foo-bar"[[BR]] return text.substr(text.find_first_not_of("-/")); } </pre><p> Here's my code. </p> <p> </p> <pre class="wiki">po::variables_map vm; po::options_description desc("Options"); desc.add_options() ("folder,f", po::wvalue&lt;wstring&gt;()-&gt;required(), "Specify the name of the folder containing the C++ projects to check") ("list,l", "List the project file names as they're checked") ("verbose,v", "Verbose; Show the node values when finding non-SAK SCC values") ("help,?", "Show usage information") ; po::positional_options_description pod; pod.add("folder", -1); try { po::store(po::wcommand_line_parser(argc, argv).options(desc).positional(pod).run(), vm); po::notify(vm); } catch (const boost::program_options::error &amp; e) { cerr &lt;&lt; w32::fg_red &lt;&lt; e.what() &lt;&lt; w32::restore &lt;&lt; endl; return ERROR_UNHANDLED_EXCEPTION; } </pre><p> I am using Boost 1.60 on Windows 7 (64-bit) with Visual Studio 2015 Update 1. </p> <p> Cheers<br /> Mark. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11935 Trac 1.4.3 ghickey@… Fri, 29 Jan 2016 01:47:44 GMT <link>https://svn.boost.org/trac10/ticket/11935#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11935#comment:1</guid> <description> <p> 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: </p> <pre class="wiki">add_options() ("name", validated&lt;string&gt;(&amp;m_name, length_max(64))-&gt;required(), "thing name") </pre><p> 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. </p> <p> The following patch will fix the problem: </p> <pre class="wiki">--- 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&amp; text) { // "--foo-bar" -&gt; "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. */ </pre><p> I'm using boost 1.58 on Linux. </p> </description> <category>Ticket</category> </item> </channel> </rss>