mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-08 09:09:43 +00:00
AK: Assert that we don't create StringViews of negative length
Due to us using size_t for the length, the actual value will always be positive. If, for example, we calculate the length as "0 - 1", we'll get SIZE_T_MAX. What we can do is check that adding the characters pointer and the length together doesn't overflow.
This commit is contained in:
parent
361a1b54d7
commit
135d29b498
Notes:
sideshowbarker
2024-07-19 07:09:43 +09:00
Author: https://github.com/bugaevc
Commit: 135d29b498
Pull-request: https://github.com/SerenityOS/serenity/pull/2024
Reviewed-by: https://github.com/awesomekling
1 changed files with 5 additions and 1 deletions
|
@ -26,6 +26,8 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Assertions.h>
|
||||||
|
#include <AK/Checked.h>
|
||||||
#include <AK/Forward.h>
|
#include <AK/Forward.h>
|
||||||
#include <AK/StdLibExtras.h>
|
#include <AK/StdLibExtras.h>
|
||||||
#include <AK/StringUtils.h>
|
#include <AK/StringUtils.h>
|
||||||
|
@ -41,11 +43,13 @@ public:
|
||||||
: m_characters(characters)
|
: m_characters(characters)
|
||||||
, m_length(length)
|
, m_length(length)
|
||||||
{
|
{
|
||||||
|
ASSERT(!Checked<uintptr_t>::addition_would_overflow((uintptr_t)characters, length));
|
||||||
}
|
}
|
||||||
StringView(const unsigned char* characters, size_t length)
|
StringView(const unsigned char* characters, size_t length)
|
||||||
: m_characters((const char*)characters)
|
: m_characters((const char*)characters)
|
||||||
, m_length(length)
|
, m_length(length)
|
||||||
{
|
{
|
||||||
|
ASSERT(!Checked<uintptr_t>::addition_would_overflow((uintptr_t)characters, length));
|
||||||
}
|
}
|
||||||
[[gnu::always_inline]] inline StringView(const char* cstring)
|
[[gnu::always_inline]] inline StringView(const char* cstring)
|
||||||
: m_characters(cstring)
|
: m_characters(cstring)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue