Boost C++ Libraries: Ticket #287: wrong usage of ios_base::narrow https://svn.boost.org/trac10/ticket/287 <pre class="wiki">/cvsroot/boost/boost/boost/date_time/date_parsing.hpp revision 1.18, lines 207, 214 std::stringstream has char_type typedef evidently equal to char, not wchar_t, so in std::stringstream::narrow function the first argument has type char and in the call ss &lt;&lt; ss.narrow(*beg++, 'X'); *beg will be converted from wchar_t to char before call to narrow will be made. I suggest the following patch: diff -c -r1.18 date_parsing.hpp *************** *** 207,214 **** wchar_t) { std::stringstream ss(""); while(beg != end) { ! ss &lt;&lt; ss.narrow(*beg++, 'X'); // 'X' will cause exception to be thrown } return parse_date&lt;date_type&gt;(ss.str()); } --- 207,215 ---- wchar_t) { std::stringstream ss(""); + std::locale loc; while(beg != end) { ! ss &lt;&lt; std::use_facet&lt;std::ctype&lt;wchar_t&gt; &gt; (loc).narrow(*beg++, 'X'); } return parse_date&lt;date_type&gt;(ss.str()); } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/287 Trac 1.4.3 Marshall Clow Fri, 29 May 2009 18:27:51 GMT description changed; severity, milestone set https://svn.boost.org/trac10/ticket/287#comment:1 https://svn.boost.org/trac10/ticket/287#comment:1 <ul> <li><strong>severity</strong> → <span class="trac-field-new">Problem</span> </li> <li><strong>description</strong> modified (<a href="/trac10/ticket/287?action=diff&amp;version=1">diff</a>) </li> <li><strong>milestone</strong> → <span class="trac-field-new">Boost 1.40.0</span> </li> </ul> <p> Marshall sez: looking at the code in trunk, it appears that this patch has been (more or less) applied. I'm not sure about the constructor call to std:locale() for each character, though. {{ </p> <blockquote> <p> while(beg != end) { </p> </blockquote> <p> #if !defined(BOOST_DATE_TIME_NO_LOCALE) </p> <blockquote> <p> ss &lt;&lt; std::use_facet&lt;std::ctype&lt;wchar_t&gt; &gt;(std::locale()).narrow(*beg++, 'X'); <em> 'X' will cause exception to be thrown </em></p> </blockquote> <p> #else </p> <blockquote> <p> ss &lt;&lt; ss.narrow(*beg++, 'X'); </p> </blockquote> <p> #endif </p> <blockquote> <p> } </p> </blockquote> <p> }} </p> Ticket Steven Watanabe Sun, 31 May 2009 00:54:44 GMT <link>https://svn.boost.org/trac10/ticket/287#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/287#comment:2</guid> <description> <p> But the #else is still just as wrong as ever. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Andrey Semashev</dc:creator> <pubDate>Sun, 31 May 2009 11:32:22 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/287#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/287#comment:3</guid> <description> <p> I'm not sure we need or can fix the #else branch since there is no real wide character support without locales. As for extracting locale construction and facet extraction out of the loop - that's a good optimization. Anyway, the ticket can be closed, IMHO. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Sun, 31 May 2009 13:29:32 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/287#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/287#comment:4</guid> <description> <p> At least the call to narrow should be removed to avoid giving the false impression that it's safe. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Andrey Semashev</dc:creator> <pubDate>Mon, 01 Jun 2009 09:14:35 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/287#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/287#comment:5</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/287#comment:4" title="Comment 4">steven_watanabe</a>: </p> <blockquote class="citation"> <p> At least the call to narrow should be removed to avoid giving the false impression that it's safe. </p> </blockquote> <p> I think this would be more appropriate: </p> <pre class="wiki">char c = 'X'; // 'X' will cause exception to be thrown const wchar_t wc = *beg++; if (wc &gt;= 0 &amp;&amp; wc &lt;= 127) c = static_cast&lt; char &gt;(wc); ss &lt;&lt; c; </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Andrey Semashev</dc:creator> <pubDate>Mon, 01 Jun 2009 09:52:40 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/287#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/287#comment:6</guid> <description> <p> Went in revision 53529. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Andrey Semashev</dc:creator> <pubDate>Thu, 04 Jun 2009 08:39:58 GMT</pubDate> <title>status, resolution changed https://svn.boost.org/trac10/ticket/287#comment:7 https://svn.boost.org/trac10/ticket/287#comment:7 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Merged into release branch at revision 53618. Will be released in 1.40. </p> Ticket