VirtualBox

Changeset 35567 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
Jan 14, 2011 2:16:45 PM (14 years ago)
Author:
vboxsync
Message:

IPRT: fix rare crash in MiniString::substr(); rename substr() to substrCP() and add a substr that operates on bytes, not codepoints; more to come

Location:
trunk/src/VBox/Runtime
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/string/ministring.cpp

    r35128 r35567  
    224224}
    225225
    226 MiniString MiniString::substr(size_t pos /*= 0*/, size_t n /*= npos*/)
     226MiniString MiniString::substrCP(size_t pos /*= 0*/, size_t n /*= npos*/)
    227227    const
    228228{
     
    256256
    257257                size_t cbCopy = psz - pFirst;
    258                 ret.reserve(cbCopy + 1); // may throw bad_alloc
    259 #ifndef RT_EXCEPTIONS_ENABLED
    260                 AssertRelease(capacity() >= cbCopy + 1);
    261 #endif
    262                 memcpy(ret.m_psz, pFirst, cbCopy);
    263                 ret.m_cch = cbCopy;
    264                 ret.m_psz[cbCopy] = '\0';
     258                if (cbCopy)
     259                {
     260                    ret.reserve(cbCopy + 1); // may throw bad_alloc
     261#ifndef RT_EXCEPTIONS_ENABLED
     262                    AssertRelease(capacity() >= cbCopy + 1);
     263#endif
     264                    memcpy(ret.m_psz, pFirst, cbCopy);
     265                    ret.m_cch = cbCopy;
     266                    ret.m_psz[cbCopy] = '\0';
     267                }
    265268            }
    266269        }
  • trunk/src/VBox/Runtime/testcase/tstIprtMiniString.cpp

    r33862 r35567  
    211211    CHECK_EQUAL(SubStr15, "cdef");
    212212
     213    /* substr() and substrCP() functions */
     214    iprt::MiniString strTest("");
     215    CHECK_EQUAL(strTest.substr(0), "");
     216    CHECK_EQUAL(strTest.substrCP(0), "");
     217    CHECK_EQUAL(strTest.substr(1), "");
     218    CHECK_EQUAL(strTest.substrCP(1), "");
     219
     220    /* now let's have some non-ASCII to chew on */
     221    strTest = "abcdefßäbcdef";
     222            // 13 codepoints, but 15 bytes (excluding null terminator);
     223            // "ß" and "ä" consume two bytes each
     224    CHECK_EQUAL(strTest.substr(0),   strTest.c_str());
     225    CHECK_EQUAL(strTest.substrCP(0), strTest.c_str());
     226
     227    CHECK_EQUAL(strTest.substr(2),   "cdefßäbcdef");
     228    CHECK_EQUAL(strTest.substrCP(2), "cdefßäbcdef");
     229
     230    CHECK_EQUAL(strTest.substr(2, 2),   "cd");
     231    CHECK_EQUAL(strTest.substrCP(2, 2), "cd");
     232
     233    CHECK_EQUAL(strTest.substr(6),   "ßäbcdef");
     234    CHECK_EQUAL(strTest.substrCP(6), "ßäbcdef");
     235
     236    CHECK_EQUAL(strTest.substr(6, 2),   "ß");           // UTF-8 "ß" consumes two bytes
     237    CHECK_EQUAL(strTest.substrCP(6, 1), "ß");
     238
     239    CHECK_EQUAL(strTest.substr(8),   "äbcdef");         // UTF-8 "ß" consumes two bytes
     240    CHECK_EQUAL(strTest.substrCP(7), "äbcdef");
     241
     242    CHECK_EQUAL(strTest.substr(8, 3),   "äb");          // UTF-8 "ä" consumes two bytes
     243    CHECK_EQUAL(strTest.substrCP(7, 2), "äb");
     244
     245    CHECK_EQUAL(strTest.substr(14, 1),   "f");
     246    CHECK_EQUAL(strTest.substrCP(12, 1), "f");
     247
     248    CHECK_EQUAL(strTest.substr(15, 1),   "");
     249    CHECK_EQUAL(strTest.substrCP(13, 1), "");
     250
     251    CHECK_EQUAL(strTest.substr(16, 1),   "");
     252    CHECK_EQUAL(strTest.substrCP(15, 1), "");
    213253
    214254    /* special constructor and assignment arguments */
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