mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 22:37:35 +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:
parent
e3da0adfe6
commit
c70f45ff44
75 changed files with 264 additions and 203 deletions
|
@ -64,7 +64,7 @@ int inet_pton(int af, char const* src, void* dst)
|
|||
*(uint32_t*)dst = u.l;
|
||||
return 1;
|
||||
} else if (af == AF_INET6) {
|
||||
auto addr = IPv6Address::from_string(src);
|
||||
auto addr = IPv6Address::from_string({ src, strlen(src) });
|
||||
if (!addr.has_value()) {
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
|
|
|
@ -214,11 +214,11 @@ int OptionParser::handle_short_option()
|
|||
|
||||
option const* OptionParser::lookup_long_option(char* raw) const
|
||||
{
|
||||
StringView arg = raw;
|
||||
StringView arg { raw, strlen(raw) };
|
||||
|
||||
for (size_t index = 0; m_long_options[index].name; index++) {
|
||||
auto& option = m_long_options[index];
|
||||
StringView name = option.name;
|
||||
StringView name { option.name, strlen(option.name) };
|
||||
|
||||
if (!arg.starts_with(name))
|
||||
continue;
|
||||
|
@ -347,12 +347,12 @@ bool OptionParser::find_next_option()
|
|||
int getopt(int argc, char* const* argv, char const* short_options)
|
||||
{
|
||||
option dummy { nullptr, 0, nullptr, 0 };
|
||||
OptionParser parser { argc, argv, short_options, &dummy };
|
||||
OptionParser parser { argc, argv, { short_options, strlen(short_options) }, &dummy };
|
||||
return parser.getopt();
|
||||
}
|
||||
|
||||
int getopt_long(int argc, char* const* argv, char const* short_options, const struct option* long_options, int* out_long_option_index)
|
||||
{
|
||||
OptionParser parser { argc, argv, short_options, long_options, out_long_option_index };
|
||||
OptionParser parser { argc, argv, { short_options, strlen(short_options) }, long_options, out_long_option_index };
|
||||
return parser.getopt();
|
||||
}
|
||||
|
|
|
@ -16,7 +16,8 @@ int getsubopt(char** option_array, char* const* tokens, char** option_value)
|
|||
if (**option_array == '\0')
|
||||
return -1;
|
||||
|
||||
auto option_string = StringView(*option_array);
|
||||
auto const* option_ptr = *option_array;
|
||||
StringView option_string { option_ptr, strlen(option_ptr) };
|
||||
|
||||
auto possible_comma_location = option_string.find(',');
|
||||
char* option_end = const_cast<char*>(option_string.characters_without_null_termination()) + possible_comma_location.value_or(option_string.length());
|
||||
|
@ -34,7 +35,8 @@ int getsubopt(char** option_array, char* const* tokens, char** option_value)
|
|||
});
|
||||
|
||||
for (int count = 0; tokens[count] != NULL; ++count) {
|
||||
auto token_stringview = StringView(tokens[count]);
|
||||
auto const* token = tokens[count];
|
||||
StringView token_stringview { token, strlen(token) };
|
||||
if (!option_string.starts_with(token_stringview))
|
||||
continue;
|
||||
if (tokens[count][value_start - *option_array] != '\0')
|
||||
|
|
|
@ -93,7 +93,7 @@ hostent* gethostbyname(char const* name)
|
|||
{
|
||||
h_errno = 0;
|
||||
|
||||
auto ipv4_address = IPv4Address::from_string(name);
|
||||
auto ipv4_address = IPv4Address::from_string({ name, strlen(name) });
|
||||
|
||||
if (ipv4_address.has_value()) {
|
||||
gethostbyname_name_buffer = ipv4_address.value().to_string();
|
||||
|
|
|
@ -386,8 +386,8 @@ private:
|
|||
|
||||
extern "C" int vsscanf(char const* input, char const* format, va_list ap)
|
||||
{
|
||||
GenericLexer format_lexer { format };
|
||||
GenericLexer input_lexer { input };
|
||||
GenericLexer format_lexer { { format, strlen(format) } };
|
||||
GenericLexer input_lexer { { input, strlen(input) } };
|
||||
|
||||
int elements_matched = 0;
|
||||
|
||||
|
|
|
@ -243,10 +243,10 @@ static_assert(sizeof(sys_signame) == sizeof(char const*) * NSIG);
|
|||
int getsignalbyname(char const* name)
|
||||
{
|
||||
VERIFY(name);
|
||||
StringView name_sv(name);
|
||||
StringView name_sv { name, strlen(name) };
|
||||
for (size_t i = 0; i < NSIG; ++i) {
|
||||
auto signal_name = StringView(sys_signame[i]);
|
||||
if (signal_name == name_sv || (name_sv.starts_with("SIG") && signal_name == name_sv.substring_view(3)))
|
||||
StringView signal_name { sys_signame[i], sizeof(sys_signame[i]) - 1 };
|
||||
if (signal_name == name_sv || (name_sv.starts_with("SIG"sv) && signal_name == name_sv.substring_view(3)))
|
||||
return i;
|
||||
}
|
||||
errno = EINVAL;
|
||||
|
|
|
@ -114,7 +114,7 @@ int __attribute__((weak)) tgetnum(char const* id)
|
|||
static Vector<char> s_tgoto_buffer;
|
||||
char* __attribute__((weak)) tgoto([[maybe_unused]] char const* cap, [[maybe_unused]] int col, [[maybe_unused]] int row)
|
||||
{
|
||||
auto cap_str = StringView(cap).replace("%p1%d", String::number(col), ReplaceMode::FirstOnly).replace("%p2%d", String::number(row), ReplaceMode::FirstOnly);
|
||||
auto cap_str = StringView { cap, strlen(cap) }.replace("%p1%d"sv, String::number(col), ReplaceMode::FirstOnly).replace("%p2%d"sv, String::number(row), ReplaceMode::FirstOnly);
|
||||
|
||||
s_tgoto_buffer.clear_with_capacity();
|
||||
s_tgoto_buffer.ensure_capacity(cap_str.length());
|
||||
|
|
|
@ -377,7 +377,7 @@ void tzset()
|
|||
StringView time_zone;
|
||||
|
||||
if (char* tz = getenv("TZ"); tz != nullptr)
|
||||
time_zone = tz;
|
||||
time_zone = { tz, strlen(tz) };
|
||||
else
|
||||
time_zone = TimeZone::system_time_zone();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue