mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-05-13 06:32:54 +00:00
AK: Add input bounds checking to String::substring()
This checks for overflow in String::substring(). It also rearranges some declarations in the header.
This commit is contained in:
parent
268d81a56c
commit
17eddf3ac4
Notes:
sideshowbarker
2024-07-18 11:06:05 +09:00
Author: https://github.com/MaxWipfli
Commit: 17eddf3ac4
Pull-request: https://github.com/SerenityOS/serenity/pull/8368
Reviewed-by: https://github.com/alimpfard
Reviewed-by: https://github.com/trflynn89
2 changed files with 13 additions and 14 deletions
|
@ -91,6 +91,16 @@ String String::isolated_copy() const
|
|||
return String(move(*impl));
|
||||
}
|
||||
|
||||
String String::substring(size_t start, size_t length) const
|
||||
{
|
||||
if (!length)
|
||||
return String::empty();
|
||||
VERIFY(m_impl);
|
||||
VERIFY(!Checked<size_t>::addition_would_overflow(start, length));
|
||||
VERIFY(start + length <= m_impl->length());
|
||||
return { characters() + start, length };
|
||||
}
|
||||
|
||||
String String::substring(size_t start) const
|
||||
{
|
||||
VERIFY(m_impl);
|
||||
|
@ -98,21 +108,11 @@ String String::substring(size_t start) const
|
|||
return { characters() + start, length() - start };
|
||||
}
|
||||
|
||||
String String::substring(size_t start, size_t length) const
|
||||
{
|
||||
if (!length)
|
||||
return "";
|
||||
VERIFY(m_impl);
|
||||
VERIFY(start + length <= m_impl->length());
|
||||
// FIXME: This needs some input bounds checking.
|
||||
return { characters() + start, length };
|
||||
}
|
||||
|
||||
StringView String::substring_view(size_t start, size_t length) const
|
||||
{
|
||||
VERIFY(m_impl);
|
||||
VERIFY(!Checked<size_t>::addition_would_overflow(start, length));
|
||||
VERIFY(start + length <= m_impl->length());
|
||||
// FIXME: This needs some input bounds checking.
|
||||
return { characters() + start, length };
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue