Uploaded image for project: 'C++ Standard Library'
  1. C++ Standard Library
  2. STDCXX-854

incorrect using the caching array in ctype<wchar_t>::narrow(wchar_t, char)

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 4.1.2, 4.1.3, 4.1.4, 4.2.0
    • 4.2.1
    • 22. Localization
    • None
    • All

    • Patch Available
    • Incorrect Behavior

    Description

      The 22.locale.ctype and 22.locale.ctype.narrow tests are failing with number of assertions. The reason is casting the at least 16-bit wchar_t value to 8-bit unsigned char and using this value as index in cache table in ctype<wchar_t>::narrow(wchar_t, char). As a result the index always fits the table of size 256 elements.

      The proposed patch:

      Index: include/loc/_ctype.h
      ===================================================================
      --- include/loc/_ctype.h	(revision 646660)
      +++ include/loc/_ctype.h	(working copy)
      @@ -541,20 +541,21 @@
       inline char
       ctype<wchar_t>::narrow (char_type __c, char __dfault) const
       {
      -    const _RWSTD_SIZE_T __inx = _RWSTD_STATIC_CAST (unsigned char, __c);
      -
           // optimize away all but the first call to the virtual do_widen()
      -    if (   __inx < sizeof _C_narrow_tab / sizeof *_C_narrow_tab
      -        && _C_narrow_tab [__inx])
      -        return _C_narrow_tab [__inx];
      +    if (   0 <= __c
      +        && __c < sizeof _C_narrow_tab / sizeof *_C_narrow_tab
      +        && _C_narrow_tab [__c])
      +        return _C_narrow_tab [__c];
       
           // template argument provided to work around an HP aCC bug (PR #27087)
           ctype<wchar_t>* const __self = _RWSTD_CONST_CAST (ctype<wchar_t>*, this);
       
           const char __ch = do_narrow (__c, __dfault);
       
      -    if (__inx < sizeof _C_narrow_tab / sizeof *_C_narrow_tab && __ch != __dfault)
      -        __self->_C_narrow_tab [__inx] = __ch;
      +    if (   0 <= __c
      +        && __c < sizeof _C_narrow_tab / sizeof *_C_narrow_tab
      +        && __ch != __dfault)
      +        __self->_C_narrow_tab [__c] = __ch;
       
           return __ch;
       }
      

      Attachments

        Activity

          People

            farid Farid Zaripov
            farid Farid Zaripov
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - 1h
                1h
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 1h
                1h