Skip to content

Commit

Permalink
Add bounds check to StringImpl::operator[]
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=263899

Reviewed by Ryosuke Niwa.

Add bounds check to StringImpl::operator[] for extra safety. This
tested as performance neutral on Speedometer and Jetstream.

* Source/WTF/wtf/text/StringImpl.h:
(WTF::StringImpl::at const):
* Source/WTF/wtf/text/WTFString.h:
(WTF::String::characterAt const):

Canonical link: https://commits.webkit.org/269967@main
  • Loading branch information
cdumez committed Oct 30, 2023
1 parent 135c6d5 commit e93f8aa
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Source/WTF/wtf/text/StringImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,7 @@ inline void StringImpl::deref()

inline UChar StringImpl::at(unsigned i) const
{
ASSERT_WITH_SECURITY_IMPLICATION(i < m_length);
RELEASE_ASSERT(i < m_length);
return is8Bit() ? m_data8[i] : m_data16[i];
}

Expand Down
2 changes: 1 addition & 1 deletion Source/WTF/wtf/text/WTFString.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ inline UChar String::characterAt(unsigned index) const
{
if (!m_impl || index >= m_impl->length())
return 0;
return (*m_impl)[index];
return m_impl->is8Bit() ? m_impl->characters8()[index] : m_impl->characters16()[index];
}

inline String WARN_UNUSED_RETURN makeStringByReplacingAll(const String& string, UChar target, UChar replacement)
Expand Down

0 comments on commit e93f8aa

Please sign in to comment.