Boost C++ Libraries: Ticket #7704: lexical_cast to filesystem::path fails for string::length > 22 https://svn.boost.org/trac10/ticket/7704 <p> When building with clang on Darwin using <code>-stdlib=libc++</code>, a <code>lexical_cast</code> to a <code>filesystem::path</code> will fail if the input string is longer than 22 elements. </p> <p> Consider this test program: </p> <div class="wiki-code"><div class="code"><pre><span class="cp">#include</span> <span class="cpf">&lt;boost/lexical_cast.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/filesystem/path.hpp&gt;</span><span class="cp"></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="n">boost</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">path</span> <span class="n">p</span><span class="p">;</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">s1</span> <span class="o">=</span> <span class="s">&quot;aaaaaaaaaaaaaaaaaaaaaaa&quot;</span><span class="p">;</span> <span class="n">p</span> <span class="o">=</span> <span class="n">boost</span><span class="o">::</span><span class="n">lexical_cast</span><span class="o">&lt;</span><span class="n">boost</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">path</span><span class="o">&gt;</span><span class="p">(</span><span class="n">s1</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">ex</span><span class="p">)</span> <span class="p">{</span> <span class="n">std</span><span class="o">::</span><span class="n">cerr</span> <span class="o">&lt;&lt;</span> <span class="n">ex</span><span class="p">.</span><span class="n">what</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span> <span class="k">return</span> <span class="n">EXIT_FAILURE</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> </pre></div></div><p> …compiled thusly: </p> <pre class="wiki">clang++ -stdlib=libc++ -I /Users/bradenmcdaniel/Source/boost_1_52_0 -L /Users/bradenmcdaniel/Source/boost_1_52_0/stage/lib -lboost_filesystem -lboost_system -o lexical-cast-path lexical_cast_path.cpp </pre><p> When run, it produces this output: </p> <pre class="wiki">$ ./lexical-cast-path bad lexical cast: source type value could not be interpreted as target </pre><p> If the length of <code>s1</code> is reduced by 1 (from 23 to 22), the test program will succeed. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7704 Trac 1.4.3 Braden McDaniel <braden@…> Mon, 19 Nov 2012 18:33:52 GMT component changed; owner set https://svn.boost.org/trac10/ticket/7704#comment:1 https://svn.boost.org/trac10/ticket/7704#comment:1 <ul> <li><strong>owner</strong> set to <span class="trac-author">Antony Polukhin</span> </li> <li><strong>component</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">lexical_cast</span> </li> </ul> Ticket Antony Polukhin Wed, 21 Nov 2012 13:58:27 GMT <link>https://svn.boost.org/trac10/ticket/7704#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7704#comment:2</guid> <description> <p> Could not reproduce it on clang version 3.0-6ubuntu3 with default stl library. </p> <p> It looks more like a libc++ bug, but I`m not sure. </p> <p> Could you attach a backtrace? ( Have not found a .deb pakage for libc++ and don`t have enough time to install libc++ from sources) </p> </description> <category>Ticket</category> </item> <item> <author>Braden McDaniel <braden@…></author> <pubDate>Wed, 21 Nov 2012 14:57:58 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7704#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7704#comment:3</guid> <description> <p> That's consistent with what I've seen: I was not able to reproduce the problem when building without the <code>-stdlib=libc++</code> flag. Unfortunately, the version of GNU libstdc++ available on Darwin is rather dated and thus not perceived as a path forward for new C++ development. </p> <p> Here is a backtrace from the throw point: </p> <pre class="wiki">#0 0x00007fff8457254d in __cxa_throw () #1 0x000000010000658c in boost::throw_exception&lt;boost::bad_lexical_cast&gt; (e=@0x7fff5fbff9a0) at throw_exception.hpp:66 #2 0x0000000100006249 in boost::detail::lexical_cast_do_cast&lt;boost::filesystem::path, std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt; &gt;::lexical_cast_impl (arg=@0x7fff5fbffaa0) at lexical_cast.hpp:2141 #3 0x0000000100005b7c in boost::lexical_cast&lt;boost::filesystem::path, std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt; &gt; (arg=@0x7fff5fbffaa0) at lexical_cast.hpp:2300 #4 0x0000000100005162 in main () at lexical_cast_path.cpp:10 </pre><p> If you'd like me to try to get one from some other point in the code, let me know. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Wed, 21 Nov 2012 21:17:44 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7704#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7704#comment:4</guid> <description> <p> Try this code: </p> <pre class="wiki">#include &lt;sstream&gt; #include &lt;boost/filesystem/path.hpp&gt; int main() { boost::filesystem::path p; std::istringstream s1("aaaaaaaaaaaaaaaaaaaaaaa"); s1 &gt;&gt; p; if (s1.get() != std::char_traits&lt;char&gt;::eof()) return EXIT_FAILURE; return 0; } </pre><p> If it exits with <code>EXIT_FAILURE</code>, then it is a bug in libc++ </p> </description> <category>Ticket</category> </item> <item> <author>Braden McDaniel <braden@…></author> <pubDate>Wed, 21 Nov 2012 22:44:36 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7704#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7704#comment:5</guid> <description> <p> That test program succeeds (that is, it returns <code>0</code>). </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Sat, 01 Dec 2012 18:45:32 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7704#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7704#comment:6</guid> <description> <p> Please, try this one: </p> <pre class="wiki"> #include &lt;sstream&gt; #include &lt;boost/filesystem/path.hpp&gt; #include &lt;boost/lexical_cast.hpp&gt; template&lt; class BufferType &gt; class stl_buf_unlocker: public BufferType{ public: typedef BufferType base_class; using base_class::pptr; using base_class::pbase; using base_class::setg; using base_class::setp; }; typedef stl_buf_unlocker&lt;std::basic_stringbuf&lt;char&gt; &gt; unlocked_but_t; int main() { char data[] = "aaaaaaaaaaaaaaaaaaaaaaa"; char* start = data; char* finish = start + sizeof(data); std::basic_istringstream&lt;char&gt; stream; static_cast&lt;unlocked_but_t*&gt;(stream.rdbuf())-&gt;setg(start, start, finish); stream.unsetf(std::ios::skipws); std::string output; // boost::filesystem::path output; const bool b1 = (stream &gt;&gt; output); const bool b2 = (stream.get() == std::char_traits&lt;char&gt;::eof()); BOOST_ASSERT(b1); BOOST_ASSERT(b2); return 0; } </pre><p> If it does not trigger asserts, then comment <code>std::string output;</code> and uncomment <code>boost::filesystem::path output;</code> and try it once more. </p> </description> <category>Ticket</category> </item> <item> <author>Braden McDaniel <braden@…></author> <pubDate>Mon, 03 Dec 2012 16:42:07 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7704#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7704#comment:7</guid> <description> <p> In both cases no assert is triggered and the program returns <code>0</code>. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Sun, 24 Mar 2013 15:26:47 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7704#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7704#comment:8</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/83543" title="Reimplement STL string buffer unlocker (refs #8267 and refs #7704)">[83543]</a>) Reimplement STL string buffer unlocker (refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8267" title="#8267: Bugs: lexical_cast uses stream buffers incorrectly (closed: fixed)">#8267</a> and refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/7704" title="#7704: Bugs: lexical_cast to filesystem::path fails for string::length &gt; 22 (closed: fixed)">#7704</a>) </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Sat, 30 Mar 2013 07:20:26 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7704#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7704#comment:9</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/83642" title="Fix streams and buffers usage (refs #8267 and refs #7704). Now ...">[83642]</a>) Fix streams and buffers usage (refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8267" title="#8267: Bugs: lexical_cast uses stream buffers incorrectly (closed: fixed)">#8267</a> and refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/7704" title="#7704: Bugs: lexical_cast to filesystem::path fails for string::length &gt; 22 (closed: fixed)">#7704</a>). Now conversions the use STL streams shall work faster </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Sat, 30 Mar 2013 07:41:26 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7704#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7704#comment:10</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/83644" title="Add tests for filesystem::path conversion (refs #7704)">[83644]</a>) Add tests for filesystem::path conversion (refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/7704" title="#7704: Bugs: lexical_cast to filesystem::path fails for string::length &gt; 22 (closed: fixed)">#7704</a>) </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Mon, 01 Apr 2013 20:40:46 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/7704#comment:11 https://svn.boost.org/trac10/ticket/7704#comment:11 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/83689" title="Merge from trunk: * Fix stream related issues with libc++ and clang ...">[83689]</a>) Merge from trunk: </p> <ul><li>Fix stream related issues with libc++ and clang (fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/7704" title="#7704: Bugs: lexical_cast to filesystem::path fails for string::length &gt; 22 (closed: fixed)">#7704</a>, fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8267" title="#8267: Bugs: lexical_cast uses stream buffers incorrectly (closed: fixed)">#8267</a>) </li><li>Change runtime assert to compile time when converting to pointer (fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8334" title="#8334: Bugs: Change runtime assert to compile time (closed: fixed)">#8334</a>) </li></ul> Ticket Antony Polukhin Tue, 02 Apr 2013 06:51:18 GMT milestone changed https://svn.boost.org/trac10/ticket/7704#comment:12 https://svn.boost.org/trac10/ticket/7704#comment:12 <ul> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.54.0</span> </li> </ul> Ticket