Modify

Opened 13 years ago

Closed 9 years ago

#287 closed Bugs (fixed)

wrong usage of ios_base::narrow

Reported by: vandrejev Owned by: az_sw_dude
Milestone: Boost 1.40.0 Component: date_time
Version: None Severity: Problem
Keywords: Cc:

Description (last modified by Marshall Clow)

/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 << 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 << ss.narrow(*beg++, 'X'); // 'X' will cause 
exception to be thrown
        }
        return parse_date<date_type>(ss.str());
      }
--- 207,215 ----
                                 wchar_t) 
      {
        std::stringstream ss("");
+       std::locale loc;
        while(beg != end) {
!         ss << std::use_facet<std::ctype<wchar_t> >
(loc).narrow(*beg++, 'X');
        }
        return parse_date<date_type>(ss.str());
      }

Attachments (0)

Change History (7)

comment:1 Changed 9 years ago by Marshall Clow

Description: modified (diff)
Milestone: Boost 1.40.0
Severity: Problem

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. {{

while(beg != end) {

#if !defined(BOOST_DATE_TIME_NO_LOCALE)

ss << std::use_facet<std::ctype<wchar_t> >(std::locale()).narrow(*beg++, 'X'); 'X' will cause exception to be thrown

#else

ss << ss.narrow(*beg++, 'X');

#endif

}

}}

comment:2 Changed 9 years ago by Steven Watanabe

But the #else is still just as wrong as ever.

comment:3 Changed 9 years ago by Andrey Semashev

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.

comment:4 Changed 9 years ago by Steven Watanabe

At least the call to narrow should be removed to avoid giving the false impression that it's safe.

comment:5 in reply to:  4 Changed 9 years ago by Andrey Semashev

Replying to steven_watanabe:

At least the call to narrow should be removed to avoid giving the false impression that it's safe.

I think this would be more appropriate:

char c = 'X'; // 'X' will cause exception to be thrown
const wchar_t wc = *beg++;
if (wc >= 0 && wc <= 127)
  c = static_cast< char >(wc);
ss << c;

comment:6 Changed 9 years ago by Andrey Semashev

Went in revision 53529.

comment:7 Changed 9 years ago by Andrey Semashev

Resolution: Nonefixed
Status: assignedclosed

Merged into release branch at revision 53618. Will be released in 1.40.

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain az_sw_dude.
The resolution will be deleted.

Add Comment


E-mail address and name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.