VirtualBox

Changeset 36428 in vbox for trunk/src/VBox/Main/glue


Ignore:
Timestamp:
Mar 25, 2011 12:46:45 PM (14 years ago)
Author:
vboxsync
Message:

com/string.h: AssertLogRel when encountering an invalid encoding in the copyFrom*() methods doing UTF-16/8 conversions. The ASSUMPTION is that all input strings are correctly encoded and that this is enforced by VirtualBox border code before things gets down to Utf8Str or Bstr.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/glue/string.cpp

    r35128 r36428  
    5353    size_t cwc;
    5454    int vrc = ::RTStrCalcUtf16LenEx(a_pszSrc, a_cchMax, &cwc);
    55     AssertRCReturnVoid(vrc); /* throw instead? */
     55    if (RT_FAILURE(vrc))
     56    {
     57        /* ASSUME: input is valid Utf-8. Fake out of memory error. */
     58        AssertLogRelMsgFailed(("%Rrc %.*Rhxs\n", vrc, RTStrNLen(a_pszSrc, a_cchMax), a_pszSrc));
     59        throw std::bad_alloc();
     60    }
    5661
    5762    m_bstr = ::SysAllocStringByteLen(NULL, cwc * sizeof(OLECHAR));
    58     if (m_bstr)
     63    if (RT_UNLIKELY(!m_bstr))
     64        throw std::bad_alloc();
     65
     66    PRTUTF16 pwsz = (PRTUTF16)m_bstr;
     67    vrc = ::RTStrToUtf16Ex(a_pszSrc, a_cchMax, &pwsz, cwc + 1, NULL);
     68    if (RT_FAILURE(vrc))
    5969    {
    60         PRTUTF16 pwsz = (PRTUTF16)m_bstr;
    61         vrc = ::RTStrToUtf16Ex(a_pszSrc, a_cchMax, &pwsz, cwc + 1, NULL);
    62         if (RT_FAILURE(vrc))
    63         {
    64             /* This should not happen! */
    65             AssertRC(vrc);
    66             cleanup();
    67         }
     70        /* This should not happen! */
     71        AssertRC(vrc);
     72        cleanup();
     73        throw std::bad_alloc();
    6874    }
    69     else
    70         throw std::bad_alloc();
    7175}
    7276
     
    8084    size_t cb = length() + 1;
    8185    *pstr = (char*)nsMemory::Alloc(cb);
    82     if (!*pstr)
     86    if (RT_UNLIKELY(!*pstr))
    8387        throw std::bad_alloc();
    8488    memcpy(*pstr, c_str(), cb);
     
    137141 * copying from a UTF-16 string.
    138142 *
    139  * As with the iprt::ministring::copyFrom() variants, this unconditionally
    140  * sets the members to a copy of the given other strings and makes
    141  * no assumptions about previous contents. This can therefore be used
    142  * both in copy constructors, when member variables have no defined
    143  * value, and in assignments after having called cleanup().
     143 * As with the iprt::ministring::copyFrom() variants, this unconditionally sets
     144 * the members to a copy of the given other strings and makes no assumptions
     145 * about previous contents.  This can therefore be used both in copy
     146 * constructors, when member variables have no defined value, and in
     147 * assignments after having called cleanup().
    144148 *
    145149 * This variant converts from a UTF-16 string, most probably from
    146150 * a Bstr assignment.
    147151 *
    148  * @param s
     152 * @param   a_pbstr         The source string.  The caller guarantees that this
     153 *                          is valid UTF-16.
     154 *
     155 * @sa      iprt::MiniString::copyFromN
    149156 */
    150 void Utf8Str::copyFrom(CBSTR s)
     157void Utf8Str::copyFrom(CBSTR a_pbstr)
    151158{
    152     if (s && *s)
     159    if (a_pbstr && *a_pbstr)
    153160    {
    154         int vrc = RTUtf16ToUtf8Ex((PRTUTF16)s,      // PCRTUTF16 pwszString
     161        int vrc = RTUtf16ToUtf8Ex((PCRTUTF16)a_pbstr,
    155162                                  RTSTR_MAX,        // size_t cwcString: translate entire string
    156163                                  &m_psz,           // char **ppsz: output buffer
    157164                                  0,                // size_t cch: if 0, func allocates buffer in *ppsz
    158165                                  &m_cch);          // size_t *pcch: receives the size of the output string, excluding the terminator.
    159         if (RT_FAILURE(vrc))
     166        if (RT_SUCCESS(vrc))
     167            m_cbAllocated = m_cch + 1;
     168        else
    160169        {
    161             if (    vrc == VERR_NO_STR_MEMORY
    162                  || vrc == VERR_NO_MEMORY
    163                )
    164                 throw std::bad_alloc();
     170            if (   vrc != VERR_NO_STR_MEMORY
     171                && vrc != VERR_NO_MEMORY)
     172            {
     173                /* ASSUME: input is valid Utf-16. Fake out of memory error. */
     174                AssertLogRelMsgFailed(("%Rrc %.*Rhxs\n", vrc, RTUtf16Len(a_pbstr) * sizeof(RTUTF16), a_pbstr));
     175            }
    165176
    166             // @todo what do we do with bad input strings? throw also? for now just keep an empty string
    167177            m_cch = 0;
    168178            m_cbAllocated = 0;
    169179            m_psz = NULL;
     180
     181            throw std::bad_alloc();
    170182        }
    171         else
    172             m_cbAllocated = m_cch + 1;
    173183    }
    174184    else
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette