Changeset 260383 in webkit


Ignore:
Timestamp:
Apr 20, 2020 12:10:00 PM (4 years ago)
Author:
Darin Adler
Message:

REGRESSION (r253987): StringImpl::adopt(Vector&&) copies only half of the characters in the Vector when copying across malloc zones
https://bugs.webkit.org/show_bug.cgi?id=210736

Reviewed by Yusuke Suzuki.

  • wtf/text/StringImpl.cpp:

(WTF::StringImpl::replace): Use copyCharacters instead of memcpy.

  • wtf/text/StringImpl.h: Use std::is_same_v instead of std::is_same.

(WTF::StringImpl::StringImpl): Use copyCharacters instead of memcpy.
Also drop C-style cast to cast away const.
(WTF::StringImpl::adopt): Don't even try to adopt across malloc zones.

Location:
trunk/Source/WTF
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r260366 r260383  
     12020-04-20  Darin Adler  <darin@apple.com>
     2
     3        REGRESSION (r253987): StringImpl::adopt(Vector&&) copies only half of the characters in the Vector when copying across malloc zones
     4        https://bugs.webkit.org/show_bug.cgi?id=210736
     5
     6        Reviewed by Yusuke Suzuki.
     7
     8        * wtf/text/StringImpl.cpp:
     9        (WTF::StringImpl::replace): Use copyCharacters instead of memcpy.
     10
     11        * wtf/text/StringImpl.h: Use std::is_same_v instead of std::is_same.
     12        (WTF::StringImpl::StringImpl): Use copyCharacters instead of memcpy.
     13        Also drop C-style cast to cast away const.
     14        (WTF::StringImpl::adopt): Don't even try to adopt across malloc zones.
     15
    1162020-04-20  Darin Adler  <darin@apple.com>
    217
  • trunk/Source/WTF/wtf/text/StringImpl.cpp

    r259773 r260383  
    13031303    auto newImpl = createUninitializedInternalNonEmpty(m_length, data);
    13041304
    1305     memcpy(data, m_data16, i * sizeof(UChar));
     1305    copyCharacters(data, m_data16, i);
    13061306    for (unsigned j = i; j != m_length; ++j) {
    13071307        UChar character = m_data16[j];
  • trunk/Source/WTF/wtf/text/StringImpl.h

    r257681 r260383  
    893893    : StringImplShape(s_refCountIncrement, length, static_cast<const LChar*>(nullptr), s_hashFlag8BitBuffer | StringNormal | BufferOwned)
    894894{
    895     if constexpr (std::is_same<Malloc, StringImplMalloc>::value)
     895    if constexpr (std::is_same_v<Malloc, StringImplMalloc>)
    896896        m_data8 = characters.leakPtr();
    897897    else {
    898         m_data8 = static_cast<const LChar*>(StringImplMalloc::malloc(length));
    899         memcpy((void*)m_data8, characters.get(), length);
     898        auto data8 = static_cast<LChar*>(StringImplMalloc::malloc(length * sizeof(LChar)));
     899        copyCharacters(data8, characters.get(), length);
     900        m_data8 = data8;
    900901    }
    901902
     
    928929    : StringImplShape(s_refCountIncrement, length, static_cast<const UChar*>(nullptr), StringNormal | BufferOwned)
    929930{
    930     if constexpr (std::is_same<Malloc, StringImplMalloc>::value)
     931    if constexpr (std::is_same_v<Malloc, StringImplMalloc>)
    931932        m_data16 = characters.leakPtr();
    932933    else {
    933         m_data16 = static_cast<const UChar*>(StringImplMalloc::malloc(length * sizeof(UChar)));
    934         memcpy((void*)m_data16, characters.get(), length * sizeof(UChar));
     934        auto data16 = static_cast<UChar*>(StringImplMalloc::malloc(length * sizeof(UChar)));
     935        copyCharacters(data16, characters.get(), length);
     936        m_data16 = data16;
    935937    }
    936938
     
    10321034inline Ref<StringImpl> StringImpl::adopt(Vector<CharacterType, inlineCapacity, OverflowHandler, minCapacity, Malloc>&& vector)
    10331035{
    1034     if (size_t size = vector.size()) {
    1035         ASSERT(vector.data());
    1036         if (size > MaxLength)
     1036    if constexpr (std::is_same_v<Malloc, StringImplMalloc>) {
     1037        auto length = vector.size();
     1038        if (!length)
     1039            return *empty();
     1040        if (length > MaxLength)
    10371041            CRASH();
    1038 
    1039         if constexpr (std::is_same<Malloc, StringImplMalloc>::value)
    1040             return adoptRef(*new StringImpl(vector.releaseBuffer(), size));
    1041         else {
    1042             // We have to copy between malloc zones.
    1043             auto vectorBuffer = vector.releaseBuffer();
    1044             auto stringImplBuffer = MallocPtr<CharacterType, StringImplMalloc>::malloc(size);
    1045             memcpy(stringImplBuffer.get(), vectorBuffer.get(), size);
    1046             return adoptRef(*new StringImpl(WTFMove(stringImplBuffer), size));
    1047         }
    1048     }
    1049     return *empty();
     1042        return adoptRef(*new StringImpl(vector.releaseBuffer(), length));
     1043    } else
     1044        return create(vector.data(), vector.size());
    10501045}
    10511046
     
    11351130inline void StringImpl::copyCharacters(DestinationCharacterType* destination, const SourceCharacterType* source, unsigned numCharacters)
    11361131{
    1137     static_assert(std::is_same<SourceCharacterType, LChar>::value || std::is_same<SourceCharacterType, UChar>::value);
    1138     static_assert(std::is_same<DestinationCharacterType, LChar>::value || std::is_same<DestinationCharacterType, UChar>::value);
    1139     if constexpr (std::is_same<SourceCharacterType, DestinationCharacterType>::value) {
     1132    static_assert(std::is_same_v<SourceCharacterType, LChar> || std::is_same_v<SourceCharacterType, UChar>);
     1133    static_assert(std::is_same_v<DestinationCharacterType, LChar> || std::is_same_v<DestinationCharacterType, UChar>);
     1134    if constexpr (std::is_same_v<SourceCharacterType, DestinationCharacterType>) {
    11401135        if (numCharacters == 1) {
    11411136            *destination = *source;
Note: See TracChangeset for help on using the changeset viewer.