Boost C++ Libraries: Ticket #11891: boost::program_options::validation_error throws std::out_of_range when original_token argument is empty https://svn.boost.org/trac10/ticket/11891 <p> The code below presents some bugs. </p> <div class="wiki-code"><div class="code"><pre><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;stdexcept&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/program_options.hpp&gt;</span><span class="cp"></span> <span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">namespace</span> <span class="n">po</span> <span class="o">=</span> <span class="n">boost</span><span class="o">::</span><span class="n">program_options</span><span class="p">;</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="k">try</span> <span class="p">{</span> <span class="c1">// Works OK!</span> <span class="c1">//throw po::validation_error(po::validation_error::invalid_option_value);</span> <span class="c1">// Doesn&#39;t work and throw std::out_of_range exception</span> <span class="c1">//throw po::validation_error(po::validation_error::invalid_option_value,</span> <span class="c1">//&quot;--test&quot;);</span> <span class="c1">// Doesn&#39;t work as above: std::out_of_range exception</span> <span class="c1">//throw po::validation_error(po::validation_error::invalid_option_value,</span> <span class="c1">//&quot;--test&quot;, &quot;&quot;);</span> <span class="c1">// Doesn&#39;t work properly. Although it doesn&#39;t throw out_of_range</span> <span class="c1">// exception, the value &quot;abc&quot; is no shown. The output is:</span> <span class="c1">// &quot;Error: the argument for option &#39;test&#39; is invalid&quot;</span> <span class="k">throw</span> <span class="n">po</span><span class="o">::</span><span class="n">validation_error</span><span class="p">(</span><span class="n">po</span><span class="o">::</span><span class="n">validation_error</span><span class="o">::</span><span class="n">invalid_option_value</span><span class="p">,</span> <span class="s">&quot;--test&quot;</span><span class="p">,</span> <span class="s">&quot;abc&quot;</span><span class="p">);</span> <span class="p">}</span> <span class="k">catch</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">exception</span><span class="o">&amp;</span> <span class="n">e</span><span class="p">)</span> <span class="p">{</span> <span class="n">cerr</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Error: &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">e</span><span class="p">.</span><span class="n">what</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="k">catch</span><span class="p">(...)</span> <span class="p">{</span> <span class="n">cerr</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Unknown error!&quot;</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span> <span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="p">}</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div></div><p> The first instantiation of <code>validation_error</code> works fine. The second and third version throws an <code>std::out_of_range</code> exception: </p> <pre class="wiki">terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr: __pos (which is 18446744073709551615) &gt; this-&gt;size() (which is 0) Abort trap: 6 </pre><p> The third version throws the correct exception, but doesn't show argument <code>original_token</code> (3rd argument in the <code>validation_error</code> constructor). The output is: </p> <pre class="wiki">Error: the argument for option 'test' is invalid </pre><p> where should be </p> <pre class="wiki">Error: the argument ('abc') for option 'test' is invalid </pre><p> Since the last bug is different from the first and second, may I open another ticket? </p> <p> 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: </p> <pre class="wiki">g++ -std=c++11 -ggdb boost_program_options.cpp -o test -L/opt/local/lib -lboost_program_options </pre><p> I appreciate your help! </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11891 Trac 1.4.3 ce.andrade@… Wed, 06 Jan 2016 22:54:24 GMT attachment set https://svn.boost.org/trac10/ticket/11891 https://svn.boost.org/trac10/ticket/11891 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost_program_options.cpp</span> </li> </ul> Ticket