From 797ff8d3079c718bfdd5ee33789fcb0a76c7a259 Mon Sep 17 00:00:00 2001 From: cdesouza-chromium Date: Mon, 14 Apr 2025 22:26:35 +0100 Subject: [PATCH] [unsafe-buffers] Remove all `#pragma allow_unsafe_buffers` (#28637) This change migrates all uses of `#pragma allow_unsafe_buffers` to use `UNSAFE_TODO`. This makes the offending places more visible, as well as making explicit the introduction of new spots. Resolves https://github.com/brave/brave-browser/issues/45414 --- browser/default_protocol_handler_utils_win.cc | 12 ++-- browser/ui/webui/brave_settings_ui.cc | 15 ++--- .../brave_install_modes_unittest.cc | 14 ++-- .../brave_install_util_unittest.cc | 27 ++++---- .../brave_product_install_details_unittest.cc | 10 +-- .../mini_installer/mini_installer.cc | 67 ++++++++++--------- .../chrome/installer/setup/uninstall.cc | 16 ++--- .../sandbox/win/src/interceptors_64.cc | 7 +- .../core/browser/text_embedder_unittest.cc | 20 +++--- .../renderer/page_content_extractor.cc | 7 +- .../reward/redeem_reward_confirmation.cc | 7 +- .../common/calendar/calendar_util_unittest.cc | 13 ++-- .../brave_news/browser/feed_v2_builder.cc | 7 +- .../private_cdn_helper_unittest.cc | 12 ++-- .../brave_rewards/content/diagnostic_log.cc | 22 +++--- .../content/browser/brave_shields_p3a.cc | 16 ++--- components/brave_sync/time_limited_words.cc | 13 ++-- .../browser/connection/ikev2/win/ras_utils.cc | 17 ++--- .../common/wireguard/wireguard_utils.cc | 11 +-- .../browser/brave_search_provider_unittest.cc | 7 +- components/p3a/nitro_utils/attestation.cc | 12 ++-- components/p3a/nitro_utils/cose.cc | 16 ++--- .../permissions_client_unittest.cc | 17 ++--- components/tor/tor_control.cc | 21 +++--- components/tor/tor_control_unittest.cc | 41 ++++++------ components/tor/tor_file_watcher.cc | 15 ++--- ios/browser/api/net/certificate_utility.mm | 12 ++-- .../api/storekit_receipt/storekit_receipt.mm | 15 ++--- .../win/src/module_file_name_interception.cc | 12 ++-- .../blink/renderer/brave_font_whitelist.cc | 6 +- .../core/farbling/brave_session_cache.cc | 9 +-- .../platform/brave_audio_farbling_helper.cc | 36 +++++----- 32 files changed, 205 insertions(+), 327 deletions(-) diff --git a/browser/default_protocol_handler_utils_win.cc b/browser/default_protocol_handler_utils_win.cc index 5645fc54570..44d464f6289 100644 --- a/browser/default_protocol_handler_utils_win.cc +++ b/browser/default_protocol_handler_utils_win.cc @@ -3,17 +3,11 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/browser/default_protocol_handler_utils_win.h" #include - #include + #include #include @@ -22,6 +16,7 @@ #include #include "base/base64.h" +#include "base/compiler_specific.h" #include "base/containers/span.h" #include "base/containers/span_reader.h" #include "base/files/file_path.h" @@ -66,7 +61,8 @@ std::wstring HashString(base::wcstring_view input) { // there's nothing for us to do with small strings. return std::wstring(); } - auto bytes = base::as_bytes(base::span(input.data(), input.size() + 1)); + auto bytes = + base::as_bytes(UNSAFE_TODO(base::span(input.data(), input.size() + 1))); // Compute an MD5 hash. md5[0] and md5[1] will be used as constant multipliers // in the scramble below. diff --git a/browser/ui/webui/brave_settings_ui.cc b/browser/ui/webui/brave_settings_ui.cc index 37fe1645d96..00c50931786 100644 --- a/browser/ui/webui/brave_settings_ui.cc +++ b/browser/ui/webui/brave_settings_ui.cc @@ -3,18 +3,13 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/browser/ui/webui/brave_settings_ui.h" #include #include #include +#include "base/compiler_specific.h" #include "base/feature_list.h" #include "brave/browser/brave_rewards/rewards_util.h" #include "brave/browser/brave_wallet/brave_wallet_context_utils.h" @@ -128,8 +123,8 @@ BraveSettingsUI::~BraveSettingsUI() = default; void BraveSettingsUI::AddResources(content::WebUIDataSource* html_source, Profile* profile) { for (size_t i = 0; i < kBraveSettingsResourcesSize; ++i) { - html_source->AddResourcePath(kBraveSettingsResources[i].path, - kBraveSettingsResources[i].id); + html_source->AddResourcePath(UNSAFE_TODO(kBraveSettingsResources[i]).path, + UNSAFE_TODO(kBraveSettingsResources[i]).id); } // These resource files are generated from the files in @@ -139,8 +134,8 @@ void BraveSettingsUI::AddResources(content::WebUIDataSource* html_source, // doesn't interfere with the Polymer tree/styles. if (base::FeatureList::IsEnabled(commands::features::kBraveCommands)) { for (size_t i = 0; i < kCommandsGeneratedSize; ++i) { - html_source->AddResourcePath(kCommandsGenerated[i].path, - kCommandsGenerated[i].id); + html_source->AddResourcePath(UNSAFE_TODO(kCommandsGenerated[i]).path, + UNSAFE_TODO(kCommandsGenerated[i]).id); } } diff --git a/chromium_src/chrome/install_static/brave_install_modes_unittest.cc b/chromium_src/chrome/install_static/brave_install_modes_unittest.cc index 21fbf600caa..18c37214382 100644 --- a/chromium_src/chrome/install_static/brave_install_modes_unittest.cc +++ b/chromium_src/chrome/install_static/brave_install_modes_unittest.cc @@ -3,19 +3,13 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - -#include "chrome/install_static/install_modes.h" - #include // NOLINT #include // NOLINT +#include "base/compiler_specific.h" #include "base/strings/string_util.h" +#include "chrome/install_static/install_modes.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -38,7 +32,7 @@ namespace { MATCHER(ContainsIllegalProgIdChar, "") { const wchar_t* scan = arg; wchar_t c; - while ((c = *scan++) != 0) { + while ((c = UNSAFE_TODO(*scan++)) != 0) { if (!base::IsAsciiAlphaNumeric(c) && c != L'.') { return true; } @@ -51,7 +45,7 @@ MATCHER(ContainsIllegalProgIdChar, "") { TEST(InstallModes, VerifyModes) { ASSERT_THAT(NUM_INSTALL_MODES, Gt(0)); for (int i = 0; i < NUM_INSTALL_MODES; ++i) { - const InstallConstants& mode = kInstallModes[i]; + const InstallConstants& mode = UNSAFE_TODO(kInstallModes[i]); // The modes must be listed in order. ASSERT_THAT(mode.index, Eq(i)); diff --git a/chromium_src/chrome/install_static/brave_install_util_unittest.cc b/chromium_src/chrome/install_static/brave_install_util_unittest.cc index 3a018ef9a1c..1be5f5d1cfc 100644 --- a/chromium_src/chrome/install_static/brave_install_util_unittest.cc +++ b/chromium_src/chrome/install_static/brave_install_util_unittest.cc @@ -3,16 +3,11 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include #include +#include "base/compiler_specific.h" #include "base/stl_util.h" #include "base/test/test_reg_util_win.h" #include "chrome/chrome_elf/nt_registry/nt_registry.h" @@ -357,7 +352,7 @@ TEST_P(InstallStaticUtilTest, GetChromeInstallSubDirectory) { static_assert(std::size(kInstallDirs) == NUM_INSTALL_MODES, "kInstallDirs out of date."); EXPECT_THAT(GetChromeInstallSubDirectory(), - StrCaseEq(kInstallDirs[std::get<0>(GetParam())])); + StrCaseEq(UNSAFE_TODO(kInstallDirs[std::get<0>(GetParam())]))); } TEST_P(InstallStaticUtilTest, GetRegistryPath) { @@ -380,7 +375,7 @@ TEST_P(InstallStaticUtilTest, GetRegistryPath) { static_assert(std::size(kRegistryPaths) == NUM_INSTALL_MODES, "kRegistryPaths out of date."); EXPECT_THAT(GetRegistryPath(), - StrCaseEq(kRegistryPaths[std::get<0>(GetParam())])); + StrCaseEq(UNSAFE_TODO(kRegistryPaths[std::get<0>(GetParam())]))); } TEST_P(InstallStaticUtilTest, GetUninstallRegistryPath) { @@ -407,8 +402,9 @@ TEST_P(InstallStaticUtilTest, GetUninstallRegistryPath) { #endif static_assert(std::size(kUninstallRegistryPaths) == NUM_INSTALL_MODES, "kUninstallRegistryPaths out of date."); - EXPECT_THAT(GetUninstallRegistryPath(), - StrCaseEq(kUninstallRegistryPaths[std::get<0>(GetParam())])); + EXPECT_THAT( + GetUninstallRegistryPath(), + StrCaseEq(UNSAFE_TODO(kUninstallRegistryPaths[std::get<0>(GetParam())]))); } TEST_P(InstallStaticUtilTest, GetAppGuid) { @@ -444,7 +440,8 @@ TEST_P(InstallStaticUtilTest, GetBaseAppId) { #endif static_assert(std::size(kBaseAppIds) == NUM_INSTALL_MODES, "kBaseAppIds out of date."); - EXPECT_THAT(GetBaseAppId(), StrCaseEq(kBaseAppIds[std::get<0>(GetParam())])); + EXPECT_THAT(GetBaseAppId(), + StrCaseEq(UNSAFE_TODO(kBaseAppIds[std::get<0>(GetParam())]))); } TEST_P(InstallStaticUtilTest, GetToastActivatorClsid) { @@ -501,14 +498,15 @@ TEST_P(InstallStaticUtilTest, GetToastActivatorClsid) { "kToastActivatorClsids out of date."); EXPECT_EQ(GetToastActivatorClsid(), - kToastActivatorClsids[std::get<0>(GetParam())]); + UNSAFE_TODO(kToastActivatorClsids[std::get<0>(GetParam())])); constexpr int kCLSIDSize = 39; wchar_t clsid_str[kCLSIDSize]; ASSERT_EQ(::StringFromGUID2(GetToastActivatorClsid(), clsid_str, kCLSIDSize), kCLSIDSize); EXPECT_THAT(clsid_str, - StrCaseEq(kToastActivatorClsidsString[std::get<0>(GetParam())])); + StrCaseEq(UNSAFE_TODO( + kToastActivatorClsidsString[std::get<0>(GetParam())]))); } TEST_P(InstallStaticUtilTest, UsageStatsAbsent) { @@ -590,7 +588,8 @@ TEST_P(InstallStaticUtilTest, GetChromeChannel) { version_info::Channel::UNKNOWN, }; #endif - EXPECT_EQ(kChannels[std::get<0>(GetParam())], GetChromeChannel()); + EXPECT_EQ(UNSAFE_TODO(kChannels[std::get<0>(GetParam())]), + GetChromeChannel()); } #if defined(OFFICIAL_BUILD) diff --git a/chromium_src/chrome/install_static/brave_product_install_details_unittest.cc b/chromium_src/chrome/install_static/brave_product_install_details_unittest.cc index f7af5d1b118..765c5b00b2c 100644 --- a/chromium_src/chrome/install_static/brave_product_install_details_unittest.cc +++ b/chromium_src/chrome/install_static/brave_product_install_details_unittest.cc @@ -3,15 +3,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - -#include "chrome/install_static/product_install_details.h" - #include "base/base_paths.h" +#include "base/compiler_specific.h" #include "base/files/file_path.h" #include "base/i18n/case_conversion.h" #include "base/path_service.h" @@ -23,6 +16,7 @@ #include "chrome/install_static/install_constants.h" #include "chrome/install_static/install_details.h" #include "chrome/install_static/install_modes.h" +#include "chrome/install_static/product_install_details.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" diff --git a/chromium_src/chrome/installer/mini_installer/mini_installer.cc b/chromium_src/chrome/installer/mini_installer/mini_installer.cc index e13422f87dc..df3aae40b5f 100644 --- a/chromium_src/chrome/installer/mini_installer/mini_installer.cc +++ b/chromium_src/chrome/installer/mini_installer/mini_installer.cc @@ -3,12 +3,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - +#include "base/compiler_specific.h" #include "build/branding_buildflags.h" #define BRAVE_RUN_SETUP \ @@ -52,23 +47,24 @@ void SafeStrASCIIUpper(wchar_t* str, size_t size) { if (!str || !size) return; - for (size_t i = 0; i < size && str[i] != L'\0'; ++i) { - wchar_t c = str[i]; + for (size_t i = 0; i < size && UNSAFE_TODO(str[i]) != L'\0'; ++i) { + wchar_t c = UNSAFE_TODO(str[i]); if (c >= L'a' && c <= L'z') - str[i] += L'A' - L'a'; + UNSAFE_TODO(str[i]) += L'A' - L'a'; } } bool ParseStandardReferralCode(const wchar_t* filename, ReferralCodeString* referral_code) { // Scan backwards for last dash in filename. - const wchar_t* anchor = filename + lstrlen(filename) - 1; + const wchar_t* anchor = UNSAFE_TODO(filename + lstrlen(filename) - 1); const wchar_t* scan = anchor; while (scan != filename && *scan != L'-') - --scan; + UNSAFE_TODO(--scan); - if (*scan++ != L'-') + if (*UNSAFE_TODO(scan++) != L'-') { return false; + } if (anchor - scan + 1 != kStandardReferralCodeLen) return false; @@ -78,16 +74,18 @@ bool ParseStandardReferralCode(const wchar_t* filename, // Ensure that first half of referral code is alphabetic. for (size_t i = 0; i < kStandardReferralCodeLen / 2; ++i) { - if ((ref_code[i] < L'a' || ref_code[i] > L'z') && - (ref_code[i] < L'A' || ref_code[i] > L'Z')) + if ((UNSAFE_TODO(ref_code[i]) < L'a' || UNSAFE_TODO(ref_code[i]) > L'z') && + (UNSAFE_TODO(ref_code[i]) < L'A' || UNSAFE_TODO(ref_code[i]) > L'Z')) { return false; + } } // Ensure that second half of referral code is numeric. for (size_t i = kStandardReferralCodeLen / 2; i < kStandardReferralCodeLen; ++i) { - if (ref_code[i] < L'0' || ref_code[i] > L'9') + if (UNSAFE_TODO(ref_code[i]) < L'0' || UNSAFE_TODO(ref_code[i]) > L'9') { return false; + } } if (!SafeStrCopy(ref_code_normalized, kStandardReferralCodeLen + 1, ref_code)) @@ -105,28 +103,33 @@ bool ParseExtendedReferralCode(const wchar_t* filename, ReferralCodeString* referral_code) { // Scan backwards for second-to-last dash in filename, since this // type of referral code has an embedded dash. - const wchar_t* scan = filename + lstrlen(filename) - 1; + const wchar_t* scan = UNSAFE_TODO(filename + lstrlen(filename) - 1); while (scan != filename && *scan != L'-') - --scan; + UNSAFE_TODO(--scan); - if (*scan-- != L'-') + if (*UNSAFE_TODO(scan--) != L'-') { return false; + } while (scan != filename && *scan != L'-') - --scan; + UNSAFE_TODO(--scan); - if (*scan++ != L'-') + if (*UNSAFE_TODO(scan++) != L'-') { return false; + } // Ensure that referral code is alphabetic. const wchar_t* ref_code = scan; int dashes = 0; for (int i = 0; i < lstrlen(ref_code); ++i) { - if ((ref_code[i] < L'a' || ref_code[i] > L'z') && - (ref_code[i] < L'A' || ref_code[i] > L'Z') && (ref_code[i] != L'-')) + if ((UNSAFE_TODO(ref_code[i]) < L'a' || UNSAFE_TODO(ref_code[i]) > L'z') && + (UNSAFE_TODO(ref_code[i]) < L'A' || UNSAFE_TODO(ref_code[i]) > L'Z') && + (UNSAFE_TODO(ref_code[i]) != L'-')) { return false; - if (ref_code[i] == L'-') + } + if (UNSAFE_TODO(ref_code[i]) == L'-') { ++dashes; + } } // Ensure that referral code contains exactly one dash. @@ -147,30 +150,32 @@ bool ParseReferralCode(const wchar_t* installer_filename, return false; // Strip extension from filename. - const wchar_t* scan = filename.get() + filename.length() - 1; + const wchar_t* scan = UNSAFE_TODO(filename.get() + filename.length() - 1); while (scan != filename.get() && *scan != L'.') - --scan; + UNSAFE_TODO(--scan); if (*scan == L'.') filename.truncate_at(scan - filename.get()); // Strip any de-duplicating suffix from filename, e.g. "(1)". - scan = filename.get() + filename.length() - 1; + scan = UNSAFE_TODO(filename.get() + filename.length() - 1); if (*scan == L')') { - --scan; + UNSAFE_TODO(--scan); while (scan != filename.get() && *scan >= '0' && *scan <= '9') - --scan; + UNSAFE_TODO(--scan); if (*scan == L'(') filename.truncate_at(scan - filename.get()); } // Strip trailing spaces from filename. - scan = filename.get() + filename.length() - 1; + scan = UNSAFE_TODO(filename.get() + filename.length() - 1); while (scan != filename.get() && *scan == L' ') - --scan; + UNSAFE_TODO(--scan); - if (scan != filename.get() && (scan != filename.get() + filename.length())) + if (scan != filename.get() && + (scan != UNSAFE_TODO(filename.get() + filename.length()))) { filename.truncate_at(scan - filename.get() + 1); + } // First check for 6-character standard referral code XXXDDD, where // X is an alphabetic character and D is a numeric character. If not diff --git a/chromium_src/chrome/installer/setup/uninstall.cc b/chromium_src/chrome/installer/setup/uninstall.cc index 69f242f010f..c902c8e90f9 100644 --- a/chromium_src/chrome/installer/setup/uninstall.cc +++ b/chromium_src/chrome/installer/setup/uninstall.cc @@ -3,12 +3,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - +#include "base/compiler_specific.h" #include "base/files/file_path.h" #include "base/files/file_util.h" #include "base/logging.h" @@ -50,15 +45,18 @@ void DeleteBraveFileKeys(HKEY root) { // http://msdn.microsoft.com/en-us/library/bb166549 std::wstring file_assoc_key; std::wstring open_with_progids_key; - for (int i = 0; ShellUtil::kPotentialFileAssociations[i] != nullptr; ++i) { + for (int i = 0; + UNSAFE_TODO(ShellUtil::kPotentialFileAssociations[i]) != nullptr; ++i) { file_assoc_key.assign(ShellUtil::kRegClasses); file_assoc_key.push_back(base::FilePath::kSeparators[0]); - file_assoc_key.append(ShellUtil::kPotentialFileAssociations[i]); + file_assoc_key.append( + UNSAFE_TODO(ShellUtil::kPotentialFileAssociations[i])); file_assoc_key.push_back(base::FilePath::kSeparators[0]); open_with_progids_key.assign(file_assoc_key); open_with_progids_key.append(ShellUtil::kRegOpenWithProgids); - if (ShouldUseFileTypeProgId(ShellUtil::kPotentialFileAssociations[i])) { + if (ShouldUseFileTypeProgId( + UNSAFE_TODO(ShellUtil::kPotentialFileAssociations[i]))) { DeleteRegistryValue(root, open_with_progids_key, WorkItem::kWow64Default, GetProgIdForFileType()); } diff --git a/chromium_src/sandbox/win/src/interceptors_64.cc b/chromium_src/sandbox/win/src/interceptors_64.cc index a173d9fbd8e..ed27e15583b 100644 --- a/chromium_src/sandbox/win/src/interceptors_64.cc +++ b/chromium_src/sandbox/win/src/interceptors_64.cc @@ -3,14 +3,9 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "src/sandbox/win/src/interceptors_64.cc" +#include "base/compiler_specific.h" #include "brave/sandbox/win/src/module_file_name_interception.h" namespace sandbox { diff --git a/components/ai_chat/core/browser/text_embedder_unittest.cc b/components/ai_chat/core/browser/text_embedder_unittest.cc index f85bb278589..cb582de8e13 100644 --- a/components/ai_chat/core/browser/text_embedder_unittest.cc +++ b/components/ai_chat/core/browser/text_embedder_unittest.cc @@ -3,12 +3,6 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/components/ai_chat/core/browser/text_embedder.h" #include @@ -17,6 +11,7 @@ #include #include +#include "base/compiler_specific.h" #include "base/files/file_path.h" #include "base/location.h" #include "base/path_service.h" @@ -328,15 +323,16 @@ TEST_F(TextEmbedderUnitTest, RefineTopKSimilarity) { }; for (size_t i = 0; i < sizeof(test_cases) / sizeof(test_cases[0]); ++i) { SCOPED_TRACE("Test case index: " + base::NumberToString(i)); - SetSegments(embedder_.get(), test_cases[i].segments); - auto result = RefineTopKSimilarity(test_cases[i].ranked_sentences, - test_cases[i].context_limit); - if (test_cases[i].has_value) { + SetSegments(embedder_.get(), UNSAFE_TODO(test_cases[i]).segments); + auto result = + RefineTopKSimilarity(UNSAFE_TODO(test_cases[i]).ranked_sentences, + UNSAFE_TODO(test_cases[i]).context_limit); + if (UNSAFE_TODO(test_cases[i]).has_value) { EXPECT_TRUE(result.has_value()); - EXPECT_EQ(result.value(), test_cases[i].expected); + EXPECT_EQ(result.value(), UNSAFE_TODO(test_cases[i]).expected); } else { EXPECT_FALSE(result.has_value()); - EXPECT_EQ(result.error(), test_cases[i].expected); + EXPECT_EQ(result.error(), UNSAFE_TODO(test_cases[i]).expected); } } } diff --git a/components/ai_chat/renderer/page_content_extractor.cc b/components/ai_chat/renderer/page_content_extractor.cc index c61002e741c..076d3adc67b 100644 --- a/components/ai_chat/renderer/page_content_extractor.cc +++ b/components/ai_chat/renderer/page_content_extractor.cc @@ -3,12 +3,6 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this file, // You can obtain one at https://mozilla.org/MPL/2.0/. -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/components/ai_chat/renderer/page_content_extractor.h" #include @@ -23,6 +17,7 @@ #include "base/check.h" #include "base/check_op.h" +#include "base/compiler_specific.h" #include "base/containers/fixed_flat_set.h" #include "base/containers/flat_tree.h" #include "base/containers/span.h" diff --git a/components/brave_ads/core/internal/account/utility/redeem_confirmation/reward/redeem_reward_confirmation.cc b/components/brave_ads/core/internal/account/utility/redeem_confirmation/reward/redeem_reward_confirmation.cc index a88d7870fd3..b431daf48d6 100644 --- a/components/brave_ads/core/internal/account/utility/redeem_confirmation/reward/redeem_reward_confirmation.cc +++ b/components/brave_ads/core/internal/account/utility/redeem_confirmation/reward/redeem_reward_confirmation.cc @@ -3,12 +3,6 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/components/brave_ads/core/internal/account/utility/redeem_confirmation/reward/redeem_reward_confirmation.h" #include @@ -16,6 +10,7 @@ #include #include "base/check.h" +#include "base/compiler_specific.h" #include "base/functional/bind.h" #include "base/json/json_reader.h" #include "base/strings/string_util.h" diff --git a/components/brave_ads/core/internal/common/calendar/calendar_util_unittest.cc b/components/brave_ads/core/internal/common/calendar/calendar_util_unittest.cc index d190fb038bd..ff22f0801a3 100644 --- a/components/brave_ads/core/internal/common/calendar/calendar_util_unittest.cc +++ b/components/brave_ads/core/internal/common/calendar/calendar_util_unittest.cc @@ -3,14 +3,9 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/components/brave_ads/core/internal/common/calendar/calendar_util.h" +#include "base/compiler_specific.h" #include "base/time/time.h" // IWYU pragma: keep #include "brave/components/brave_ads/core/internal/common/test/time_test_util.h" #include "testing/gtest/include/gtest/gtest.h" @@ -45,7 +40,8 @@ TEST(BraveAdsCalendarUtilTest, DaysInMonth) { // Act & Assert for (size_t i = 0; i < 12; ++i) { - EXPECT_EQ(kLastDayInMonth[i], DaysInMonth(/*year=*/2021, /*month=*/i + 1)); + EXPECT_EQ(UNSAFE_TODO(kLastDayInMonth[i]), + DaysInMonth(/*year=*/2021, /*month=*/i + 1)); } } @@ -56,7 +52,8 @@ TEST(BraveAdsCalendarUtilTest, DaysInMonthForLeapYear) { // Act & Assert for (size_t i = 0; i < 12; ++i) { - EXPECT_EQ(kDaysInMonth[i], DaysInMonth(/*year=*/2020, /*month=*/i + 1)); + EXPECT_EQ(UNSAFE_TODO(kDaysInMonth[i]), + DaysInMonth(/*year=*/2020, /*month=*/i + 1)); } } diff --git a/components/brave_news/browser/feed_v2_builder.cc b/components/brave_news/browser/feed_v2_builder.cc index 5bcf4ff5288..1c258163674 100644 --- a/components/brave_news/browser/feed_v2_builder.cc +++ b/components/brave_news/browser/feed_v2_builder.cc @@ -3,12 +3,6 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this file, // You can obtain one at https://mozilla.org/MPL/2.0/. -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/components/brave_news/browser/feed_v2_builder.h" #include @@ -19,6 +13,7 @@ #include #include +#include "base/compiler_specific.h" #include "base/containers/contains.h" #include "base/containers/flat_map.h" #include "base/containers/span.h" diff --git a/components/brave_private_cdn/private_cdn_helper_unittest.cc b/components/brave_private_cdn/private_cdn_helper_unittest.cc index 339c9139187..659d2d46384 100644 --- a/components/brave_private_cdn/private_cdn_helper_unittest.cc +++ b/components/brave_private_cdn/private_cdn_helper_unittest.cc @@ -3,16 +3,12 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif +#include "brave/components/brave_private_cdn/private_cdn_helper.h" #include #include -#include "brave/components/brave_private_cdn/private_cdn_helper.h" +#include "base/compiler_specific.h" #include "testing/gtest/include/gtest/gtest.h" TEST(BravePrivateCdnHelper, RemovePadding) { @@ -78,8 +74,8 @@ TEST(BravePrivateCdnHelper, RemovePadding) { "Inputs and outputs must have the same number of elements."); for (size_t i = 0; i < kInputCount; i++) { - std::string_view padded_string(inputs[i]); + std::string_view padded_string(UNSAFE_TODO(inputs[i])); EXPECT_TRUE(brave::private_cdn::RemovePadding(&padded_string)); - EXPECT_EQ(padded_string, outputs[i]); + EXPECT_EQ(padded_string, UNSAFE_TODO(outputs[i])); } } diff --git a/components/brave_rewards/content/diagnostic_log.cc b/components/brave_rewards/content/diagnostic_log.cc index c703dda89ee..7c29cc47372 100644 --- a/components/brave_rewards/content/diagnostic_log.cc +++ b/components/brave_rewards/content/diagnostic_log.cc @@ -3,17 +3,12 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/components/brave_rewards/content/diagnostic_log.h" #include #include +#include "base/compiler_specific.h" #include "base/files/file_util.h" #include "base/i18n/time_formatting.h" #include "base/strings/stringprintf.h" @@ -126,12 +121,12 @@ int64_t SeekFromEnd(base::File* file, int num_lines) { return -1; } - if (file->ReadAtCurrentPos(chunk, chunk_size) == -1) { + if (UNSAFE_TODO(file->ReadAtCurrentPos(chunk, chunk_size)) == -1) { return -1; } for (int i = chunk_size - 1; i >= 0; i--) { - if (chunk[i] == '\n') { + if (UNSAFE_TODO(chunk[i]) == '\n') { line_count++; if (line_count == num_lines + 1) { return length; @@ -170,7 +165,7 @@ bool TrimBeginningOfFile(base::File* file, int keep_num_lines) { const int64_t size = file->GetLength() - offset; std::unique_ptr buffer = std::make_unique(size + 1); - if (file->ReadAtCurrentPos(buffer.get(), size) == -1) { + if (UNSAFE_TODO(file->ReadAtCurrentPos(buffer.get(), size)) == -1) { return false; } @@ -181,7 +176,7 @@ bool TrimBeginningOfFile(base::File* file, int keep_num_lines) { const std::string data = std::string(buffer.get()); const int64_t new_size = data.size(); - if (file->WriteAtCurrentPos(data.c_str(), new_size) == -1) { + if (UNSAFE_TODO(file->WriteAtCurrentPos(data.c_str(), new_size)) == -1) { return false; } @@ -246,7 +241,7 @@ std::string ReadLastNLinesOnFileTaskRunner(const base::FilePath& file_path, const int64_t size = file.GetLength() - offset; std::unique_ptr buffer = std::make_unique(size + 1); - if (file.ReadAtCurrentPos(buffer.get(), size) == -1) { + if (UNSAFE_TODO(file.ReadAtCurrentPos(buffer.get(), size)) == -1) { return ""; } @@ -269,10 +264,11 @@ bool WriteOnFileTaskRunner(const base::FilePath& file_path, if (first_write) { const std::string divider = std::string(kDividerLength, '-') + "\n"; - file.WriteAtCurrentPos(divider.c_str(), divider.length()); + UNSAFE_TODO(file.WriteAtCurrentPos(divider.c_str(), divider.length())); } - if (file.WriteAtCurrentPos(log_entry.c_str(), log_entry.length()) == -1) { + if (UNSAFE_TODO(file.WriteAtCurrentPos(log_entry.c_str(), + log_entry.length())) == -1) { return false; } diff --git a/components/brave_shields/content/browser/brave_shields_p3a.cc b/components/brave_shields/content/browser/brave_shields_p3a.cc index 96736a915e2..62fd2dad157 100644 --- a/components/brave_shields/content/browser/brave_shields_p3a.cc +++ b/components/brave_shields/content/browser/brave_shields_p3a.cc @@ -3,16 +3,11 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this file, // You can obtain one at https://mozilla.org/MPL/2.0/. -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/components/brave_shields/content/browser/brave_shields_p3a.h" #include +#include "base/compiler_specific.h" #include "base/logging.h" #include "base/metrics/histogram_functions.h" #include "base/metrics/histogram_macros.h" @@ -124,10 +119,11 @@ int DomainCountRelativeToGlobalSetting(PrefService* profile_prefs, is_fingerprint ? kFPSettingOrder : kAdsSettingOrder; // Initialized in order to start iteration near the current global_setting const ControlType* setting_order_it = - std::find(setting_order, setting_order + kSettingCount, global_setting); + std::find(setting_order, UNSAFE_TODO(setting_order + kSettingCount), + global_setting); bool setting_order_in_range = - setting_order_it < setting_order + kSettingCount; + setting_order_it < UNSAFE_TODO(setting_order + kSettingCount); DCHECK(setting_order_in_range) << "Shields global setting must be in setting_order"; if (!setting_order_in_range) { @@ -145,8 +141,8 @@ int DomainCountRelativeToGlobalSetting(PrefService* profile_prefs, // Will add all domain setting counts below or above the global_setting. for (int i = sum_index_start; count_above ? i < kSettingCount : i >= 0; count_above ? i++ : i--) { - total += - GetDomainSettingCount(profile_prefs, is_fingerprint, setting_order[i]); + total += GetDomainSettingCount(profile_prefs, is_fingerprint, + UNSAFE_TODO(setting_order[i])); } DCHECK_GE(total, 0) << "DomainCountRelativeToGlobalSetting must return a positive value"; diff --git a/components/brave_sync/time_limited_words.cc b/components/brave_sync/time_limited_words.cc index 6a0d9e6c5f2..6e028ca9314 100644 --- a/components/brave_sync/time_limited_words.cc +++ b/components/brave_sync/time_limited_words.cc @@ -3,18 +3,13 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/components/brave_sync/time_limited_words.h" #include #include #include +#include "base/compiler_specific.h" #include "base/containers/span.h" #include "base/logging.h" #include "base/notreached.h" @@ -158,7 +153,8 @@ TimeLimitedWords::ParseImpl(const std::string& time_limited_words, if (num_words == kPureWordsCount) { if (now < GetWordsV1SunsetDay()) { std::string recombined_pure_words = base::JoinString( - base::span(words.begin(), kPureWordsCount), " "); + UNSAFE_TODO(base::span(words.begin(), kPureWordsCount)), + " "); if (crypto::IsPassphraseValid(recombined_pure_words)) { return recombined_pure_words; } else { @@ -169,7 +165,8 @@ TimeLimitedWords::ParseImpl(const std::string& time_limited_words, } } else if (num_words == kWordsV2Count) { std::string recombined_pure_words = base::JoinString( - base::span(words.begin(), kPureWordsCount), " "); + UNSAFE_TODO(base::span(words.begin(), kPureWordsCount)), + " "); if (crypto::IsPassphraseValid(recombined_pure_words)) { int days_actual = GetRoundedDaysDiff(GetWordsV2Epoch(), now) % BIP39_WORDLIST_LEN; diff --git a/components/brave_vpn/browser/connection/ikev2/win/ras_utils.cc b/components/brave_vpn/browser/connection/ikev2/win/ras_utils.cc index e9a3d70f388..4adeb41d8ac 100644 --- a/components/brave_vpn/browser/connection/ikev2/win/ras_utils.cc +++ b/components/brave_vpn/browser/connection/ikev2/win/ras_utils.cc @@ -3,12 +3,6 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/components/brave_vpn/browser/connection/ikev2/win/ras_utils.h" #include @@ -20,6 +14,7 @@ #include #include "base/command_line.h" +#include "base/compiler_specific.h" #include "base/files/file_path.h" #include "base/files/file_util.h" #include "base/files/scoped_file.h" @@ -265,12 +260,12 @@ RasOperationResult DisconnectEntry(const std::wstring& entry_name) { << " : The following RAS connections are currently active:" << dw_connections; for (DWORD i = 0; i < dw_connections; i++) { - std::wstring name(lp_ras_conn[i].szEntryName); - std::wstring type(lp_ras_conn[i].szDeviceType); + std::wstring name(UNSAFE_TODO(lp_ras_conn[i]).szEntryName); + std::wstring type(UNSAFE_TODO(lp_ras_conn[i]).szDeviceType); VLOG(2) << __func__ << " : " << name << ", " << type; if (name.compare(entry_name) == 0 && type.compare(L"VPN") == 0) { VLOG(2) << __func__ << " : Disconnect... " << entry_name; - dw_ret = RasHangUp(lp_ras_conn[i].hrasconn); + dw_ret = RasHangUp(UNSAFE_TODO(lp_ras_conn[i]).hrasconn); if (dw_ret != ERROR_SUCCESS) { caller = "RasHangUp()"; } @@ -679,8 +674,8 @@ CheckConnectionResult CheckConnection(const std::wstring& entry_name) { // If successful, find connection with |entry_name|. CheckConnectionResult result = CheckConnectionResult::DISCONNECTED; for (DWORD i = 0; i < dw_connections; i++) { - if (entry_name.compare(lp_ras_conn[i].szEntryName) == 0) { - result = GetConnectionState(lp_ras_conn[i].hrasconn); + if (entry_name.compare(UNSAFE_TODO(lp_ras_conn[i]).szEntryName) == 0) { + result = GetConnectionState(UNSAFE_TODO(lp_ras_conn[i]).hrasconn); break; } } diff --git a/components/brave_vpn/common/wireguard/wireguard_utils.cc b/components/brave_vpn/common/wireguard/wireguard_utils.cc index a84b536ac90..758c40a33e9 100644 --- a/components/brave_vpn/common/wireguard/wireguard_utils.cc +++ b/components/brave_vpn/common/wireguard/wireguard_utils.cc @@ -3,12 +3,6 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/components/brave_vpn/common/wireguard/wireguard_utils.h" #include @@ -17,6 +11,7 @@ #include #include "base/base64.h" +#include "base/compiler_specific.h" #include "base/logging.h" #include "base/strings/string_util.h" #include "base/strings/utf_string_conversions.h" @@ -86,8 +81,8 @@ WireguardKeyPair GenerateNewX25519Keypair() { uint8_t pubkey[32] = {}, privkey[32] = {}; X25519_keypair(pubkey, privkey); return std::make_tuple( - EncodeBase64(std::vector(pubkey, pubkey + 32)), - EncodeBase64(std::vector(privkey, privkey + 32))); + EncodeBase64(std::vector(pubkey, UNSAFE_TODO(pubkey + 32))), + EncodeBase64(std::vector(privkey, UNSAFE_TODO(privkey + 32)))); } std::optional ValidateKey(const std::string& key, diff --git a/components/omnibox/browser/brave_search_provider_unittest.cc b/components/omnibox/browser/brave_search_provider_unittest.cc index 99bbefeb4ef..0278d4b35ff 100644 --- a/components/omnibox/browser/brave_search_provider_unittest.cc +++ b/components/omnibox/browser/brave_search_provider_unittest.cc @@ -3,12 +3,6 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/components/omnibox/browser/brave_search_provider.h" #include @@ -17,6 +11,7 @@ #include #include +#include "base/compiler_specific.h" #include "base/memory/raw_ptr.h" #include "base/memory/scoped_refptr.h" #include "base/run_loop.h" diff --git a/components/p3a/nitro_utils/attestation.cc b/components/p3a/nitro_utils/attestation.cc index 1e52fe2a0c2..499d417209b 100644 --- a/components/p3a/nitro_utils/attestation.cc +++ b/components/p3a/nitro_utils/attestation.cc @@ -3,12 +3,6 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/components/p3a/nitro_utils/attestation.h" #include @@ -22,6 +16,7 @@ #include #include "base/base64.h" +#include "base/compiler_specific.h" #include "base/functional/bind.h" #include "base/strings/string_number_conversions.h" #include "base/strings/string_util.h" @@ -128,7 +123,8 @@ bool VerifyUserDataKey(scoped_refptr server_cert, << "Nitro verification: user data is missing sha256 hash prefix"; return false; } - if (memcmp(server_cert_fp.data, user_data_bytes.data() + kHashPrefixLength, + if (memcmp(server_cert_fp.data, + UNSAFE_TODO(user_data_bytes.data() + kHashPrefixLength), kSHA256HashLength) == 0) { return true; } @@ -146,7 +142,7 @@ bool VerifyUserDataKey(scoped_refptr server_cert, return false; } if (memcmp(server_cert_fp.data, - user_data_bytes.data() + kMultihashPrefixLength, + UNSAFE_TODO(user_data_bytes.data() + kMultihashPrefixLength), kSHA256HashLength) == 0) { return true; } diff --git a/components/p3a/nitro_utils/cose.cc b/components/p3a/nitro_utils/cose.cc index e95127f4f47..92df6634782 100644 --- a/components/p3a/nitro_utils/cose.cc +++ b/components/p3a/nitro_utils/cose.cc @@ -3,18 +3,13 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/components/p3a/nitro_utils/cose.h" #include #include #include +#include "base/compiler_specific.h" #include "base/logging.h" #include "base/strings/string_number_conversions.h" #include "base/time/time.h" @@ -47,8 +42,9 @@ bool ConvertCoseSignatureToDER(const std::vector& input, if (!r_comp) { return false; } - BIGNUM* s_comp = BN_bin2bn(input.data() + kSignatureComponentSize, - kSignatureComponentSize, nullptr); + BIGNUM* s_comp = + BN_bin2bn(UNSAFE_TODO(input.data() + kSignatureComponentSize), + kSignatureComponentSize, nullptr); if (!s_comp) { BN_free(r_comp); return false; @@ -83,8 +79,8 @@ bool ConvertCoseSignatureToDER(const std::vector& input, } const uint8_t* sig_cbb_data = CBB_data(&sig_cbb); - *output = - std::vector(sig_cbb_data, sig_cbb_data + CBB_len(&sig_cbb)); + *output = std::vector(sig_cbb_data, + UNSAFE_TODO(sig_cbb_data + CBB_len(&sig_cbb))); CBB_cleanup(&sig_cbb); ECDSA_SIG_free(ecdsa_sig); diff --git a/components/permissions/permissions_client_unittest.cc b/components/permissions/permissions_client_unittest.cc index 53e4b77ef8a..0ef2c38b9a7 100644 --- a/components/permissions/permissions_client_unittest.cc +++ b/components/permissions/permissions_client_unittest.cc @@ -3,13 +3,9 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "components/permissions/permissions_client.h" + +#include "base/compiler_specific.h" #include "components/content_settings/core/common/content_settings_types.h" #include "testing/gtest/include/gtest/gtest.h" #include "url/gurl.h" @@ -38,17 +34,18 @@ TEST_F(PermissionsClientUnitTest, BraveCanBypassEmbeddingOriginCheck) { for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) { GURL embedding_origin("https://test.com"); EXPECT_TRUE(client->BraveCanBypassEmbeddingOriginCheck( - cases[i].requesting_origin, embedding_origin, cases[i].type)) + UNSAFE_TODO(cases[i]).requesting_origin, embedding_origin, + UNSAFE_TODO(cases[i]).type)) << "case: " << i; GURL embedding_origin_with_port("https://test.com:123"); EXPECT_TRUE(client->BraveCanBypassEmbeddingOriginCheck( - cases[i].requesting_origin_with_port, embedding_origin_with_port, - cases[i].type)) + UNSAFE_TODO(cases[i]).requesting_origin_with_port, + embedding_origin_with_port, UNSAFE_TODO(cases[i]).type)) << "case: " << i; EXPECT_FALSE(client->BraveCanBypassEmbeddingOriginCheck( - cases[i].requesting_origin, embedding_origin, + UNSAFE_TODO(cases[i]).requesting_origin, embedding_origin, ContentSettingsType::GEOLOCATION)) << "case: " << i; } diff --git a/components/tor/tor_control.cc b/components/tor/tor_control.cc index 63112201604..b6ff2c348b6 100644 --- a/components/tor/tor_control.cc +++ b/components/tor/tor_control.cc @@ -3,17 +3,12 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/components/tor/tor_control.h" #include #include +#include "base/compiler_specific.h" #include "base/files/file_path.h" #include "base/functional/bind.h" #include "base/functional/callback_helpers.h" @@ -59,9 +54,9 @@ constexpr char kGetCircuitEstablishedReply[] = "status/circuit-established="; static std::string escapify(const char* buf, int len) { std::ostringstream s; for (int i = 0; i < len; i++) { - unsigned char ch = static_cast(buf[i]); + unsigned char ch = static_cast(UNSAFE_TODO(buf[i])); if (::isprint(ch)) { - s << buf[i]; + s << UNSAFE_TODO(buf[i]); continue; } switch (ch) { @@ -80,8 +75,8 @@ static std::string escapify(const char* buf, int len) { default: const char hex[] = "0123456789abcdef"; s << "\\x"; - s << hex[(ch >> 4) & 0xf]; - s << hex[(ch >> 0) & 0xf]; + s << UNSAFE_TODO(hex[(ch >> 4) & 0xf]); + s << UNSAFE_TODO(hex[(ch >> 0) & 0xf]); break; } } @@ -803,9 +798,9 @@ void TorControl::ReadDone(int rv) { for (int i = 0; i < rv; i++) { if (!read_cr_) { // No CR yet. Accept CR or non-LF; reject LF. - if (data[i] == 0x0d) { // CR + if (UNSAFE_TODO(data[i]) == 0x0d) { // CR read_cr_ = true; - } else if (data[i] == 0x0a) { // LF + } else if (UNSAFE_TODO(data[i]) == 0x0a) { // LF VLOG(1) << "tor: stray line feed"; Error(); return; @@ -814,7 +809,7 @@ void TorControl::ReadDone(int rv) { } } else { // CR seen. Accept LF; reject all else. - if (data[i] == 0x0a) { // LF + if (UNSAFE_TODO(data[i]) == 0x0a) { // LF // CRLF seen, so we must have i >= 2. Emit a line and advance // to the next one, unless anything went wrong with the line. assert(i >= 1); diff --git a/components/tor/tor_control_unittest.cc b/components/tor/tor_control_unittest.cc index bb3d4b96782..56e245487d0 100644 --- a/components/tor/tor_control_unittest.cc +++ b/components/tor/tor_control_unittest.cc @@ -3,16 +3,11 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/components/tor/tor_control.h" #include +#include "base/compiler_specific.h" #include "base/functional/callback_helpers.h" #include "base/run_loop.h" #include "base/test/bind.h" @@ -70,11 +65,12 @@ TEST(TorControlTest, ParseQuoted) { for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { std::string value; size_t end; - bool ok = TorControl::ParseQuoted(cases[i].input, &value, &end); - if (cases[i].output) { + bool ok = + TorControl::ParseQuoted(UNSAFE_TODO(cases[i]).input, &value, &end); + if (UNSAFE_TODO(cases[i]).output) { EXPECT_TRUE(ok) << i; - EXPECT_EQ(value, cases[i].output) << i; - EXPECT_EQ(end, cases[i].end) << i; + EXPECT_EQ(value, UNSAFE_TODO(cases[i]).output) << i; + EXPECT_EQ(end, UNSAFE_TODO(cases[i]).end) << i; } else { EXPECT_FALSE(ok) << i; } @@ -102,22 +98,23 @@ TEST(TorControlTest, ParseKV) { std::string key; std::string value; size_t end; - bool ok = TorControl::ParseKV(cases[i].input, &key, &value, &end); - if (cases[i].value) { - EXPECT_TRUE(ok) << i << ": " << cases[i].input << "\nkey : " << key - << "\nvalue: " << value; - EXPECT_EQ(key, cases[i].key) - << i << ": " << cases[i].input << "\nkey : " << key + bool ok = + TorControl::ParseKV(UNSAFE_TODO(cases[i]).input, &key, &value, &end); + if (UNSAFE_TODO(cases[i]).value) { + EXPECT_TRUE(ok) << i << ": " << UNSAFE_TODO(cases[i]).input + << "\nkey : " << key << "\nvalue: " << value; + EXPECT_EQ(key, UNSAFE_TODO(cases[i]).key) + << i << ": " << UNSAFE_TODO(cases[i]).input << "\nkey : " << key << "\nvalue: " << value; - EXPECT_EQ(value, cases[i].value) - << i << ": " << cases[i].input << "\nkey : " << key + EXPECT_EQ(value, UNSAFE_TODO(cases[i]).value) + << i << ": " << UNSAFE_TODO(cases[i]).input << "\nkey : " << key << "\nvalue: " << value; - EXPECT_EQ(end, cases[i].end) - << i << ": " << cases[i].input << "\nkey : " << key + EXPECT_EQ(end, UNSAFE_TODO(cases[i]).end) + << i << ": " << UNSAFE_TODO(cases[i]).input << "\nkey : " << key << "\nvalue: " << value; } else { - EXPECT_FALSE(ok) << i << ": " << cases[i].input << "\nkey : " << key - << "\nvalue: " << value; + EXPECT_FALSE(ok) << i << ": " << UNSAFE_TODO(cases[i]).input + << "\nkey : " << key << "\nvalue: " << value; } } } diff --git a/components/tor/tor_file_watcher.cc b/components/tor/tor_file_watcher.cc index b6e586bf156..234ea63a1d1 100644 --- a/components/tor/tor_file_watcher.cc +++ b/components/tor/tor_file_watcher.cc @@ -3,17 +3,12 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/components/tor/tor_file_watcher.h" #include #include +#include "base/compiler_specific.h" #include "base/files/file.h" #include "base/logging.h" #include "base/strings/string_number_conversions.h" @@ -201,7 +196,7 @@ bool TorFileWatcher::EatControlCookie(std::vector& cookie, // indicate the file is abnormally large. constexpr size_t kBufSiz = 33; char buf[kBufSiz]; - int nread = cookiefile.ReadAtCurrentPos(buf, kBufSiz); + int nread = UNSAFE_TODO(cookiefile.ReadAtCurrentPos(buf, kBufSiz)); if (nread <= 0) { VLOG(0) << "tor: failed to read Tor control auth cookie"; return false; @@ -212,7 +207,7 @@ bool TorFileWatcher::EatControlCookie(std::vector& cookie, } // Success! - cookie.assign(buf, buf + nread); + cookie.assign(buf, UNSAFE_TODO(buf + nread)); mtime = info.last_accessed; VLOG(3) << "Control cookie " << base::HexEncode(buf, nread) << ", mtime " << mtime; @@ -246,7 +241,7 @@ bool TorFileWatcher::EatControlPort(int& port, base::Time& mtime) { // Read up to 27/28 octets, the maximum we will ever need. const size_t kBufSiz = sizeof(kControlPortMaxTmpl); char buf[kBufSiz]; - int nread = portfile.ReadAtCurrentPos(buf, sizeof buf); + int nread = UNSAFE_TODO(portfile.ReadAtCurrentPos(buf, sizeof buf)); if (nread < 0) { VLOG(0) << "tor: failed to read control port"; return false; @@ -260,7 +255,7 @@ bool TorFileWatcher::EatControlPort(int& port, base::Time& mtime) { return false; } - buf[nread] = '\0'; + UNSAFE_TODO(buf[nread]) = '\0'; std::string text(buf); // Sanity-check the content. diff --git a/ios/browser/api/net/certificate_utility.mm b/ios/browser/api/net/certificate_utility.mm index 86e5e695bef..95b85d4272c 100644 --- a/ios/browser/api/net/certificate_utility.mm +++ b/ios/browser/api/net/certificate_utility.mm @@ -3,12 +3,6 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #import "brave/ios/browser/api/net/certificate_utility.h" #include @@ -17,6 +11,7 @@ #import "base/apple/foundation_util.h" #include "base/base64.h" +#include "base/compiler_specific.h" #include "base/containers/span.h" #include "base/functional/bind.h" #include "base/logging.h" @@ -59,8 +54,9 @@ namespace { for (std::size_t i = 0; i < sizeof(net::kBraveAcceptableCerts) / sizeof(net::kBraveAcceptableCerts[0]); ++i) { - if (net::kBraveAcceptableCerts[i]) { - std::string data = std::string(net::kBraveAcceptableCerts[i]); + if (UNSAFE_TODO(net::kBraveAcceptableCerts[i])) { + std::string data = + std::string(UNSAFE_TODO(net::kBraveAcceptableCerts[i])); if (data.size() > 0) { [result addObject:[NSData dataWithBytes:&data[0] length:data.size()]]; } diff --git a/ios/browser/api/storekit_receipt/storekit_receipt.mm b/ios/browser/api/storekit_receipt/storekit_receipt.mm index 28ff70f01cf..8f4e6b7b44c 100644 --- a/ios/browser/api/storekit_receipt/storekit_receipt.mm +++ b/ios/browser/api/storekit_receipt/storekit_receipt.mm @@ -3,16 +3,11 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/ios/browser/api/storekit_receipt/storekit_receipt.h" #include +#include "base/compiler_specific.h" #include "base/logging.h" #include "base/strings/string_number_conversions.h" #include "base/strings/sys_string_conversions.h" @@ -71,8 +66,8 @@ NSString* decode_asn1_string(bssl::der::Input value) { if (type == V_ASN1_IA5STRING) { std::string result; - if (bssl::der::ParseIA5String(bssl::der::Input(base::span( - data, static_cast(length))), + if (bssl::der::ParseIA5String(bssl::der::Input(UNSAFE_TODO(base::span( + data, static_cast(length)))), &result)) { return base::SysUTF8ToNSString(result); } @@ -326,9 +321,9 @@ bool pkcs7_get_signed_content( } for (auto&& octet : result) { - auto receipt_input_ = bssl::der::Input(base::span( + auto receipt_input_ = bssl::der::Input(UNSAFE_TODO(base::span( ASN1_STRING_data(octet.get()), - static_cast(ASN1_STRING_length(octet.get())))); + static_cast(ASN1_STRING_length(octet.get()))))); if (receipt_input_.empty()) { VLOG(1) << "Cannot parse receipt data from PKCS7 container"; diff --git a/sandbox/win/src/module_file_name_interception.cc b/sandbox/win/src/module_file_name_interception.cc index bdca4d59c98..5282702b3a1 100644 --- a/sandbox/win/src/module_file_name_interception.cc +++ b/sandbox/win/src/module_file_name_interception.cc @@ -3,12 +3,6 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(https://github.com/brave/brave-browser/issues/41661): Remove this and -// convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/sandbox/win/src/module_file_name_interception.h" #include @@ -18,6 +12,7 @@ #include #include +#include "base/compiler_specific.h" #include "base/strings/string_util.h" #include "base/win/windows_types.h" @@ -82,12 +77,13 @@ std::optional PatchFilenameImpl(CharT* filename, --size; // space for null-terminator const size_t brave_pos = length - kBraveLen; - ReplaceAt(filename + brave_pos, size - brave_pos, FromTo::kChrome); + ReplaceAt(UNSAFE_TODO(filename + brave_pos), size - brave_pos, + FromTo::kChrome); if (size < length + kLenDiff) { ::SetLastError(ERROR_INSUFFICIENT_BUFFER); } length = std::min(size, length + kLenDiff); - filename[length] = 0; + UNSAFE_TODO(filename[length]) = 0; return length; } diff --git a/third_party/blink/renderer/brave_font_whitelist.cc b/third_party/blink/renderer/brave_font_whitelist.cc index 1a240f31bef..0fc297ae7d6 100644 --- a/third_party/blink/renderer/brave_font_whitelist.cc +++ b/third_party/blink/renderer/brave_font_whitelist.cc @@ -3,17 +3,13 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(crbug.com/ABC): Remove this and convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/third_party/blink/renderer/brave_font_whitelist.h" #include #include #include +#include "base/compiler_specific.h" #include "base/containers/fixed_flat_set.h" #if BUILDFLAG(IS_LINUX) diff --git a/third_party/blink/renderer/core/farbling/brave_session_cache.cc b/third_party/blink/renderer/core/farbling/brave_session_cache.cc index 19d0ce01786..2300b6954d6 100644 --- a/third_party/blink/renderer/core/farbling/brave_session_cache.cc +++ b/third_party/blink/renderer/core/farbling/brave_session_cache.cc @@ -3,15 +3,11 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(crbug.com/ABC): Remove this and convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/third_party/blink/renderer/core/farbling/brave_session_cache.h" #include +#include "base/compiler_specific.h" #include "base/debug/alias.h" #include "base/debug/dump_without_crashing.h" #include "base/feature_list.h" @@ -365,7 +361,8 @@ WTF::String BraveSessionCache::GenerateRandomString(std::string seed, base::span destination; WTF::String value = WTF::String::CreateUninitialized(length, destination); for (auto& c : destination) { - c = kLettersForRandomStrings[v % kLettersForRandomStringsLength]; + c = UNSAFE_TODO( + kLettersForRandomStrings[v % kLettersForRandomStringsLength]); v = lfsr_next(v); } return value; diff --git a/third_party/blink/renderer/platform/brave_audio_farbling_helper.cc b/third_party/blink/renderer/platform/brave_audio_farbling_helper.cc index 89b35d47703..bbe47f36fb7 100644 --- a/third_party/blink/renderer/platform/brave_audio_farbling_helper.cc +++ b/third_party/blink/renderer/platform/brave_audio_farbling_helper.cc @@ -3,15 +3,11 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#ifdef UNSAFE_BUFFERS_BUILD -// TODO(crbug.com/ABC): Remove this and convert code to safer constructs. -#pragma allow_unsafe_buffers -#endif - #include "brave/third_party/blink/renderer/platform/brave_audio_farbling_helper.h" #include +#include "base/compiler_specific.h" #include "third_party/blink/renderer/platform/audio/audio_utilities.h" namespace blink { @@ -60,17 +56,18 @@ void BraveAudioFarblingHelper::FarbleFloatTimeDomainData( for (size_t i = 0; i < len; ++i) { v = lfsr_next(v); float value = (v / maxUInt64AsDouble) / 10; - destination[i] = value; + UNSAFE_TODO(destination[i]) = value; } } else { for (size_t i = 0; i < len; ++i) { // Buffer access is protected due to modulo operation. float value = fudge_factor_ * - input_buffer[(i + write_index - fft_size + input_buffer_size) % - input_buffer_size]; + UNSAFE_TODO( + input_buffer[(i + write_index - fft_size + input_buffer_size) % + input_buffer_size]); - destination[i] = value; + UNSAFE_TODO(destination[i]) = value; } } } @@ -100,15 +97,16 @@ void BraveAudioFarblingHelper::FarbleByteTimeDomainData( scaled_value = UCHAR_MAX; } - destination[i] = static_cast(scaled_value); + UNSAFE_TODO(destination[i]) = static_cast(scaled_value); } } else { for (size_t i = 0; i < len; ++i) { // Buffer access is protected due to modulo operation. float value = fudge_factor_ * - input_buffer[(i + write_index - fft_size + input_buffer_size) % - input_buffer_size]; + UNSAFE_TODO( + input_buffer[(i + write_index - fft_size + input_buffer_size) % + input_buffer_size]); // Scale from nominal -1 -> +1 to unsigned byte. double scaled_value = 128 * (value + 1); @@ -121,7 +119,7 @@ void BraveAudioFarblingHelper::FarbleByteTimeDomainData( scaled_value = UCHAR_MAX; } - destination[i] = static_cast(scaled_value); + UNSAFE_TODO(destination[i]) = static_cast(scaled_value); } } } @@ -153,11 +151,11 @@ void BraveAudioFarblingHelper::FarbleConvertToByteData( scaled_value = UCHAR_MAX; } - destination[i] = static_cast(scaled_value); + UNSAFE_TODO(destination[i]) = static_cast(scaled_value); } } else { for (size_t i = 0; i < len; ++i) { - float linear_value = fudge_factor_ * source[i]; + float linear_value = fudge_factor_ * UNSAFE_TODO(source[i]); double db_mag = audio_utilities::LinearToDecibels(linear_value); // The range m_minDecibels to m_maxDecibels will be scaled to byte values @@ -173,7 +171,7 @@ void BraveAudioFarblingHelper::FarbleConvertToByteData( scaled_value = UCHAR_MAX; } - destination[i] = static_cast(scaled_value); + UNSAFE_TODO(destination[i]) = static_cast(scaled_value); } } } @@ -188,13 +186,13 @@ void BraveAudioFarblingHelper::FarbleConvertFloatToDb(const float* source, v = lfsr_next(v); float linear_value = (v / maxUInt64AsDouble) / 10; double db_mag = audio_utilities::LinearToDecibels(linear_value); - destination[i] = static_cast(db_mag); + UNSAFE_TODO(destination[i]) = static_cast(db_mag); } } else { for (size_t i = 0; i < len; ++i) { - float linear_value = fudge_factor_ * source[i]; + float linear_value = fudge_factor_ * UNSAFE_TODO(source[i]); double db_mag = audio_utilities::LinearToDecibels(linear_value); - destination[i] = static_cast(db_mag); + UNSAFE_TODO(destination[i]) = static_cast(db_mag); } } }