1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:17:44 +00:00

Everywhere: Explicitly specify the size in StringView constructors

This commit moves the length calculations out to be directly on the
StringView users. This is an important step towards the goal of removing
StringView(char const*), as it moves the responsibility of calculating
the size of the string to the user of the StringView (which will prevent
naive uses causing OOB access).
This commit is contained in:
sin-ack 2022-07-11 19:53:29 +00:00 committed by Andreas Kling
parent e3da0adfe6
commit c70f45ff44
75 changed files with 264 additions and 203 deletions

View file

@ -18,7 +18,7 @@ inline String demangle(StringView name)
{
int status = 0;
auto* demangled_name = abi::__cxa_demangle(name.to_string().characters(), nullptr, nullptr, &status);
auto string = String(status == 0 ? demangled_name : name);
auto string = String(status == 0 ? StringView { demangled_name, strlen(demangled_name) } : name);
if (status == 0)
free(demangled_name);
return string;

View file

@ -92,7 +92,7 @@ public:
constexpr bool consume_specific(char const* next)
{
return consume_specific(StringView { next });
return consume_specific(StringView { next, __builtin_strlen(next) });
}
constexpr char consume_escaped_character(char escape_char = '\\', StringView escape_map = "n\nr\rt\tb\bf\f")

View file

@ -89,11 +89,11 @@ public:
TRY(begin_item(key));
if constexpr (IsLegacyBuilder<Builder>) {
TRY(m_builder.try_append('"'));
TRY(m_builder.try_append_escaped_for_json(value));
TRY(m_builder.try_append_escaped_for_json({ value, strlen(value) }));
TRY(m_builder.try_append('"'));
} else {
TRY(m_builder.append('"'));
TRY(m_builder.append_escaped_for_json(value));
TRY(m_builder.append_escaped_for_json({ value, strlen(value) }));
TRY(m_builder.append('"'));
}
return {};

View file

@ -15,8 +15,8 @@ namespace AK {
class SourceLocation {
public:
[[nodiscard]] constexpr StringView function_name() const { return StringView(m_function); }
[[nodiscard]] constexpr StringView filename() const { return StringView(m_file); }
[[nodiscard]] constexpr StringView function_name() const { return { m_function, __builtin_strlen(m_function) }; }
[[nodiscard]] constexpr StringView filename() const { return { m_file, __builtin_strlen(m_file) }; }
[[nodiscard]] constexpr u32 line_number() const { return m_line; }
[[nodiscard]] static constexpr SourceLocation current(char const* const file = __builtin_FILE(), u32 line = __builtin_LINE(), char const* const function = __builtin_FUNCTION())