From c70f45ff4498fcb7ce0671e9107ecff8009d7eb2 Mon Sep 17 00:00:00 2001 From: sin-ack Date: Mon, 11 Jul 2022 19:53:29 +0000 Subject: [PATCH] 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). --- AK/Demangle.h | 2 +- AK/GenericLexer.h | 2 +- AK/JsonObjectSerializer.h | 4 +- AK/SourceLocation.h | 4 +- Kernel/Arch/x86/common/Processor.cpp | 2 +- Kernel/CommandLine.cpp | 2 +- Kernel/Net/IPv4Socket.cpp | 2 +- Kernel/init.cpp | 2 +- .../LibWeb/WrapperGenerator/main.cpp | 2 +- Tests/AK/TestSourceLocation.cpp | 2 +- Tests/AK/TestStringView.cpp | 2 +- Tests/AK/TestUtf8.cpp | 40 ++++++++-------- Tests/LibC/TestLibCTime.cpp | 8 ++-- Tests/LibC/TestRealpath.cpp | 4 +- Userland/Applications/Help/main.cpp | 10 ++-- Userland/Applications/SpaceAnalyzer/main.cpp | 3 +- .../Applications/Spreadsheet/ExportDialog.cpp | 4 +- Userland/DevTools/HackStudio/main.cpp | 6 ++- Userland/Libraries/LibArchive/Tar.h | 2 +- Userland/Libraries/LibC/arpa/inet.cpp | 2 +- Userland/Libraries/LibC/getopt.cpp | 8 ++-- Userland/Libraries/LibC/getsubopt.cpp | 6 ++- Userland/Libraries/LibC/netdb.cpp | 2 +- Userland/Libraries/LibC/scanf.cpp | 4 +- Userland/Libraries/LibC/signal.cpp | 6 +-- Userland/Libraries/LibC/termcap.cpp | 2 +- Userland/Libraries/LibC/time.cpp | 2 +- Userland/Libraries/LibCore/Account.cpp | 22 +++++---- Userland/Libraries/LibCore/ArgsParser.cpp | 28 +++++------ Userland/Libraries/LibCore/DateTime.cpp | 6 ++- Userland/Libraries/LibCore/FileWatcher.cpp | 2 +- Userland/Libraries/LibCore/System.cpp | 3 +- .../LibCore/SystemServerTakeover.cpp | 2 +- Userland/Libraries/LibCoredump/Reader.cpp | 6 ++- Userland/Libraries/LibCoredump/Reader.h | 3 +- Userland/Libraries/LibCrypt/crypt.cpp | 2 +- Userland/Libraries/LibEDID/EDID.cpp | 2 +- Userland/Libraries/LibELF/DynamicLinker.cpp | 9 ++-- Userland/Libraries/LibELF/DynamicObject.cpp | 3 +- Userland/Libraries/LibELF/DynamicObject.h | 3 +- Userland/Libraries/LibGUI/Window.cpp | 2 +- Userland/Libraries/LibGfx/Color.cpp | 2 +- .../Libraries/LibGfx/Font/FontStyleMapping.h | 3 +- Userland/Libraries/LibIPC/Encoder.cpp | 2 +- Userland/Libraries/LibJS/AST.cpp | 3 +- Userland/Libraries/LibJS/Token.h | 4 +- Userland/Libraries/LibLine/Editor.cpp | 2 +- Userland/Libraries/LibMain/Main.cpp | 2 +- Userland/Libraries/LibRegex/C/Regex.cpp | 5 +- .../Libraries/LibSanitizer/UBSanitizer.cpp | 3 +- Userland/Libraries/LibTimeZone/TimeZone.cpp | 2 +- Userland/Libraries/LibWeb/Layout/Node.cpp | 3 +- Userland/Services/DHCPClient/DHCPv4.h | 13 ++++- Userland/Shell/Builtin.cpp | 32 +++++++------ Userland/Shell/Shell.cpp | 12 +++-- Userland/Utilities/du.cpp | 9 ++-- Userland/Utilities/expr.cpp | 2 +- Userland/Utilities/fgrep.cpp | 3 +- Userland/Utilities/find.cpp | 26 +++++----- Userland/Utilities/groupdel.cpp | 7 +-- Userland/Utilities/ls.cpp | 6 +-- Userland/Utilities/mktemp.cpp | 6 ++- Userland/Utilities/netstat.cpp | 16 +++---- Userland/Utilities/paste.cpp | 2 +- Userland/Utilities/pidof.cpp | 2 +- Userland/Utilities/pls.cpp | 2 +- Userland/Utilities/pro.cpp | 4 +- Userland/Utilities/strace.cpp | 2 +- Userland/Utilities/stty.cpp | 2 +- Userland/Utilities/test-unveil.cpp | 4 +- Userland/Utilities/test.cpp | 48 ++++++++++++------- Userland/Utilities/top.cpp | 2 +- Userland/Utilities/usermod.cpp | 2 +- Userland/Utilities/wasm.cpp | 4 +- Userland/Utilities/xargs.cpp | 2 +- 75 files changed, 264 insertions(+), 203 deletions(-) diff --git a/AK/Demangle.h b/AK/Demangle.h index f3b7746af6d6ce..14627670dcbb82 100644 --- a/AK/Demangle.h +++ b/AK/Demangle.h @@ -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; diff --git a/AK/GenericLexer.h b/AK/GenericLexer.h index 5db2bdc5eacbcc..f5d5396ac4c41d 100644 --- a/AK/GenericLexer.h +++ b/AK/GenericLexer.h @@ -92,7 +92,7 @@ class GenericLexer { 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") diff --git a/AK/JsonObjectSerializer.h b/AK/JsonObjectSerializer.h index 0869205517d6aa..b13b45985b7cea 100644 --- a/AK/JsonObjectSerializer.h +++ b/AK/JsonObjectSerializer.h @@ -89,11 +89,11 @@ class JsonObjectSerializer { TRY(begin_item(key)); if constexpr (IsLegacyBuilder) { 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 {}; diff --git a/AK/SourceLocation.h b/AK/SourceLocation.h index c70d71314428c7..a25f661a13a1f2 100644 --- a/AK/SourceLocation.h +++ b/AK/SourceLocation.h @@ -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()) diff --git a/Kernel/Arch/x86/common/Processor.cpp b/Kernel/Arch/x86/common/Processor.cpp index ff388b22a80230..e76fc36840ac86 100644 --- a/Kernel/Arch/x86/common/Processor.cpp +++ b/Kernel/Arch/x86/common/Processor.cpp @@ -710,7 +710,7 @@ UNMAP_AFTER_INIT void Processor::detect_hypervisor_hyperv(CPUID const& hyperviso alignas(sizeof(u32)) char interface_signature_buffer[5]; *reinterpret_cast(interface_signature_buffer) = hypervisor_interface.eax(); interface_signature_buffer[4] = '\0'; - StringView hyperv_interface_signature(interface_signature_buffer); + StringView hyperv_interface_signature { interface_signature_buffer, strlen(interface_signature_buffer) }; dmesgln("CPU[{}]: Hyper-V interface signature '{}' ({:#x})", current_id(), hyperv_interface_signature, hypervisor_interface.eax()); diff --git a/Kernel/CommandLine.cpp b/Kernel/CommandLine.cpp index b43487fb5ac31b..1a20968c44a927 100644 --- a/Kernel/CommandLine.cpp +++ b/Kernel/CommandLine.cpp @@ -41,7 +41,7 @@ CommandLine const& kernel_command_line() UNMAP_AFTER_INIT void CommandLine::initialize() { VERIFY(!s_the); - s_the = new CommandLine(s_cmd_line); + s_the = new CommandLine({ s_cmd_line, strlen(s_cmd_line) }); dmesgln("Kernel Commandline: {}", kernel_command_line().string()); // Validate the modes the user passed in. (void)s_the->panic_mode(Validate::Yes); diff --git a/Kernel/Net/IPv4Socket.cpp b/Kernel/Net/IPv4Socket.cpp index 1c5dc22fbac853..67dd1ea1fe408a 100644 --- a/Kernel/Net/IPv4Socket.cpp +++ b/Kernel/Net/IPv4Socket.cpp @@ -689,7 +689,7 @@ ErrorOr IPv4Socket::ioctl(OpenFileDescription&, unsigned request, Userspac memcpy(namebuf, ifr.ifr_name, IFNAMSIZ); namebuf[sizeof(namebuf) - 1] = '\0'; - auto adapter = NetworkingManagement::the().lookup_by_name(namebuf); + auto adapter = NetworkingManagement::the().lookup_by_name({ namebuf, strlen(namebuf) }); if (!adapter) return ENODEV; diff --git a/Kernel/init.cpp b/Kernel/init.cpp index cf15035cbdc897..bad8511cf05a63 100644 --- a/Kernel/init.cpp +++ b/Kernel/init.cpp @@ -408,7 +408,7 @@ UNMAP_AFTER_INIT void setup_serial_debug() // serial_debug will output all the dbgln() data to COM1 at // 8-N-1 57600 baud. this is particularly useful for debugging the boot // process on live hardware. - if (StringView(kernel_cmdline).contains("serial_debug")) { + if (StringView { kernel_cmdline, strlen(kernel_cmdline) }.contains("serial_debug"sv)) { set_serial_debug(true); } } diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator/main.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator/main.cpp index 15107e5a701632..a641334f06b954 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator/main.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator/main.cpp @@ -61,7 +61,7 @@ int main(int argc, char** argv) .short_name = 'i', .value_name = "path", .accept_value = [&](char const* s) { - s_header_search_paths.append(s); + s_header_search_paths.append({ s, strlen(s) }); return true; }, }); diff --git a/Tests/AK/TestSourceLocation.cpp b/Tests/AK/TestSourceLocation.cpp index fcaf905cd4cdbd..84713381c17436 100644 --- a/Tests/AK/TestSourceLocation.cpp +++ b/Tests/AK/TestSourceLocation.cpp @@ -31,7 +31,7 @@ static StringView test_default_arg(SourceLocation const& loc = SourceLocation::c TEST_CASE(default_arg_scenario) { auto actual_calling_function = test_default_arg(); - auto expected_calling_function = StringView(__FUNCTION__); + auto expected_calling_function = StringView { __FUNCTION__, strlen(__FUNCTION__) }; EXPECT_EQ(expected_calling_function, actual_calling_function); } diff --git a/Tests/AK/TestStringView.cpp b/Tests/AK/TestStringView.cpp index 6623081c3c999a..2de877adeb899a 100644 --- a/Tests/AK/TestStringView.cpp +++ b/Tests/AK/TestStringView.cpp @@ -20,7 +20,7 @@ TEST_CASE(construct_empty) TEST_CASE(view_literal) { char const* truth = "cats rule dogs drool"; - StringView view(truth); + StringView view { truth, strlen(truth) }; EXPECT_EQ(view.is_null(), false); EXPECT_EQ(view.characters_without_null_termination(), truth); EXPECT_EQ(view, view); diff --git a/Tests/AK/TestUtf8.cpp b/Tests/AK/TestUtf8.cpp index f8e48672583b81..c8d8b5511f1dbb 100644 --- a/Tests/AK/TestUtf8.cpp +++ b/Tests/AK/TestUtf8.cpp @@ -51,33 +51,33 @@ TEST_CASE(decode_utf8) TEST_CASE(validate_invalid_ut8) { size_t valid_bytes; - char invalid_utf8_1[] = { 42, 35, (char)182, 9, 0 }; - Utf8View utf8_1 { StringView { invalid_utf8_1 } }; + char invalid_utf8_1[] = { 42, 35, (char)182, 9 }; + Utf8View utf8_1 { StringView { invalid_utf8_1, 4 } }; EXPECT(!utf8_1.validate(valid_bytes)); EXPECT(valid_bytes == 2); - char invalid_utf8_2[] = { 42, 35, (char)208, (char)208, 0 }; - Utf8View utf8_2 { StringView { invalid_utf8_2 } }; + char invalid_utf8_2[] = { 42, 35, (char)208, (char)208 }; + Utf8View utf8_2 { StringView { invalid_utf8_2, 4 } }; EXPECT(!utf8_2.validate(valid_bytes)); EXPECT(valid_bytes == 2); - char invalid_utf8_3[] = { (char)208, 0 }; - Utf8View utf8_3 { StringView { invalid_utf8_3 } }; + char invalid_utf8_3[] = { (char)208 }; + Utf8View utf8_3 { StringView { invalid_utf8_3, 1 } }; EXPECT(!utf8_3.validate(valid_bytes)); EXPECT(valid_bytes == 0); - char invalid_utf8_4[] = { (char)208, 35, 0 }; - Utf8View utf8_4 { StringView { invalid_utf8_4 } }; + char invalid_utf8_4[] = { (char)208, 35 }; + Utf8View utf8_4 { StringView { invalid_utf8_4, 2 } }; EXPECT(!utf8_4.validate(valid_bytes)); EXPECT(valid_bytes == 0); - char invalid_utf8_5[] = { (char)0xf4, (char)0x8f, (char)0xbf, (char)0xc0, 0 }; // U+110000 - Utf8View utf8_5 { StringView { invalid_utf8_5 } }; + char invalid_utf8_5[] = { (char)0xf4, (char)0x8f, (char)0xbf, (char)0xc0 }; // U+110000 + Utf8View utf8_5 { StringView { invalid_utf8_5, 4 } }; EXPECT(!utf8_5.validate(valid_bytes)); EXPECT(valid_bytes == 0); - char invalid_utf8_6[] = { (char)0xf4, (char)0xa1, (char)0xb0, (char)0xbd, 0 }; // U+121c3d - Utf8View utf8_6 { StringView { invalid_utf8_6 } }; + char invalid_utf8_6[] = { (char)0xf4, (char)0xa1, (char)0xb0, (char)0xbd }; // U+121c3d + Utf8View utf8_6 { StringView { invalid_utf8_6, 4 } }; EXPECT(!utf8_6.validate(valid_bytes)); EXPECT(valid_bytes == 0); } @@ -122,8 +122,8 @@ TEST_CASE(decode_invalid_ut8) { // Test case 1 : Getting an extension byte as first byte of the code point { - char raw_data[] = { 'a', 'b', (char)0xA0, 'd', 0 }; - Utf8View view { StringView { raw_data } }; + char raw_data[] = { 'a', 'b', (char)0xA0, 'd' }; + Utf8View view { StringView { raw_data, 4 } }; u32 expected_characters[] = { 'a', 'b', 0xFFFD, 'd' }; String expected_underlying_bytes[] = { "a", "b", "\xA0", "d" }; size_t expected_size = sizeof(expected_characters) / sizeof(expected_characters[0]); @@ -140,8 +140,8 @@ TEST_CASE(decode_invalid_ut8) // Test case 2 : Getting a non-extension byte when an extension byte is expected { - char raw_data[] = { 'a', 'b', (char)0xC0, 'd', 'e', 0 }; - Utf8View view { StringView { raw_data } }; + char raw_data[] = { 'a', 'b', (char)0xC0, 'd', 'e' }; + Utf8View view { StringView { raw_data, 5 } }; u32 expected_characters[] = { 'a', 'b', 0xFFFD, 'd', 'e' }; String expected_underlying_bytes[] = { "a", "b", "\xC0", "d", "e" }; size_t expected_size = sizeof(expected_characters) / sizeof(expected_characters[0]); @@ -158,8 +158,8 @@ TEST_CASE(decode_invalid_ut8) // Test case 3 : Not enough bytes before the end of the string { - char raw_data[] = { 'a', 'b', (char)0x90, 'd', 0 }; - Utf8View view { StringView { raw_data } }; + char raw_data[] = { 'a', 'b', (char)0x90, 'd' }; + Utf8View view { StringView { raw_data, 4 } }; u32 expected_characters[] = { 'a', 'b', 0xFFFD, 'd' }; String expected_underlying_bytes[] = { "a", "b", "\x90", "d" }; size_t expected_size = sizeof(expected_characters) / sizeof(expected_characters[0]); @@ -176,8 +176,8 @@ TEST_CASE(decode_invalid_ut8) // Test case 4 : Not enough bytes at the end of the string { - char raw_data[] = { 'a', 'b', 'c', (char)0x90, 0 }; - Utf8View view { StringView { raw_data } }; + char raw_data[] = { 'a', 'b', 'c', (char)0x90 }; + Utf8View view { StringView { raw_data, 4 } }; u32 expected_characters[] = { 'a', 'b', 'c', 0xFFFD }; String expected_underlying_bytes[] = { "a", "b", "c", "\x90" }; size_t expected_size = sizeof(expected_characters) / sizeof(expected_characters[0]); diff --git a/Tests/LibC/TestLibCTime.cpp b/Tests/LibC/TestLibCTime.cpp index 6534e9e0c182be..adcd68ba8feec1 100644 --- a/Tests/LibC/TestLibCTime.cpp +++ b/Tests/LibC/TestLibCTime.cpp @@ -41,7 +41,7 @@ TEST_CASE(asctime) time_t epoch = 0; auto result = asctime(localtime(&epoch)); - EXPECT_EQ(expected_epoch, StringView(result)); + EXPECT_EQ(expected_epoch, StringView(result, strlen(result))); } TEST_CASE(asctime_r) @@ -51,7 +51,7 @@ TEST_CASE(asctime_r) char buffer[26] {}; time_t epoch = 0; auto result = asctime_r(localtime(&epoch), buffer); - EXPECT_EQ(expected_epoch, StringView(result)); + EXPECT_EQ(expected_epoch, StringView(result, strlen(result))); } TEST_CASE(ctime) @@ -61,7 +61,7 @@ TEST_CASE(ctime) time_t epoch = 0; auto result = ctime(&epoch); - EXPECT_EQ(expected_epoch, StringView(result)); + EXPECT_EQ(expected_epoch, StringView(result, strlen(result))); } TEST_CASE(ctime_r) @@ -72,7 +72,7 @@ TEST_CASE(ctime_r) time_t epoch = 0; auto result = ctime_r(&epoch, buffer); - EXPECT_EQ(expected_epoch, StringView(result)); + EXPECT_EQ(expected_epoch, StringView(result, strlen(result))); } TEST_CASE(tzset) diff --git a/Tests/LibC/TestRealpath.cpp b/Tests/LibC/TestRealpath.cpp index 21a1d527f81206..dc6e558d3bf4fa 100644 --- a/Tests/LibC/TestRealpath.cpp +++ b/Tests/LibC/TestRealpath.cpp @@ -47,7 +47,7 @@ TEST_CASE(overlong_realpath) // Then, create a long path. StringBuilder expected; - expected.append(tmp_dir); + expected.append({ tmp_dir, strlen(tmp_dir) }); // But first, demonstrate the functionality at a reasonable depth: auto expected_str = expected.build(); @@ -63,7 +63,7 @@ TEST_CASE(overlong_realpath) return; } expected.append('/'); - expected.append(PATH_LOREM_250); + expected.append({ PATH_LOREM_250, strlen(PATH_LOREM_250) }); ret = chdir(PATH_LOREM_250); if (ret < 0) { perror("chdir iter"); diff --git a/Userland/Applications/Help/main.cpp b/Userland/Applications/Help/main.cpp index c3838dee50e339..4dbb1741f0ad64 100644 --- a/Userland/Applications/Help/main.cpp +++ b/Userland/Applications/Help/main.cpp @@ -17,7 +17,7 @@ using namespace Help; -static String parse_input(char const* input) +static String parse_input(StringView input) { AK::URL url(input); if (url.is_valid()) @@ -50,9 +50,10 @@ ErrorOr serenity_main(Main::Arguments arguments) .name = "section", .min_values = 0, .max_values = 1, - .accept_value = [&](char const* input) { + .accept_value = [&](char const* input_ptr) { + StringView input { input_ptr, strlen(input_ptr) }; // If it's a number, use it as the section - if (auto number = StringView(input).to_int(); number.has_value()) { + if (auto number = input.to_int(); number.has_value()) { section = number.value(); return true; } @@ -66,7 +67,8 @@ ErrorOr serenity_main(Main::Arguments arguments) .name = "page", .min_values = 0, .max_values = 1, - .accept_value = [&](char const* input) { + .accept_value = [&](char const* input_ptr) { + StringView input { input_ptr, strlen(input_ptr) }; // If start_page was already set by our section arg, then it can't be set again if (start_page.is_empty()) return false; diff --git a/Userland/Applications/SpaceAnalyzer/main.cpp b/Userland/Applications/SpaceAnalyzer/main.cpp index 67615db99e9d5d..62ff16025b8024 100644 --- a/Userland/Applications/SpaceAnalyzer/main.cpp +++ b/Userland/Applications/SpaceAnalyzer/main.cpp @@ -259,7 +259,8 @@ static void analyze(RefPtr tree, SpaceAnalyzer::TreeMapWidget& treemapwidg if (!first) { builder.append(", "); } - builder.append(strerror(key)); + auto const* error = strerror(key); + builder.append({ error, strlen(error) }); builder.append(" ("); int value = error_accumulator.get(key).value(); builder.append(String::number(value)); diff --git a/Userland/Applications/Spreadsheet/ExportDialog.cpp b/Userland/Applications/Spreadsheet/ExportDialog.cpp index 37c8f2e72dc48a..502e9be0b2d6ce 100644 --- a/Userland/Applications/Spreadsheet/ExportDialog.cpp +++ b/Userland/Applications/Spreadsheet/ExportDialog.cpp @@ -280,9 +280,11 @@ Result ExportDialog::make_and_run_for(StringView mime, Core::File& bool result = file.write(file_content); if (!result) { int error_number = errno; + auto const* error = strerror(error_number); + StringBuilder sb; sb.append("Unable to save file. Error: "); - sb.append(strerror(error_number)); + sb.append({ error, strlen(error) }); return sb.to_string(); } diff --git a/Userland/DevTools/HackStudio/main.cpp b/Userland/DevTools/HackStudio/main.cpp index 7f855470d79319..fd23ec8027dce7 100644 --- a/Userland/DevTools/HackStudio/main.cpp +++ b/Userland/DevTools/HackStudio/main.cpp @@ -121,7 +121,11 @@ static void notify_make_not_available() static void update_path_environment_variable() { StringBuilder path; - path.append(getenv("PATH")); + + auto const* path_env_ptr = getenv("PATH"); + if (path_env_ptr != NULL) + path.append({ path_env_ptr, strlen(path_env_ptr) }); + if (path.length()) path.append(":"); path.append("/usr/local/sbin:/usr/local/bin:/usr/bin:/bin"); diff --git a/Userland/Libraries/LibArchive/Tar.h b/Userland/Libraries/LibArchive/Tar.h index af2bd9f171381e..dcfb38f9ecabb1 100644 --- a/Userland/Libraries/LibArchive/Tar.h +++ b/Userland/Libraries/LibArchive/Tar.h @@ -99,7 +99,7 @@ class [[gnu::packed]] TarFileHeader { time_t timestamp() const { return get_field_as_integral(m_timestamp); } unsigned checksum() const { return get_field_as_integral(m_checksum); } TarFileType type_flag() const { return TarFileType(m_type_flag); } - StringView link_name() const { return m_link_name; } + StringView link_name() const { return { m_link_name, strlen(m_link_name) }; } StringView magic() const { return get_field_as_string_view(m_magic); } StringView version() const { return get_field_as_string_view(m_version); } StringView owner_name() const { return get_field_as_string_view(m_owner_name); } diff --git a/Userland/Libraries/LibC/arpa/inet.cpp b/Userland/Libraries/LibC/arpa/inet.cpp index 86c6f307a69ee1..542fde7482124e 100644 --- a/Userland/Libraries/LibC/arpa/inet.cpp +++ b/Userland/Libraries/LibC/arpa/inet.cpp @@ -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; diff --git a/Userland/Libraries/LibC/getopt.cpp b/Userland/Libraries/LibC/getopt.cpp index 2081d214095502..1f46fede9423e1 100644 --- a/Userland/Libraries/LibC/getopt.cpp +++ b/Userland/Libraries/LibC/getopt.cpp @@ -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(); } diff --git a/Userland/Libraries/LibC/getsubopt.cpp b/Userland/Libraries/LibC/getsubopt.cpp index 1bf78efc4298ec..1f8c65635b1f66 100644 --- a/Userland/Libraries/LibC/getsubopt.cpp +++ b/Userland/Libraries/LibC/getsubopt.cpp @@ -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(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') diff --git a/Userland/Libraries/LibC/netdb.cpp b/Userland/Libraries/LibC/netdb.cpp index ef93dfd08de710..6288d468413978 100644 --- a/Userland/Libraries/LibC/netdb.cpp +++ b/Userland/Libraries/LibC/netdb.cpp @@ -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(); diff --git a/Userland/Libraries/LibC/scanf.cpp b/Userland/Libraries/LibC/scanf.cpp index 82d96cbef740ee..d82c5d6b945228 100644 --- a/Userland/Libraries/LibC/scanf.cpp +++ b/Userland/Libraries/LibC/scanf.cpp @@ -386,8 +386,8 @@ struct ReadElement { 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; diff --git a/Userland/Libraries/LibC/signal.cpp b/Userland/Libraries/LibC/signal.cpp index 7291059fdc739c..850dd48caa5664 100644 --- a/Userland/Libraries/LibC/signal.cpp +++ b/Userland/Libraries/LibC/signal.cpp @@ -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; diff --git a/Userland/Libraries/LibC/termcap.cpp b/Userland/Libraries/LibC/termcap.cpp index 230428ffd56b45..171b440eb7c80d 100644 --- a/Userland/Libraries/LibC/termcap.cpp +++ b/Userland/Libraries/LibC/termcap.cpp @@ -114,7 +114,7 @@ int __attribute__((weak)) tgetnum(char const* id) static Vector 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()); diff --git a/Userland/Libraries/LibC/time.cpp b/Userland/Libraries/LibC/time.cpp index 0a66be371a8705..8cbc57220af2c6 100644 --- a/Userland/Libraries/LibC/time.cpp +++ b/Userland/Libraries/LibC/time.cpp @@ -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(); diff --git a/Userland/Libraries/LibCore/Account.cpp b/Userland/Libraries/LibCore/Account.cpp index 12e22d3b82aec8..eeab247d5d617b 100644 --- a/Userland/Libraries/LibCore/Account.cpp +++ b/Userland/Libraries/LibCore/Account.cpp @@ -40,7 +40,7 @@ static String get_salt() static Vector get_extra_gids(passwd const& pwd) { - StringView username { pwd.pw_name }; + StringView username { pwd.pw_name, strlen(pwd.pw_name) }; Vector extra_gids; setgrent(); for (auto* group = getgrent(); group; group = getgrent()) { @@ -78,7 +78,7 @@ ErrorOr Account::self([[maybe_unused]] Read options) spwd spwd = {}; #ifndef AK_OS_BSD_GENERIC if (options != Read::PasswdOnly) { - auto maybe_spwd = TRY(Core::System::getspnam(pwd->pw_name)); + auto maybe_spwd = TRY(Core::System::getspnam({ pwd->pw_name, strlen(pwd->pw_name) })); if (!maybe_spwd.has_value()) return Error::from_string_literal("No shadow entry for user"sv); spwd = maybe_spwd.release_value(); @@ -90,14 +90,14 @@ ErrorOr Account::self([[maybe_unused]] Read options) ErrorOr Account::from_name(char const* username, [[maybe_unused]] Read options) { - auto pwd = TRY(Core::System::getpwnam(username)); + auto pwd = TRY(Core::System::getpwnam({ username, strlen(username) })); if (!pwd.has_value()) return Error::from_string_literal("No such user"sv); spwd spwd = {}; #ifndef AK_OS_BSD_GENERIC if (options != Read::PasswdOnly) { - auto maybe_spwd = TRY(Core::System::getspnam(pwd->pw_name)); + auto maybe_spwd = TRY(Core::System::getspnam({ pwd->pw_name, strlen(pwd->pw_name) })); if (!maybe_spwd.has_value()) return Error::from_string_literal("No shadow entry for user"sv); spwd = maybe_spwd.release_value(); @@ -115,7 +115,7 @@ ErrorOr Account::from_uid(uid_t uid, [[maybe_unused]] Read options) spwd spwd = {}; #ifndef AK_OS_BSD_GENERIC if (options != Read::PasswdOnly) { - auto maybe_spwd = TRY(Core::System::getspnam(pwd->pw_name)); + auto maybe_spwd = TRY(Core::System::getspnam({ pwd->pw_name, strlen(pwd->pw_name) })); if (!maybe_spwd.has_value()) return Error::from_string_literal("No shadow entry for user"sv); spwd = maybe_spwd.release_value(); @@ -270,18 +270,22 @@ ErrorOr Account::sync() auto new_shadow_file_content = TRY(generate_shadow_file()); #endif + // FIXME: mkstemp taking Span makes this code entirely un-AKable. + // Make this code less char-pointery. char new_passwd_name[] = "/etc/passwd.XXXXXX"; + size_t new_passwd_name_length = strlen(new_passwd_name); #ifndef AK_OS_BSD_GENERIC char new_shadow_name[] = "/etc/shadow.XXXXXX"; + size_t new_shadow_name_length = strlen(new_shadow_name); #endif { - auto new_passwd_fd = TRY(Core::System::mkstemp(new_passwd_name)); + auto new_passwd_fd = TRY(Core::System::mkstemp({ new_passwd_name, new_passwd_name_length })); ScopeGuard new_passwd_fd_guard = [new_passwd_fd] { close(new_passwd_fd); }; TRY(Core::System::fchmod(new_passwd_fd, 0644)); #ifndef AK_OS_BSD_GENERIC - auto new_shadow_fd = TRY(Core::System::mkstemp(new_shadow_name)); + auto new_shadow_fd = TRY(Core::System::mkstemp({ new_shadow_name, new_shadow_name_length })); ScopeGuard new_shadow_fd_guard = [new_shadow_fd] { close(new_shadow_fd); }; TRY(Core::System::fchmod(new_shadow_fd, 0600)); #endif @@ -295,9 +299,9 @@ ErrorOr Account::sync() #endif } - TRY(Core::System::rename(new_passwd_name, "/etc/passwd")); + TRY(Core::System::rename({ new_passwd_name, new_passwd_name_length }, "/etc/passwd"sv)); #ifndef AK_OS_BSD_GENERIC - TRY(Core::System::rename(new_shadow_name, "/etc/shadow")); + TRY(Core::System::rename({ new_shadow_name, new_shadow_name_length }, "/etc/shadow"sv)); #endif return {}; diff --git a/Userland/Libraries/LibCore/ArgsParser.cpp b/Userland/Libraries/LibCore/ArgsParser.cpp index ead8ed0925236b..798a89fa2baa29 100644 --- a/Userland/Libraries/LibCore/ArgsParser.cpp +++ b/Userland/Libraries/LibCore/ArgsParser.cpp @@ -129,7 +129,7 @@ bool ArgsParser::parse(int argc, char* const* argv, FailureBehavior failure_beha } if (m_perform_autocomplete) { - autocomplete(stdout, argv[0], Span { argv + optind, static_cast(argc - optind) }); + autocomplete(stdout, { argv[0], strlen(argv[0]) }, Span { argv + optind, static_cast(argc - optind) }); if (failure_behavior == FailureBehavior::Exit || failure_behavior == FailureBehavior::PrintUsageAndExit) exit(0); return false; @@ -445,7 +445,7 @@ void ArgsParser::add_option(StringView& value, char const* help_string, char con short_name, value_name, [&value](char const* s) { - value = s; + value = { s, strlen(s) }; return true; }, hide_mode, @@ -462,7 +462,7 @@ void ArgsParser::add_option(int& value, char const* help_string, char const* lon short_name, value_name, [&value](char const* s) { - auto opt = StringView(s).to_int(); + auto opt = StringView { s, strlen(s) }.to_int(); value = opt.value_or(0); return opt.has_value(); }, @@ -480,7 +480,7 @@ void ArgsParser::add_option(unsigned& value, char const* help_string, char const short_name, value_name, [&value](char const* s) { - auto opt = StringView(s).to_uint(); + auto opt = StringView { s, strlen(s) }.to_uint(); value = opt.value_or(0); return opt.has_value(); }, @@ -533,7 +533,7 @@ void ArgsParser::add_option(Optional& value, char const* help_string, ch short_name, value_name, [&value](char const* s) { - value = AK::StringUtils::convert_to_uint(s); + value = AK::StringUtils::convert_to_uint({ s, strlen(s) }); return value.has_value(); }, hide_mode, @@ -552,7 +552,7 @@ void ArgsParser::add_option(Vector& values, char const* help_string, cha [&values, separator](char const* s) { bool parsed_all_values = true; - StringView { s }.for_each_split_view(separator, false, [&](auto value) { + StringView { s, strlen(s) }.for_each_split_view(separator, false, [&](auto value) { if (auto maybe_value = AK::StringUtils::convert_to_uint(value); maybe_value.has_value()) values.append(*maybe_value); else @@ -610,7 +610,7 @@ void ArgsParser::add_positional_argument(StringView& value, char const* help_str required == Required::Yes ? 1 : 0, 1, [&value](char const* s) { - value = s; + value = { s, strlen(s) }; return true; } }; @@ -625,7 +625,7 @@ void ArgsParser::add_positional_argument(int& value, char const* help_string, ch required == Required::Yes ? 1 : 0, 1, [&value](char const* s) { - auto opt = StringView(s).to_int(); + auto opt = StringView { s, strlen(s) }.to_int(); value = opt.value_or(0); return opt.has_value(); } @@ -641,7 +641,7 @@ void ArgsParser::add_positional_argument(unsigned& value, char const* help_strin required == Required::Yes ? 1 : 0, 1, [&value](char const* s) { - auto opt = StringView(s).to_uint(); + auto opt = StringView { s, strlen(s) }.to_uint(); value = opt.value_or(0); return opt.has_value(); } @@ -703,7 +703,7 @@ void ArgsParser::add_positional_argument(Vector& values, char const* required == Required::Yes ? 1 : 0, INT_MAX, [&values](char const* s) { - values.append(s); + values.append({ s, strlen(s) }); return true; } }; @@ -723,7 +723,7 @@ void ArgsParser::autocomplete(FILE* file, StringView program_name, Span FileWatcherBase::add_watch(String path, FileWatcherEvent::Type event_mask) diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp index bf48e871a8a77e..e5183206144152 100644 --- a/Userland/Libraries/LibCore/System.cpp +++ b/Userland/Libraries/LibCore/System.cpp @@ -970,7 +970,8 @@ ErrorOr exec(StringView filename, Span arguments, SearchInPath }; if (search_in_path == SearchInPath::Yes && !filename.contains('/')) { - StringView path = getenv("PATH"); + auto const* path_ptr = getenv("PATH"); + StringView path { path_ptr, strlen(path_ptr) }; if (path.is_empty()) path = "/bin:/usr/bin"; auto parts = path.split_view(':'); diff --git a/Userland/Libraries/LibCore/SystemServerTakeover.cpp b/Userland/Libraries/LibCore/SystemServerTakeover.cpp index a1dfe9e401aca4..dfbff119d3dce1 100644 --- a/Userland/Libraries/LibCore/SystemServerTakeover.cpp +++ b/Userland/Libraries/LibCore/SystemServerTakeover.cpp @@ -23,7 +23,7 @@ static void parse_sockets_from_system_server() return; } - for (auto& socket : StringView(sockets).split_view(' ')) { + for (auto& socket : StringView { sockets, strlen(sockets) }.split_view(' ')) { auto params = socket.split_view(':'); s_overtaken_sockets.set(params[0].to_string(), strtol(params[1].to_string().characters(), nullptr, 10)); } diff --git a/Userland/Libraries/LibCoredump/Reader.cpp b/Userland/Libraries/LibCoredump/Reader.cpp index e010abab58db9d..05c77f8584db46 100644 --- a/Userland/Libraries/LibCoredump/Reader.cpp +++ b/Userland/Libraries/LibCoredump/Reader.cpp @@ -157,7 +157,8 @@ const JsonObject Reader::process_info() const } if (!process_info_notes_entry) return {}; - auto process_info_json_value = JsonValue::from_string(process_info_notes_entry->json_data); + auto const* json_data_ptr = process_info_notes_entry->json_data; + auto process_info_json_value = JsonValue::from_string({ json_data_ptr, strlen(json_data_ptr) }); if (process_info_json_value.is_error()) return {}; if (!process_info_json_value.value().is_object()) @@ -256,7 +257,8 @@ HashMap Reader::metadata() const } if (!metadata_notes_entry) return {}; - auto metadata_json_value = JsonValue::from_string(metadata_notes_entry->json_data); + auto const* json_data_ptr = metadata_notes_entry->json_data; + auto metadata_json_value = JsonValue::from_string({ json_data_ptr, strlen(json_data_ptr) }); if (metadata_json_value.is_error()) return {}; if (!metadata_json_value.value().is_object()) diff --git a/Userland/Libraries/LibCoredump/Reader.h b/Userland/Libraries/LibCoredump/Reader.h index 8f13f74f701297..48a5fca355818e 100644 --- a/Userland/Libraries/LibCoredump/Reader.h +++ b/Userland/Libraries/LibCoredump/Reader.h @@ -133,12 +133,13 @@ void Reader::for_each_memory_region_info(Func func) const }; ByteReader::load(raw_data.data(), raw_memory_region_info); + auto const* region_name_ptr = bit_cast(raw_data.offset_pointer(raw_data.size())); MemoryRegionInfo memory_region_info { raw_memory_region_info.header, raw_memory_region_info.region_start, raw_memory_region_info.region_end, raw_memory_region_info.program_header_index, - { bit_cast(raw_data.offset_pointer(raw_data.size())) }, + { region_name_ptr, strlen(region_name_ptr) }, }; IterationDecision decision = func(memory_region_info); if (decision == IterationDecision::Break) diff --git a/Userland/Libraries/LibCrypt/crypt.cpp b/Userland/Libraries/LibCrypt/crypt.cpp index 9823ef980ec05c..0e27288d2b93ee 100644 --- a/Userland/Libraries/LibCrypt/crypt.cpp +++ b/Userland/Libraries/LibCrypt/crypt.cpp @@ -45,7 +45,7 @@ char* crypt_r(char const* key, char const* salt, struct crypt_data* data) data->result[header_len] = '$'; Crypto::Hash::SHA256 sha; - sha.update(key); + sha.update(StringView { key, strlen(key) }); sha.update((u8 const*)salt_value, salt_len); auto digest = sha.digest(); diff --git a/Userland/Libraries/LibEDID/EDID.cpp b/Userland/Libraries/LibEDID/EDID.cpp index 865d683c266b78..fdeee023c8658e 100644 --- a/Userland/Libraries/LibEDID/EDID.cpp +++ b/Userland/Libraries/LibEDID/EDID.cpp @@ -417,7 +417,7 @@ StringView Parser::version() const StringView Parser::legacy_manufacturer_id() const { - return m_legacy_manufacturer_id; + return { m_legacy_manufacturer_id, strlen(m_legacy_manufacturer_id) }; } #ifndef KERNEL diff --git a/Userland/Libraries/LibELF/DynamicLinker.cpp b/Userland/Libraries/LibELF/DynamicLinker.cpp index 5e1fe96a207cb2..5e464cd0cb8c4e 100644 --- a/Userland/Libraries/LibELF/DynamicLinker.cpp +++ b/Userland/Libraries/LibELF/DynamicLinker.cpp @@ -487,19 +487,20 @@ static Result __dlsym(void* handle, char const* symbol_na __pthread_mutex_lock(&s_loader_lock); ScopeGuard unlock_guard = [] { __pthread_mutex_unlock(&s_loader_lock); }; + StringView symbol_name_view { symbol_name, strlen(symbol_name) }; Optional symbol; if (handle) { auto object = static_cast(handle); - symbol = object->lookup_symbol(symbol_name); + symbol = object->lookup_symbol(symbol_name_view); } else { // When handle is 0 (RTLD_DEFAULT) we should look up the symbol in all global modules // https://pubs.opengroup.org/onlinepubs/009604499/functions/dlsym.html - symbol = DynamicLinker::lookup_global_symbol(symbol_name); + symbol = DynamicLinker::lookup_global_symbol(symbol_name_view); } if (!symbol.has_value()) - return DlErrorMessage { String::formatted("Symbol {} not found", symbol_name) }; + return DlErrorMessage { String::formatted("Symbol {} not found", symbol_name_view) }; if (symbol.value().type == STT_GNU_IFUNC) return (void*)reinterpret_cast(symbol.value().address.as_ptr())(); @@ -551,7 +552,7 @@ static Result __dladdr(void* addr, Dl_info* info) static void read_environment_variables() { for (char** env = s_envp; *env; ++env) { - StringView env_string { *env }; + StringView env_string { *env, strlen(*env) }; if (env_string == "_LOADER_BREAKPOINT=1"sv) { s_do_breakpoint_trap_before_entry = true; } diff --git a/Userland/Libraries/LibELF/DynamicObject.cpp b/Userland/Libraries/LibELF/DynamicObject.cpp index 64b4e629ce12bd..29471f6560974b 100644 --- a/Userland/Libraries/LibELF/DynamicObject.cpp +++ b/Userland/Libraries/LibELF/DynamicObject.cpp @@ -336,7 +336,8 @@ auto DynamicObject::HashSection::lookup_gnu_symbol(StringView name, u32 hash_val StringView DynamicObject::symbol_string_table_string(ElfW(Word) index) const { - return StringView { (char const*)base_address().offset(m_string_table_offset + index).as_ptr() }; + auto const* symbol_string_table_ptr = reinterpret_cast(base_address().offset(m_string_table_offset + index).as_ptr()); + return StringView { symbol_string_table_ptr, strlen(symbol_string_table_ptr) }; } char const* DynamicObject::raw_symbol_string_table_string(ElfW(Word) index) const diff --git a/Userland/Libraries/LibELF/DynamicObject.h b/Userland/Libraries/LibELF/DynamicObject.h index 2d40e8dc704949..7bf874f410dc23 100644 --- a/Userland/Libraries/LibELF/DynamicObject.h +++ b/Userland/Libraries/LibELF/DynamicObject.h @@ -488,8 +488,7 @@ inline void DynamicObject::for_each_needed_library(F func) const if (entry.tag() != DT_NEEDED) return; ElfW(Word) offset = entry.val(); - StringView name { (const char*)(m_base_address.offset(m_string_table_offset).offset(offset)).as_ptr() }; - func(name); + func(symbol_string_table_string(offset)); }); } diff --git a/Userland/Libraries/LibGUI/Window.cpp b/Userland/Libraries/LibGUI/Window.cpp index 96b0b5065d67e9..54b83c3b46f493 100644 --- a/Userland/Libraries/LibGUI/Window.cpp +++ b/Userland/Libraries/LibGUI/Window.cpp @@ -125,7 +125,7 @@ void Window::show() Gfx::IntRect launch_origin_rect; if (auto* launch_origin_rect_string = getenv("__libgui_launch_origin_rect")) { - auto parts = StringView(launch_origin_rect_string).split_view(','); + auto parts = StringView { launch_origin_rect_string, strlen(launch_origin_rect_string) }.split_view(','); if (parts.size() == 4) { launch_origin_rect = Gfx::IntRect { parts[0].to_int().value_or(0), diff --git a/Userland/Libraries/LibGfx/Color.cpp b/Userland/Libraries/LibGfx/Color.cpp index 7d76698d9ddecf..b5abcb8844f2f2 100644 --- a/Userland/Libraries/LibGfx/Color.cpp +++ b/Userland/Libraries/LibGfx/Color.cpp @@ -80,7 +80,7 @@ Optional Color::from_string(StringView string) struct ColorAndWebName { constexpr ColorAndWebName(ARGB32 c, char const* n) : color(c) - , name(n) + , name(n != nullptr ? StringView { n, __builtin_strlen(n) } : StringView {}) { } ARGB32 color; diff --git a/Userland/Libraries/LibGfx/Font/FontStyleMapping.h b/Userland/Libraries/LibGfx/Font/FontStyleMapping.h index 1653dd0e43bf18..cad4e57c21309b 100644 --- a/Userland/Libraries/LibGfx/Font/FontStyleMapping.h +++ b/Userland/Libraries/LibGfx/Font/FontStyleMapping.h @@ -12,9 +12,10 @@ namespace Gfx { struct FontStyleMapping { + // NOTE: __builtin_strlen required to make this work at compile time. constexpr FontStyleMapping(int s, char const* n) : style(s) - , name(n) + , name(StringView { n, __builtin_strlen(n) }) { } int style { 0 }; diff --git a/Userland/Libraries/LibIPC/Encoder.cpp b/Userland/Libraries/LibIPC/Encoder.cpp index 430659dd0edf5d..64e67b3c2b8f24 100644 --- a/Userland/Libraries/LibIPC/Encoder.cpp +++ b/Userland/Libraries/LibIPC/Encoder.cpp @@ -135,7 +135,7 @@ Encoder& Encoder::operator<<(double value) Encoder& Encoder::operator<<(char const* value) { - return *this << StringView(value); + return *this << StringView { value, strlen(value) }; } Encoder& Encoder::operator<<(StringView value) diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp index fb87f6d4373a4e..63a2b11dadd0bb 100644 --- a/Userland/Libraries/LibJS/AST.cpp +++ b/Userland/Libraries/LibJS/AST.cpp @@ -64,7 +64,8 @@ class InterpreterNodeScope { String ASTNode::class_name() const { // NOTE: We strip the "JS::" prefix. - return demangle(typeid(*this).name()).substring(4); + auto const* typename_ptr = typeid(*this).name(); + return demangle({ typename_ptr, strlen(typename_ptr) }).substring(4); } static void print_indent(int indent) diff --git a/Userland/Libraries/LibJS/Token.h b/Userland/Libraries/LibJS/Token.h index 086c1df3a3ff8f..38c629d7af014a 100644 --- a/Userland/Libraries/LibJS/Token.h +++ b/Userland/Libraries/LibJS/Token.h @@ -15,12 +15,12 @@ namespace JS { // U+2028 LINE SEPARATOR constexpr char const line_separator_chars[] { (char)0xe2, (char)0x80, (char)0xa8, 0 }; -constexpr const StringView LINE_SEPARATOR_STRING { line_separator_chars }; +constexpr const StringView LINE_SEPARATOR_STRING { line_separator_chars, sizeof(line_separator_chars) - 1 }; constexpr const u32 LINE_SEPARATOR { 0x2028 }; // U+2029 PARAGRAPH SEPARATOR constexpr char const paragraph_separator_chars[] { (char)0xe2, (char)0x80, (char)0xa9, 0 }; -constexpr const StringView PARAGRAPH_SEPARATOR_STRING { paragraph_separator_chars }; +constexpr const StringView PARAGRAPH_SEPARATOR_STRING { paragraph_separator_chars, sizeof(paragraph_separator_chars) - 1 }; constexpr const u32 PARAGRAPH_SEPARATOR { 0x2029 }; // U+00A0 NO BREAK SPACE diff --git a/Userland/Libraries/LibLine/Editor.cpp b/Userland/Libraries/LibLine/Editor.cpp index 5c46e113462730..711328e5a67ccb 100644 --- a/Userland/Libraries/LibLine/Editor.cpp +++ b/Userland/Libraries/LibLine/Editor.cpp @@ -544,7 +544,7 @@ void Editor::initialize() m_configuration.set(Configuration::NonInteractive); } else { auto* term = getenv("TERM"); - if (StringView { term }.starts_with("xterm")) + if (term != NULL && StringView { term, strlen(term) }.starts_with("xterm"sv)) m_configuration.set(Configuration::Full); else m_configuration.set(Configuration::NoEscapeSequences); diff --git a/Userland/Libraries/LibMain/Main.cpp b/Userland/Libraries/LibMain/Main.cpp index 81b4bb8ffb29ee..69f6e30fa81bb1 100644 --- a/Userland/Libraries/LibMain/Main.cpp +++ b/Userland/Libraries/LibMain/Main.cpp @@ -34,7 +34,7 @@ int main(int argc, char** argv) Vector arguments; arguments.ensure_capacity(argc); for (int i = 0; i < argc; ++i) - arguments.unchecked_append(argv[i]); + arguments.unchecked_append({ argv[i], strlen(argv[i]) }); auto result = serenity_main({ .argc = argc, diff --git a/Userland/Libraries/LibRegex/C/Regex.cpp b/Userland/Libraries/LibRegex/C/Regex.cpp index a3b9abcc753e5e..28d93d5da6cd47 100644 --- a/Userland/Libraries/LibRegex/C/Regex.cpp +++ b/Userland/Libraries/LibRegex/C/Regex.cpp @@ -91,10 +91,11 @@ int regexec(regex_t const* reg, char const* string, size_t nmatch, regmatch_t pm } RegexResult result; + StringView string_view { string, strlen(string) }; if (eflags & REG_SEARCH) - result = preg->re->visit([&](auto& re) { return re->search(string, PosixOptions {} | (PosixFlags)eflags); }); + result = preg->re->visit([&](auto& re) { return re->search(string_view, PosixOptions {} | (PosixFlags)eflags); }); else - result = preg->re->visit([&](auto& re) { return re->match(string, PosixOptions {} | (PosixFlags)eflags); }); + result = preg->re->visit([&](auto& re) { return re->match(string_view, PosixOptions {} | (PosixFlags)eflags); }); if (result.success) { auto capture_groups_count = preg->re->visit([](auto& re) { return re->parser_result.capture_groups_count; }); diff --git a/Userland/Libraries/LibSanitizer/UBSanitizer.cpp b/Userland/Libraries/LibSanitizer/UBSanitizer.cpp index c86a74d9afa3bb..ae1c7ae21fd8bf 100644 --- a/Userland/Libraries/LibSanitizer/UBSanitizer.cpp +++ b/Userland/Libraries/LibSanitizer/UBSanitizer.cpp @@ -31,7 +31,8 @@ static void print_location(SourceLocation const& location) static bool checked_env_for_deadly = false; if (!checked_env_for_deadly) { checked_env_for_deadly = true; - StringView options = getenv("UBSAN_OPTIONS"); + auto const* options_ptr = getenv("UBSAN_OPTIONS"); + auto options = options_ptr != NULL ? StringView { options_ptr, strlen(options_ptr) } : StringView {}; // FIXME: Parse more options and complain about invalid options if (!options.is_null()) { if (options.contains("halt_on_error=1")) diff --git a/Userland/Libraries/LibTimeZone/TimeZone.cpp b/Userland/Libraries/LibTimeZone/TimeZone.cpp index 20bbb1736a74a1..fae4ad50a87f95 100644 --- a/Userland/Libraries/LibTimeZone/TimeZone.cpp +++ b/Userland/Libraries/LibTimeZone/TimeZone.cpp @@ -82,7 +82,7 @@ StringView system_time_zone() StringView current_time_zone() { - return canonicalize_time_zone(tzname[0]).value_or("UTC"sv); + return canonicalize_time_zone({ tzname[0], __builtin_strlen(tzname[0]) }).value_or("UTC"sv); } ErrorOr change_time_zone([[maybe_unused]] StringView time_zone) diff --git a/Userland/Libraries/LibWeb/Layout/Node.cpp b/Userland/Libraries/LibWeb/Layout/Node.cpp index bab1b79afcd1ae..a825ee167f07a8 100644 --- a/Userland/Libraries/LibWeb/Layout/Node.cpp +++ b/Userland/Libraries/LibWeb/Layout/Node.cpp @@ -574,7 +574,8 @@ bool Node::is_root_element() const String Node::class_name() const { - return demangle(typeid(*this).name()); + auto const* mangled_name = typeid(*this).name(); + return demangle({ mangled_name, strlen(mangled_name) }); } String Node::debug_description() const diff --git a/Userland/Services/DHCPClient/DHCPv4.h b/Userland/Services/DHCPClient/DHCPv4.h index de00f46c6dc996..762bc2b3455d00 100644 --- a/Userland/Services/DHCPClient/DHCPv4.h +++ b/Userland/Services/DHCPClient/DHCPv4.h @@ -214,8 +214,17 @@ class [[gnu::packed]] DHCPv4Packet { MACAddress const& chaddr() const { return *(MACAddress const*)&m_chaddr[0]; } void set_chaddr(MACAddress const& mac) { *(MACAddress*)&m_chaddr[0] = mac; } - StringView sname() const { return { (char const*)&m_sname[0] }; } - StringView file() const { return { (char const*)&m_file[0] }; } + StringView sname() const + { + char const* sname_ptr = reinterpret_cast(&m_sname[0]); + return { sname_ptr, strlen(sname_ptr) }; + } + + StringView file() const + { + char const* file_ptr = reinterpret_cast(&m_file[0]); + return { file_ptr, strlen(file_ptr) }; + } private: NetworkOrdered m_op; diff --git a/Userland/Shell/Builtin.cpp b/Userland/Shell/Builtin.cpp index 538a4bb6f1059d..28ffe7cbac58e8 100644 --- a/Userland/Shell/Builtin.cpp +++ b/Userland/Shell/Builtin.cpp @@ -35,7 +35,7 @@ int Shell::builtin_dump(int argc, char const** argv) if (argc != 2) return 1; - Parser { argv[1] }.parse()->dump(0); + Parser { StringView { argv[1], strlen(argv[1]) } }.parse()->dump(0); return 0; } @@ -124,7 +124,8 @@ int Shell::builtin_bg(int argc, char const** argv) .name = "job-id", .min_values = 0, .max_values = 1, - .accept_value = [&](StringView value) -> bool { + .accept_value = [&](auto value_ptr) -> bool { + StringView value { value_ptr, strlen(value_ptr) }; // Check if it's a pid (i.e. literal integer) if (auto number = value.to_uint(); number.has_value()) { job_id = number.value(); @@ -497,7 +498,8 @@ int Shell::builtin_fg(int argc, char const** argv) .name = "job-id", .min_values = 0, .max_values = 1, - .accept_value = [&](StringView value) -> bool { + .accept_value = [&](auto const* value_ptr) -> bool { + StringView value { value_ptr, strlen(value_ptr) }; // Check if it's a pid (i.e. literal integer) if (auto number = value.to_uint(); number.has_value()) { job_id = number.value(); @@ -568,7 +570,8 @@ int Shell::builtin_disown(int argc, char const** argv) .name = "job-id", .min_values = 0, .max_values = INT_MAX, - .accept_value = [&](StringView value) -> bool { + .accept_value = [&](auto const* value_ptr) -> bool { + StringView value { value_ptr, strlen(value_ptr) }; // Check if it's a pid (i.e. literal integer) if (auto number = value.to_uint(); number.has_value()) { job_ids.append(number.value()); @@ -721,7 +724,7 @@ int Shell::builtin_pushd(int argc, char const** argv) if (argc == 2) { directory_stack.append(cwd.characters()); if (argv[1][0] == '/') { - path_builder.append(argv[1]); + path_builder.append({ argv[1], strlen(argv[1]) }); } else { path_builder.appendff("{}/{}", cwd, argv[1]); } @@ -732,7 +735,7 @@ int Shell::builtin_pushd(int argc, char const** argv) if (arg[0] != '-') { if (arg[0] == '/') { - path_builder.append(arg); + path_builder.append({ arg, strlen(arg) }); } else path_builder.appendff("{}/{}", cwd, arg); } @@ -969,7 +972,8 @@ int Shell::builtin_wait(int argc, char const** argv) .name = "job-id", .min_values = 0, .max_values = INT_MAX, - .accept_value = [&](StringView value) -> bool { + .accept_value = [&](auto const* value_ptr) -> bool { + StringView value { value_ptr, strlen(value_ptr) }; // Check if it's a pid (i.e. literal integer) if (auto number = value.to_uint(); number.has_value()) { job_ids.append(number.value()); @@ -1071,14 +1075,14 @@ int Shell::builtin_kill(int argc, char const** argv) { // Simply translate the arguments and pass them to `kill' Vector replaced_values; - auto kill_path = find_in_path("kill"); + auto kill_path = find_in_path("kill"sv); if (kill_path.is_empty()) { warnln("kill: `kill' not found in PATH"); return 126; } replaced_values.append(kill_path); for (auto i = 1; i < argc; ++i) { - if (auto job_id = resolve_job_spec(argv[i]); job_id.has_value()) { + if (auto job_id = resolve_job_spec({ argv[i], strlen(argv[1]) }); job_id.has_value()) { auto job = find_job(job_id.value()); if (job) { replaced_values.append(String::number(job->pid())); @@ -1240,7 +1244,7 @@ int Shell::builtin_argsparser_parse(int argc, char const** argv) return false; } option.accept_value = [&, current_variable, treat_arg_as_list, type](auto value) { - auto result = try_convert(value, type); + auto result = try_convert({ value, strlen(value) }, type); if (result.has_value()) { auto value = result.release_value(); if (treat_arg_as_list) @@ -1262,7 +1266,7 @@ int Shell::builtin_argsparser_parse(int argc, char const** argv) return false; } arg.accept_value = [&, current_variable, treat_arg_as_list, type](auto value) { - auto result = try_convert(value, type); + auto result = try_convert({ value, strlen(value) }, type); if (result.has_value()) { auto value = result.release_value(); if (treat_arg_as_list) @@ -1345,7 +1349,7 @@ int Shell::builtin_argsparser_parse(int argc, char const** argv) return false; } - StringView ty = name; + StringView ty { name, strlen(name) }; if (ty == "bool") { if (auto option = current.get_pointer()) { if (option->value_name != nullptr) { @@ -1499,7 +1503,7 @@ int Shell::builtin_argsparser_parse(int argc, char const** argv) return false; } - auto number = StringView(value).to_uint(); + auto number = StringView { value, strlen(value) }.to_uint(); if (!number.has_value()) { warnln("Invalid value for --min: '{}', expected a non-negative number", value); return false; @@ -1527,7 +1531,7 @@ int Shell::builtin_argsparser_parse(int argc, char const** argv) return false; } - auto number = StringView(value).to_uint(); + auto number = StringView { value, strlen(value) }.to_uint(); if (!number.has_value()) { warnln("Invalid value for --max: '{}', expected a non-negative number", value); return false; diff --git a/Userland/Shell/Shell.cpp b/Userland/Shell/Shell.cpp index d1a39477a5002c..9f23e0cc93270f 100644 --- a/Userland/Shell/Shell.cpp +++ b/Userland/Shell/Shell.cpp @@ -110,7 +110,7 @@ String Shell::prompt() const builder.append(username); break; case 'h': - builder.append(hostname); + builder.append({ hostname, strlen(hostname) }); break; case 'w': { String home_path = getenv("HOME"); @@ -1607,7 +1607,7 @@ Vector Shell::complete_variable(StringView name, siz // Look at the environment. for (auto i = 0; environ[i]; ++i) { - auto entry = StringView { environ[i] }; + StringView entry { environ[i], strlen(environ[i]) }; if (entry.starts_with(pattern)) { auto parts = entry.split_view('='); if (parts.is_empty() || parts.first().is_empty()) @@ -2183,7 +2183,10 @@ Shell::Shell() // Add the default PATH vars. { StringBuilder path; - path.append(getenv("PATH")); + auto const* path_env_ptr = getenv("PATH"); + + if (path_env_ptr != NULL) + path.append({ path_env_ptr, strlen(path_env_ptr) }); if (path.length()) path.append(":"); path.append("/usr/local/sbin:/usr/local/bin:/usr/bin:/bin"); @@ -2476,7 +2479,8 @@ void Shell::timer_event(Core::TimerEvent& event) if (m_is_subshell) return; - StringView option = getenv("HISTORY_AUTOSAVE_TIME_MS"); + auto const* autosave_env_ptr = getenv("HISTORY_AUTOSAVE_TIME_MS"); + auto option = autosave_env_ptr != NULL ? StringView { autosave_env_ptr, strlen(autosave_env_ptr) } : StringView {}; auto time = option.to_uint(); if (!time.has_value() || time.value() == 0) { diff --git a/Userland/Utilities/du.cpp b/Userland/Utilities/du.cpp index 28229f6e55f315..68ab071a603ed2 100644 --- a/Userland/Utilities/du.cpp +++ b/Userland/Utilities/du.cpp @@ -65,12 +65,13 @@ ErrorOr parse_args(Main::Arguments arguments, Vector& files, DuOpt "time", 0, "time-type", - [&du_option](StringView s) { - if (s == "mtime"sv || s == "modification"sv) + [&du_option](auto const* option_ptr) { + StringView option { option_ptr, strlen(option_ptr) }; + if (option == "mtime"sv || option == "modification"sv) du_option.time_type = DuOption::TimeType::Modification; - else if (s == "ctime"sv || s == "status"sv || s == "use"sv) + else if (option == "ctime"sv || option == "status"sv || option == "use"sv) du_option.time_type = DuOption::TimeType::Status; - else if (s == "atime"sv || s == "access"sv) + else if (option == "atime"sv || option == "access"sv) du_option.time_type = DuOption::TimeType::Access; else return false; diff --git a/Userland/Utilities/expr.cpp b/Userland/Utilities/expr.cpp index 2ef9e4e2bae3ee..bb175db72b9bec 100644 --- a/Userland/Utilities/expr.cpp +++ b/Userland/Utilities/expr.cpp @@ -31,7 +31,7 @@ template [[noreturn]] void fail(Fmt&& fmt, Args&&... args) { warn("ERROR: \e[31m"); - warnln(StringView { fmt }, args...); + warnln(StringView { fmt, strlen(fmt) }, args...); warn("\e[0m"); exit(2); } diff --git a/Userland/Utilities/fgrep.cpp b/Userland/Utilities/fgrep.cpp index 76d55756d5e70b..41b2c470273f8e 100644 --- a/Userland/Utilities/fgrep.cpp +++ b/Userland/Utilities/fgrep.cpp @@ -19,7 +19,8 @@ ErrorOr serenity_main(Main::Arguments arguments) } for (;;) { char buffer[4096]; - auto str = StringView(fgets(buffer, sizeof(buffer), stdin)); + fgets(buffer, sizeof(buffer), stdin); + auto str = StringView { buffer, strlen(buffer) }; if (str.contains(arguments.strings[1])) TRY(Core::System::write(1, str.bytes())); if (feof(stdin)) diff --git a/Userland/Utilities/find.cpp b/Userland/Utilities/find.cpp index 5e4535922838a0..536de4f2a0260f 100644 --- a/Userland/Utilities/find.cpp +++ b/Userland/Utilities/find.cpp @@ -112,8 +112,8 @@ class TypeCommand final : public Command { public: TypeCommand(char const* arg) { - StringView type = arg; - if (type.length() != 1 || !StringView("bcdlpfs").contains(type[0])) + StringView type { arg, strlen(arg) }; + if (type.length() != 1 || !"bcdlpfs"sv.contains(type[0])) fatal_error("Invalid mode: \033[1m{}", arg); m_type = type[0]; } @@ -157,7 +157,7 @@ class LinksCommand final : public StatCommand { public: LinksCommand(char const* arg) { - auto number = StringView(arg).to_uint(); + auto number = StringView { arg, strlen(arg) }.to_uint(); if (!number.has_value()) fatal_error("Invalid number: \033[1m{}", arg); m_links = number.value(); @@ -180,7 +180,7 @@ class UserCommand final : public StatCommand { m_uid = passwd->pw_uid; } else { // Attempt to parse it as decimal UID. - auto number = StringView(arg).to_uint(); + auto number = StringView { arg, strlen(arg) }.to_uint(); if (!number.has_value()) fatal_error("Invalid user: \033[1m{}", arg); m_uid = number.value(); @@ -204,7 +204,7 @@ class GroupCommand final : public StatCommand { m_gid = gr->gr_gid; } else { // Attempt to parse it as decimal GID. - auto number = StringView(arg).to_int(); + auto number = StringView { arg, strlen(arg) }.to_int(); if (!number.has_value()) fatal_error("Invalid group: \033[1m{}", arg); m_gid = number.value(); @@ -224,7 +224,7 @@ class SizeCommand final : public StatCommand { public: SizeCommand(char const* arg) { - StringView view = arg; + StringView view { arg, strlen(arg) }; if (view.ends_with('c')) { m_is_bytes = true; view = view.substring_view(0, view.length() - 1); @@ -252,7 +252,7 @@ class SizeCommand final : public StatCommand { class NameCommand : public Command { public: NameCommand(char const* pattern, CaseSensitivity case_sensitivity) - : m_pattern(pattern) + : m_pattern(pattern, strlen(pattern)) , m_case_sensitivity(case_sensitivity) { } @@ -306,7 +306,7 @@ class ExecCommand final : public Command { // constness. auto argv = const_cast&>(m_argv); for (auto& arg : argv) { - if (StringView(arg) == "{}") + if (StringView { arg, strlen(arg) } == "{}") arg = const_cast(file_data.full_path.string().characters()); } argv.append(nullptr); @@ -374,11 +374,11 @@ static OwnPtr parse_simple_command(Vector& args) return {}; char* raw_arg = args.take_first(); - StringView arg = raw_arg; + StringView arg { raw_arg, strlen(raw_arg) }; if (arg == "(") { auto command = parse_complex_command(args); - if (command && !args.is_empty() && StringView(args.first()) == ")") + if (command && !args.is_empty() && StringView { args.first(), strlen(args.first()) } == ")") return command; fatal_error("Unmatched \033[1m("); } else if (arg == "-type") { @@ -438,7 +438,7 @@ static OwnPtr parse_complex_command(Vector& args) while (command && !args.is_empty()) { char* raw_arg = args.take_first(); - StringView arg = raw_arg; + StringView arg { raw_arg, strlen(raw_arg) }; enum { And, @@ -533,7 +533,7 @@ static void walk_tree(FileData& root_data, Command& command) continue; FileData file_data { - root_data.full_path.append(dirent->d_name), + root_data.full_path.append({ dirent->d_name, strlen(dirent->d_name) }), dirfd, dirent->d_name, (struct stat) {}, @@ -561,7 +561,7 @@ ErrorOr serenity_main(Main::Arguments arguments) while (!args.is_empty()) { char* raw_arg = args.take_first(); - StringView arg = raw_arg; + StringView arg { raw_arg, strlen(raw_arg) }; if (arg == "-L") { g_follow_symlinks = true; } else if (!arg.starts_with('-')) { diff --git a/Userland/Utilities/groupdel.cpp b/Userland/Utilities/groupdel.cpp index 688d1eddda44ac..9021fd64c2ffb4 100644 --- a/Userland/Utilities/groupdel.cpp +++ b/Userland/Utilities/groupdel.cpp @@ -58,9 +58,10 @@ ErrorOr serenity_main(Main::Arguments arguments) // Create a temporary group file char temp_group[] = "/etc/group.XXXXXX"; + StringView temp_group_view { temp_group, strlen(temp_group) }; auto unlink_temp_files = [&] { - if (Core::System::unlink(temp_group).is_error()) + if (Core::System::unlink(temp_group_view).is_error()) perror("unlink"); }; @@ -92,8 +93,8 @@ ErrorOr serenity_main(Main::Arguments arguments) return 1; } - TRY(Core::System::chmod(temp_group, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); - TRY(Core::System::rename(temp_group, "/etc/group")); + TRY(Core::System::chmod(temp_group_view, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); + TRY(Core::System::rename(temp_group_view, "/etc/group"sv)); unlink_temp_files_guard.disarm(); diff --git a/Userland/Utilities/ls.cpp b/Userland/Utilities/ls.cpp index d26c4bc695591e..1dc537dc135a86 100644 --- a/Userland/Utilities/ls.cpp +++ b/Userland/Utilities/ls.cpp @@ -416,7 +416,7 @@ static int do_file_system_object_long(char const* path) continue; StringBuilder builder; - builder.append(path); + builder.append({ path, strlen(path) }); builder.append('/'); builder.append(metadata.name); metadata.path = builder.to_string(); @@ -460,7 +460,7 @@ static bool print_names(char const* path, size_t longest_name, Vector serenity_main(Main::Arguments arguments) if (target_directory.is_empty()) { if (!file_template.is_empty()) { // If a custom template is specified we assume the target directory is the current directory - target_directory = getcwd(nullptr, 0); + // FIXME: Get rid of this minor memory leak. + auto const* cwd_ptr = getcwd(nullptr, 0); + target_directory = StringView { cwd_ptr, strlen(cwd_ptr) }; } else { LexicalPath template_path(file_template); char const* env_directory = getenv("TMPDIR"); - target_directory = env_directory && *env_directory ? env_directory : "/tmp"; + target_directory = env_directory && *env_directory ? StringView { env_directory, strlen(env_directory) } : "/tmp"sv; } } diff --git a/Userland/Utilities/netstat.cpp b/Userland/Utilities/netstat.cpp index 0ae7af7dd8b220..80c0ac318217a7 100644 --- a/Userland/Utilities/netstat.cpp +++ b/Userland/Utilities/netstat.cpp @@ -185,7 +185,7 @@ ErrorOr serenity_main(Main::Arguments arguments) auto addr = from_string.value().to_in_addr_t(); auto* hostent = gethostbyaddr(&addr, sizeof(in_addr), AF_INET); if (hostent != nullptr) { - auto host_name = StringView(hostent->h_name); + auto host_name = StringView { hostent->h_name, strlen(hostent->h_name) }; if (!host_name.is_empty()) peer_address = host_name; } @@ -195,7 +195,7 @@ ErrorOr serenity_main(Main::Arguments arguments) if (!flag_numeric) { auto service = getservbyport(htons(if_object.get("peer_port").to_u32()), "tcp"); if (service != nullptr) { - auto s_name = StringView(service->s_name); + auto s_name = StringView { service->s_name, strlen(service->s_name) }; if (!s_name.is_empty()) peer_port = s_name; } @@ -207,7 +207,7 @@ ErrorOr serenity_main(Main::Arguments arguments) auto addr = from_string.value().to_in_addr_t(); auto* hostent = gethostbyaddr(&addr, sizeof(in_addr), AF_INET); if (hostent != nullptr) { - auto host_name = StringView(hostent->h_name); + auto host_name = StringView { hostent->h_name, strlen(hostent->h_name) }; if (!host_name.is_empty()) local_address = host_name; } @@ -217,7 +217,7 @@ ErrorOr serenity_main(Main::Arguments arguments) if (!flag_numeric) { auto service = getservbyport(htons(if_object.get("local_port").to_u32()), "tcp"); if (service != nullptr) { - auto s_name = StringView(service->s_name); + auto s_name = StringView { service->s_name, strlen(service->s_name) }; if (!s_name.is_empty()) local_port = s_name; } @@ -269,7 +269,7 @@ ErrorOr serenity_main(Main::Arguments arguments) auto addr = from_string.value().to_in_addr_t(); auto* hostent = gethostbyaddr(&addr, sizeof(in_addr), AF_INET); if (hostent != nullptr) { - auto host_name = StringView(hostent->h_name); + auto host_name = StringView { hostent->h_name, strlen(hostent->h_name) }; if (!host_name.is_empty()) local_address = host_name; } @@ -279,7 +279,7 @@ ErrorOr serenity_main(Main::Arguments arguments) if (!flag_numeric) { auto service = getservbyport(htons(if_object.get("local_port").to_u32()), "udp"); if (service != nullptr) { - auto s_name = StringView(service->s_name); + auto s_name = StringView { service->s_name, strlen(service->s_name) }; if (!s_name.is_empty()) local_port = s_name; } @@ -291,7 +291,7 @@ ErrorOr serenity_main(Main::Arguments arguments) auto addr = from_string.value().to_in_addr_t(); auto* hostent = gethostbyaddr(&addr, sizeof(in_addr), AF_INET); if (hostent != nullptr) { - auto host_name = StringView(hostent->h_name); + auto host_name = StringView { hostent->h_name, strlen(hostent->h_name) }; if (!host_name.is_empty()) peer_address = host_name; } @@ -301,7 +301,7 @@ ErrorOr serenity_main(Main::Arguments arguments) if (!flag_numeric) { auto service = getservbyport(htons(if_object.get("peer_port").to_u32()), "udp"); if (service != nullptr) { - auto s_name = StringView(service->s_name); + auto s_name = StringView { service->s_name, strlen(service->s_name) }; if (!s_name.is_empty()) peer_port = s_name; } diff --git a/Userland/Utilities/paste.cpp b/Userland/Utilities/paste.cpp index 20e9db0b2db1b0..9880a4c4e3d1a2 100644 --- a/Userland/Utilities/paste.cpp +++ b/Userland/Utilities/paste.cpp @@ -27,7 +27,7 @@ static void spawn_command(Span command, ByteBuffer const& data, char MUST(Core::System::dup2(pipefd[0], 0)); MUST(Core::System::close(pipefd[0])); MUST(Core::System::close(pipefd[1])); - MUST(Core::System::setenv("CLIPBOARD_STATE", state, true)); + MUST(Core::System::setenv("CLIPBOARD_STATE"sv, { state, strlen(state) }, true)); MUST(Core::System::exec(command[0], command, Core::System::SearchInPath::Yes)); perror("exec"); exit(1); diff --git a/Userland/Utilities/pidof.cpp b/Userland/Utilities/pidof.cpp index b4c83d39e99099..f7db363cdbffbe 100644 --- a/Userland/Utilities/pidof.cpp +++ b/Userland/Utilities/pidof.cpp @@ -63,7 +63,7 @@ ErrorOr serenity_main(Main::Arguments args) if (!strcmp(omit_pid_value, "%PPID")) { pid_to_omit = getppid(); } else { - auto number = StringView(omit_pid_value).to_uint(); + auto number = StringView { omit_pid_value, strlen(omit_pid_value) }.to_uint(); if (!number.has_value()) { warnln("Invalid value for -o"); args_parser.print_usage(stderr, args.argv[0]); diff --git a/Userland/Utilities/pls.cpp b/Userland/Utilities/pls.cpp index b11456113d6c9f..c27991a1f464e4 100644 --- a/Userland/Utilities/pls.cpp +++ b/Userland/Utilities/pls.cpp @@ -49,7 +49,7 @@ ErrorOr serenity_main(Main::Arguments arguments) Vector exec_environment; for (size_t i = 0; environ[i]; ++i) { - StringView env_view { environ[i] }; + StringView env_view { environ[i], strlen(environ[i]) }; auto maybe_needle = env_view.find('='); if (!maybe_needle.has_value()) diff --git a/Userland/Utilities/pro.cpp b/Userland/Utilities/pro.cpp index 775eafa585b509..148cacd8a66dfe 100644 --- a/Userland/Utilities/pro.cpp +++ b/Userland/Utilities/pro.cpp @@ -170,7 +170,7 @@ ErrorOr serenity_main(Main::Arguments arguments) .short_name = 'H', .value_name = "header-value", .accept_value = [&](auto* s) { - StringView header { s }; + StringView header { s, strlen(s) }; auto split = header.find(':'); if (!split.has_value()) return false; @@ -316,7 +316,7 @@ ErrorOr serenity_main(Main::Arguments arguments) request->stream_into(output_stream); }; - request = protocol_client->start_request(method, url, request_headers, data ? StringView { data }.bytes() : ReadonlyBytes {}, proxy_data); + request = protocol_client->start_request(method, url, request_headers, data ? StringView { data, strlen(data) }.bytes() : ReadonlyBytes {}, proxy_data); setup_request(); dbgln("started request with id {}", request->id()); diff --git a/Userland/Utilities/strace.cpp b/Userland/Utilities/strace.cpp index f39a5239cb7458..37e9f4645d13dd 100644 --- a/Userland/Utilities/strace.cpp +++ b/Userland/Utilities/strace.cpp @@ -836,7 +836,7 @@ ErrorOr serenity_main(Main::Arguments arguments) auto parse_syscalls = [](char const* option, auto& hash_table) { if (option != nullptr) { - for (auto syscall : StringView(option).split_view(',')) + for (auto syscall : StringView { option, strlen(option) }.split_view(',')) hash_table.set(syscall); } }; diff --git a/Userland/Utilities/stty.cpp b/Userland/Utilities/stty.cpp index a88a84b8c25679..5d09a7211f2813 100644 --- a/Userland/Utilities/stty.cpp +++ b/Userland/Utilities/stty.cpp @@ -295,7 +295,7 @@ Result apply_modes(size_t parameter_count, char** raw_parameters, ter Vector parameters; parameters.ensure_capacity(parameter_count); for (size_t i = 0; i < parameter_count; ++i) - parameters.append(StringView(raw_parameters[i])); + parameters.append(StringView { raw_parameters[i], strlen(raw_parameters[i]) }); auto parse_baud = [&](size_t idx) -> Optional { auto maybe_numeric_value = parameters[idx].to_uint(); diff --git a/Userland/Utilities/test-unveil.cpp b/Userland/Utilities/test-unveil.cpp index 2acfd6e4ec329a..4889d3d062209c 100644 --- a/Userland/Utilities/test-unveil.cpp +++ b/Userland/Utilities/test-unveil.cpp @@ -26,7 +26,7 @@ ErrorOr serenity_main(Main::Arguments arguments) .short_name = 'u', .value_name = "path", .accept_value = [&](auto* s) { - StringView path { s }; + StringView path { s, strlen(s) }; if (path.is_empty()) return false; auto maybe_error = Core::System::unveil(path, permissions); @@ -55,7 +55,7 @@ ErrorOr serenity_main(Main::Arguments arguments) .min_values = 0, .max_values = INT_MAX, .accept_value = [&](auto* s) { - auto maybe_error = Core::System::access(s, X_OK); + auto maybe_error = Core::System::access({ s, strlen(s) }, X_OK); if (maybe_error.is_error()) warnln("'{}' - fail: {}", s, maybe_error.error()); else diff --git a/Userland/Utilities/test.cpp b/Userland/Utilities/test.cpp index 399d19027b3213..2a5a712768d35b 100644 --- a/Userland/Utilities/test.cpp +++ b/Userland/Utilities/test.cpp @@ -324,7 +324,7 @@ static bool should_treat_expression_as_single_string(StringView arg_after) static OwnPtr parse_simple_expression(char* argv[]) { - StringView arg = argv[optind]; + StringView arg { argv[optind], strlen(argv[optind]) }; if (arg.is_null()) { return {}; } @@ -332,20 +332,24 @@ static OwnPtr parse_simple_expression(char* argv[]) if (arg == "(") { optind++; auto command = parse_complex_expression(argv); - if (command && argv[optind] && StringView(argv[++optind]) == ")") - return command; + if (command && argv[optind]) { + auto const* next_option = argv[++optind]; + if (StringView { next_option, strlen(next_option) } == ")") + return command; + } + fatal_error("Unmatched \033[1m("); } // Try to read a unary op. if (arg.starts_with('-') && arg.length() == 2) { optind++; - if (should_treat_expression_as_single_string(argv[optind])) { + if (should_treat_expression_as_single_string({ argv[optind], strlen(argv[optind]) })) { --optind; return make(move(arg), "", StringCompare::NotEqual); } - StringView value = argv[optind]; + StringView value { argv[optind], strlen(argv[optind]) }; switch (arg[1]) { case 'b': return make(value, FileIsOfKind::BlockDevice); @@ -393,42 +397,49 @@ static OwnPtr parse_simple_expression(char* argv[]) } } + auto get_next_arg = [&argv]() -> StringView { + auto const* next_arg = argv[++optind]; + if (next_arg == NULL) + return StringView {}; + return StringView { next_arg, strlen(next_arg) }; + }; + // Try to read a binary op, this is either a op , op , or op . auto lhs = arg; - arg = argv[++optind]; + arg = get_next_arg(); if (arg == "=") { - StringView rhs = argv[++optind]; + StringView rhs = get_next_arg(); return make(lhs, rhs, StringCompare::Equal); } else if (arg == "!=") { - StringView rhs = argv[++optind]; + StringView rhs = get_next_arg(); return make(lhs, rhs, StringCompare::NotEqual); } else if (arg == "-eq") { - StringView rhs = argv[++optind]; + StringView rhs = get_next_arg(); return make(lhs, rhs, NumericCompare::Equal); } else if (arg == "-ge") { - StringView rhs = argv[++optind]; + StringView rhs = get_next_arg(); return make(lhs, rhs, NumericCompare::GreaterOrEqual); } else if (arg == "-gt") { - StringView rhs = argv[++optind]; + StringView rhs = get_next_arg(); return make(lhs, rhs, NumericCompare::Greater); } else if (arg == "-le") { - StringView rhs = argv[++optind]; + StringView rhs = get_next_arg(); return make(lhs, rhs, NumericCompare::LessOrEqual); } else if (arg == "-lt") { - StringView rhs = argv[++optind]; + StringView rhs = get_next_arg(); return make(lhs, rhs, NumericCompare::Less); } else if (arg == "-ne") { - StringView rhs = argv[++optind]; + StringView rhs = get_next_arg(); return make(lhs, rhs, NumericCompare::NotEqual); } else if (arg == "-ef") { - StringView rhs = argv[++optind]; + StringView rhs = get_next_arg(); return make(lhs, rhs, FileCompare::Same); } else if (arg == "-nt") { - StringView rhs = argv[++optind]; + StringView rhs = get_next_arg(); return make(lhs, rhs, FileCompare::ModificationTimestampGreater); } else if (arg == "-ot") { - StringView rhs = argv[++optind]; + StringView rhs = get_next_arg(); return make(lhs, rhs, FileCompare::ModificationTimestampLess); } else if (arg == "-o" || arg == "-a") { // '-a' and '-o' are boolean ops, which are part of a complex expression @@ -460,7 +471,8 @@ static OwnPtr parse_complex_expression(char* argv[]) if (!command && argv[optind]) fatal_error("expected an expression"); - StringView arg = argv[++optind]; + auto const* arg_ptr = argv[++optind]; + StringView arg { arg_ptr, strlen(arg_ptr) }; enum { AndOp, diff --git a/Userland/Utilities/top.cpp b/Userland/Utilities/top.cpp index 1b43ae362d7aa6..b85616dab32bb3 100644 --- a/Userland/Utilities/top.cpp +++ b/Userland/Utilities/top.cpp @@ -144,7 +144,7 @@ static void parse_args(Main::Arguments arguments, TopOption& top_option) 's', nullptr, [&top_option](char const* s) { - StringView sort_by_option { s }; + StringView sort_by_option { s, strlen(s) }; if (sort_by_option == "pid"sv) top_option.sort_by = TopOption::SortBy::Pid; else if (sort_by_option == "tid"sv) diff --git a/Userland/Utilities/usermod.cpp b/Userland/Utilities/usermod.cpp index 17cc041c73add6..1300a32af68581 100644 --- a/Userland/Utilities/usermod.cpp +++ b/Userland/Utilities/usermod.cpp @@ -62,7 +62,7 @@ ErrorOr serenity_main(Main::Arguments arguments) if (move_home) { TRY(Core::System::unveil(target_account.home_directory().characters(), "c")); - TRY(Core::System::unveil(new_home_directory, "wc")); + TRY(Core::System::unveil({ new_home_directory, strlen(new_home_directory) }, "wc")); } unveil(nullptr, nullptr); diff --git a/Userland/Utilities/wasm.cpp b/Userland/Utilities/wasm.cpp index fc8106700bd1fd..236eba24d9b29c 100644 --- a/Userland/Utilities/wasm.cpp +++ b/Userland/Utilities/wasm.cpp @@ -294,7 +294,7 @@ ErrorOr serenity_main(Main::Arguments arguments) .short_name = 'l', .value_name = "file", .accept_value = [&](char const* str) { - if (auto v = StringView { str }; !v.is_empty()) { + if (auto v = StringView { str, strlen(str) }; !v.is_empty()) { modules_to_link_in.append(v); return true; } @@ -308,7 +308,7 @@ ErrorOr serenity_main(Main::Arguments arguments) .short_name = 0, .value_name = "u64", .accept_value = [&](char const* str) -> bool { - if (auto v = StringView { str }.to_uint(); v.has_value()) { + if (auto v = StringView { str, strlen(str) }.to_uint(); v.has_value()) { values_to_push.append(v.value()); return true; } diff --git a/Userland/Utilities/xargs.cpp b/Userland/Utilities/xargs.cpp index c04932ee792d27..e258d13fe643bb 100644 --- a/Userland/Utilities/xargs.cpp +++ b/Userland/Utilities/xargs.cpp @@ -178,7 +178,7 @@ bool read_items(FILE* fp, char entry_separator, Function c Decision decision; do { - decision = callback(item); + decision = callback({ item, strlen(item) }); if (decision == Stop) { free(item); return true;