C++11 std::array rewrite for memory safety [3/19]
Split from:
https://chromium-review.googlesource.com/c/chromium/src/+/6004959/21
Generated patch
---------------
- Tool: ./tool/clang/spanify/rewrite-multiple-platform.sh
- Platform: Linux.
- Filter: This includes 2400/4222 patches. I included the std::array
ones and excluded build errors.
Google announcement:
--------------------
https://groups.google.com/a/google.com/g/chrome-memory-safety/c/RMiO4gaVLQA/m/Yz-3NCObAgAJ
Benchmarks:
----------
See design doc and
https://chromium-review.googlesource.com/c/chromium/src/+/6004959/21
Description
-----------
The consensus during the memory safety summit was to begin rewriting
relevant C-style arrays to C++11 std::array. It can be done immediately,
offers better developer ergonomics, and fix large chunks of the
-Wunsafe-buffer-usage errors in Chrome.
To clarify, this effort is complementary to the longer plan work with
enabling -fsanitize=array-bounds, and we plan to leverage both,
especially for protecting 3p code.
[Attached] is a document detailing the rationale, benefits, and
considerations for potential compile-time and performance impacts.
[Attached]:https://docs.google.com/document/d/1z5aBDg26lHmNDjXRCysElWKx7E4PAJXqykI_k7ondJI/edit?tab=t.0#heading=h.cqgo7wvp0kzt
NO_IFTTT=No need to update base/debug/stack_trace.h
Bug: 378069401
Change-Id: I3954f5f56075e55edadf8c00ad34fb858cbaacc6
R: [email protected]
AX-Relnotes: n/a.
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6043821
Commit-Queue: Daniel Cheng <[email protected]>
Auto-Submit: Arthur Sonzogni <[email protected]>
Reviewed-by: Daniel Cheng <[email protected]>
Owners-Override: Daniel Cheng <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1394426}
diff --git a/components/affiliations/core/browser/facet_manager_unittest.cc b/components/affiliations/core/browser/facet_manager_unittest.cc
index b024d7b..f01aa1844 100644
--- a/components/affiliations/core/browser/facet_manager_unittest.cc
+++ b/components/affiliations/core/browser/facet_manager_unittest.cc
@@ -2,16 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifdef UNSAFE_BUFFERS_BUILD
-// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
-#pragma allow_unsafe_buffers
-#endif
-
#include "components/affiliations/core/browser/facet_manager.h"
#include <stddef.h>
#include <algorithm>
+#include <array>
#include <memory>
#include "base/functional/bind.h"
@@ -581,10 +577,11 @@
// [F-------------------------F-----------------------F------------------>
//
TEST_F(FacetManagerTest, PrefetchWithEmptyOrStaleCache) {
- struct {
+ struct TestCases {
base::TimeDelta prefetch_length;
size_t expected_num_fetches;
- } const kTestCases[] = {
+ };
+ const auto kTestCases = std::to_array<TestCases>({
// Note: Zero length prefetches are tested later.
{GetShortTestPeriod(), 1},
{GetCacheSoftExpiryPeriod(), 1},
@@ -592,12 +589,14 @@
{GetCacheHardExpiryPeriod() + GetShortTestPeriod(), 2},
{GetCacheSoftExpiryPeriod() + GetCacheSoftExpiryPeriod(), 2},
{GetCacheHardExpiryPeriod() + GetCacheSoftExpiryPeriod(), 2},
- {base::TimeDelta::Max(), 3}};
+ {base::TimeDelta::Max(), 3},
+ });
- const base::TimeDelta kExpectedFetchTimes[] = {
+ const auto kExpectedFetchTimes = std::to_array<base::TimeDelta>({
base::TimeDelta(),
GetCacheSoftExpiryPeriod(),
- 2 * GetCacheSoftExpiryPeriod()};
+ 2 * GetCacheSoftExpiryPeriod(),
+ });
const base::TimeDelta kMaximumTestDuration = 2 * GetCacheHardExpiryPeriod();
@@ -688,11 +687,12 @@
// : [F----------------------F------------->
//
TEST_F(FacetManagerTest, PrefetchTriggeredFetchSchedulingAfterNonEmptyCache) {
- struct {
+ struct TestCases {
base::TimeDelta prefetch_start;
base::TimeDelta prefetch_end;
size_t expected_num_fetches;
- } const kTestCases[] = {
+ };
+ const auto kTestCases = std::to_array<TestCases>({
// Note: Zero length prefetches are tested later.
// Prefetch starts at the exact time the data was incidentally fetched.
@@ -702,39 +702,34 @@
{base::TimeDelta(), GetCacheHardExpiryPeriod() + GetShortTestPeriod(), 1},
{base::TimeDelta(), 2 * GetCacheSoftExpiryPeriod(), 1},
{base::TimeDelta(),
- GetCacheSoftExpiryPeriod() + GetCacheHardExpiryPeriod(),
- 1},
+ GetCacheSoftExpiryPeriod() + GetCacheHardExpiryPeriod(), 1},
{base::TimeDelta(), base::TimeDelta::Max(), 2},
// Prefetch starts a short time after the unrelated fetch.
{GetShortTestPeriod(), 2 * GetShortTestPeriod(), 0},
{GetShortTestPeriod(), GetCacheSoftExpiryPeriod(), 0},
{GetShortTestPeriod(), GetCacheHardExpiryPeriod(), 0},
- {GetShortTestPeriod(),
- GetCacheHardExpiryPeriod() + GetShortTestPeriod(),
+ {GetShortTestPeriod(), GetCacheHardExpiryPeriod() + GetShortTestPeriod(),
1},
{GetShortTestPeriod(), 2 * GetCacheSoftExpiryPeriod(), 1},
{GetShortTestPeriod(),
- GetCacheSoftExpiryPeriod() + GetCacheHardExpiryPeriod(),
- 1},
+ GetCacheSoftExpiryPeriod() + GetCacheHardExpiryPeriod(), 1},
{GetShortTestPeriod(), base::TimeDelta::Max(), 2},
// Prefetch starts at the soft expiry time of the unrelated fetch.
{GetCacheSoftExpiryPeriod(),
- GetCacheSoftExpiryPeriod() + GetShortTestPeriod(),
- 0},
+ GetCacheSoftExpiryPeriod() + GetShortTestPeriod(), 0},
{GetCacheSoftExpiryPeriod(), GetCacheHardExpiryPeriod(), 0},
- {GetShortTestPeriod(),
- GetCacheHardExpiryPeriod() + GetShortTestPeriod(),
+ {GetShortTestPeriod(), GetCacheHardExpiryPeriod() + GetShortTestPeriod(),
1},
{GetCacheSoftExpiryPeriod(), 2 * GetCacheSoftExpiryPeriod(), 1},
{GetCacheSoftExpiryPeriod(),
- GetCacheSoftExpiryPeriod() + GetCacheHardExpiryPeriod(),
- 1},
- {GetCacheSoftExpiryPeriod(), base::TimeDelta::Max(), 2}};
+ GetCacheSoftExpiryPeriod() + GetCacheHardExpiryPeriod(), 1},
+ {GetCacheSoftExpiryPeriod(), base::TimeDelta::Max(), 2},
+ });
- const base::TimeDelta kExpectedFetchTimes[] = {
- GetCacheSoftExpiryPeriod(), 2 * GetCacheSoftExpiryPeriod()};
+ const auto kExpectedFetchTimes = std::to_array<base::TimeDelta>(
+ {GetCacheSoftExpiryPeriod(), 2 * GetCacheSoftExpiryPeriod()});
const base::TimeDelta kMaximumTestDuration = 2 * GetCacheHardExpiryPeriod();
@@ -772,34 +767,32 @@
// Last block of tests from above.
TEST_F(FacetManagerTest, PrefetchTriggeredFetchSchedulingAfterNonEmptyCache2) {
- struct {
+ struct TestCases {
base::TimeDelta prefetch_start;
base::TimeDelta prefetch_end;
size_t expected_num_fetches;
- } const kTestCases[] = {
+ };
+ const auto kTestCases = std::to_array<TestCases>({
// Note: Zero length prefetches are tested later.
// Prefetch starts between the soft and hard expiry time.
{GetCacheSoftExpiryPeriod() + GetShortTestPeriod(),
- GetCacheHardExpiryPeriod(),
- 0},
+ GetCacheHardExpiryPeriod(), 0},
{GetCacheSoftExpiryPeriod() + GetShortTestPeriod(),
- GetCacheHardExpiryPeriod() + GetShortTestPeriod(),
- 1},
+ GetCacheHardExpiryPeriod() + GetShortTestPeriod(), 1},
{GetCacheSoftExpiryPeriod() + GetShortTestPeriod(),
- 2 * GetCacheSoftExpiryPeriod() + GetShortTestPeriod(),
- 1},
+ 2 * GetCacheSoftExpiryPeriod() + GetShortTestPeriod(), 1},
{GetCacheSoftExpiryPeriod() + GetShortTestPeriod(),
GetCacheHardExpiryPeriod() + GetCacheSoftExpiryPeriod() +
GetShortTestPeriod(),
1},
{GetCacheSoftExpiryPeriod() + GetShortTestPeriod(),
- base::TimeDelta::Max(),
- 2}};
+ base::TimeDelta::Max(), 2},
+ });
- const base::TimeDelta kExpectedFetchTimes[] = {
- GetCacheSoftExpiryPeriod() + GetShortTestPeriod(),
- 2 * GetCacheSoftExpiryPeriod() + GetShortTestPeriod()};
+ const auto kExpectedFetchTimes = std::to_array<base::TimeDelta>(
+ {GetCacheSoftExpiryPeriod() + GetShortTestPeriod(),
+ 2 * GetCacheSoftExpiryPeriod() + GetShortTestPeriod()});
const base::TimeDelta kMaximumTestDuration = 2 * GetCacheHardExpiryPeriod();
@@ -887,18 +880,20 @@
// : : : [----):
//
TEST_F(FacetManagerTest, NestedPrefetches) {
- struct {
+ struct FirstPrefetchParams {
base::TimeDelta prefetch_length;
size_t expected_num_fetches;
- } const kFirstPrefetchParams[] = {
+ };
+ const auto kFirstPrefetchParams = std::to_array<FirstPrefetchParams>({
{GetCacheHardExpiryPeriod() + GetCacheSoftExpiryPeriod(), 2},
{base::TimeDelta::Max(), 3},
- };
+ });
- struct {
+ struct SecondPrefetchParams {
base::TimeDelta second_prefetch_start;
base::TimeDelta second_prefetch_end;
- } const kSecondPrefetchParams[] = {
+ };
+ const auto kSecondPrefetchParams = std::to_array<SecondPrefetchParams>({
{base::TimeDelta(), GetShortTestPeriod()},
{GetShortTestPeriod(), 2 * GetShortTestPeriod()},
{GetShortTestPeriod(), GetCacheSoftExpiryPeriod()},
@@ -919,12 +914,14 @@
{2 * GetCacheSoftExpiryPeriod(),
GetCacheHardExpiryPeriod() + GetCacheSoftExpiryPeriod()},
{2 * GetCacheSoftExpiryPeriod() + GetShortTestPeriod(),
- GetCacheHardExpiryPeriod() + GetCacheSoftExpiryPeriod()}};
+ GetCacheHardExpiryPeriod() + GetCacheSoftExpiryPeriod()},
+ });
- const base::TimeDelta kExpectedFetchTimes[] = {
+ const auto kExpectedFetchTimes = std::to_array<base::TimeDelta>({
base::TimeDelta(),
GetCacheSoftExpiryPeriod(),
- 2 * GetCacheSoftExpiryPeriod()};
+ 2 * GetCacheSoftExpiryPeriod(),
+ });
const base::TimeDelta kTestDuration =
GetCacheSoftExpiryPeriod() + GetCacheHardExpiryPeriod();
@@ -972,24 +969,25 @@
// : [F----------------------F----------->
//
TEST_F(FacetManagerTest, OverlappingPrefetches) {
- struct {
+ struct TestCases {
base::TimeDelta second_prefetch_start;
base::TimeDelta second_prefetch_end;
size_t expected_num_fetches;
- } const kTestCases[] = {
+ };
+ const auto kTestCases = std::to_array<TestCases>({
{GetShortTestPeriod(),
- GetCacheSoftExpiryPeriod() + GetCacheHardExpiryPeriod(),
- 2},
+ GetCacheSoftExpiryPeriod() + GetCacheHardExpiryPeriod(), 2},
{GetShortTestPeriod(), base::TimeDelta::Max(), 3},
{GetCacheSoftExpiryPeriod(),
- GetCacheSoftExpiryPeriod() + GetCacheHardExpiryPeriod(),
- 2},
- {GetCacheSoftExpiryPeriod(), base::TimeDelta::Max(), 3}};
+ GetCacheSoftExpiryPeriod() + GetCacheHardExpiryPeriod(), 2},
+ {GetCacheSoftExpiryPeriod(), base::TimeDelta::Max(), 3},
+ });
- const base::TimeDelta kExpectedFetchTimes[] = {
+ const auto kExpectedFetchTimes = std::to_array<base::TimeDelta>({
base::TimeDelta(),
GetCacheSoftExpiryPeriod(),
- 2 * GetCacheSoftExpiryPeriod()};
+ 2 * GetCacheSoftExpiryPeriod(),
+ });
const base::TimeDelta kTestDuration =
GetCacheSoftExpiryPeriod() + GetCacheHardExpiryPeriod();
@@ -1044,76 +1042,50 @@
// [NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN...
//
TEST_F(FacetManagerTest, PrefetchWithNonInstantFetches) {
- struct {
+ struct TestCases {
base::TimeDelta prefetch_length;
base::TimeDelta expected_fetch_time1;
base::TimeDelta fetch_completion_delay1;
base::TimeDelta expected_fetch_time2;
base::TimeDelta fetch_completion_delay2;
- } const kTestCases[] = {
- {GetCacheHardExpiryPeriod(),
- base::TimeDelta(),
- GetShortTestPeriod(),
- base::TimeDelta::Max(),
- base::TimeDelta::Max()},
+ };
+ const auto kTestCases = std::to_array<TestCases>({
+ {GetCacheHardExpiryPeriod(), base::TimeDelta(), GetShortTestPeriod(),
+ base::TimeDelta::Max(), base::TimeDelta::Max()},
{GetCacheHardExpiryPeriod() + GetCacheSoftExpiryPeriod(),
- base::TimeDelta(),
- base::TimeDelta(),
- GetCacheSoftExpiryPeriod(),
+ base::TimeDelta(), base::TimeDelta(), GetCacheSoftExpiryPeriod(),
GetCacheSoftExpiryPeriod() + GetShortTestPeriod()},
{GetCacheHardExpiryPeriod() + GetCacheSoftExpiryPeriod(),
- base::TimeDelta(),
- GetCacheSoftExpiryPeriod(),
- base::TimeDelta::Max(),
+ base::TimeDelta(), GetCacheSoftExpiryPeriod(), base::TimeDelta::Max(),
base::TimeDelta::Max()},
- {GetCacheHardExpiryPeriod() + GetShortTestPeriod(),
- base::TimeDelta(),
- GetShortTestPeriod(),
- base::TimeDelta::Max(),
- base::TimeDelta::Max()},
+ {GetCacheHardExpiryPeriod() + GetShortTestPeriod(), base::TimeDelta(),
+ GetShortTestPeriod(), base::TimeDelta::Max(), base::TimeDelta::Max()},
{GetCacheHardExpiryPeriod() + GetCacheSoftExpiryPeriod() +
GetShortTestPeriod(),
- base::TimeDelta(),
- base::TimeDelta(),
- GetCacheSoftExpiryPeriod(),
+ base::TimeDelta(), base::TimeDelta(), GetCacheSoftExpiryPeriod(),
GetCacheSoftExpiryPeriod() + GetShortTestPeriod()},
{GetCacheHardExpiryPeriod() + GetCacheSoftExpiryPeriod() +
GetShortTestPeriod(),
- base::TimeDelta(),
- GetCacheSoftExpiryPeriod() + GetShortTestPeriod(),
- base::TimeDelta::Max(),
- base::TimeDelta::Max()},
+ base::TimeDelta(), GetCacheSoftExpiryPeriod() + GetShortTestPeriod(),
+ base::TimeDelta::Max(), base::TimeDelta::Max()},
{GetCacheHardExpiryPeriod() + GetCacheSoftExpiryPeriod() +
2 * GetShortTestPeriod(),
- base::TimeDelta(),
- GetShortTestPeriod(),
+ base::TimeDelta(), GetShortTestPeriod(),
GetCacheSoftExpiryPeriod() + GetShortTestPeriod(),
GetCacheSoftExpiryPeriod() + 2 * GetShortTestPeriod()},
- {GetShortTestPeriod(),
- base::TimeDelta(),
- base::TimeDelta::Max(),
- base::TimeDelta::Max(),
- base::TimeDelta::Max()},
- {GetCacheHardExpiryPeriod(),
- base::TimeDelta(),
- base::TimeDelta::Max(),
- base::TimeDelta::Max(),
+ {GetShortTestPeriod(), base::TimeDelta(), base::TimeDelta::Max(),
+ base::TimeDelta::Max(), base::TimeDelta::Max()},
+ {GetCacheHardExpiryPeriod(), base::TimeDelta(), base::TimeDelta::Max(),
+ base::TimeDelta::Max(), base::TimeDelta::Max()},
+ {GetCacheSoftExpiryPeriod() + GetCacheHardExpiryPeriod(),
+ base::TimeDelta(), base::TimeDelta(), GetCacheSoftExpiryPeriod(),
base::TimeDelta::Max()},
{GetCacheSoftExpiryPeriod() + GetCacheHardExpiryPeriod(),
- base::TimeDelta(),
- base::TimeDelta(),
- GetCacheSoftExpiryPeriod(),
+ base::TimeDelta(), base::TimeDelta::Max(), base::TimeDelta::Max(),
base::TimeDelta::Max()},
- {GetCacheSoftExpiryPeriod() + GetCacheHardExpiryPeriod(),
- base::TimeDelta(),
- base::TimeDelta::Max(),
- base::TimeDelta::Max(),
- base::TimeDelta::Max()},
- {base::TimeDelta::Max(),
- base::TimeDelta(),
- base::TimeDelta::Max(),
- base::TimeDelta::Max(),
- base::TimeDelta::Max()}};
+ {base::TimeDelta::Max(), base::TimeDelta(), base::TimeDelta::Max(),
+ base::TimeDelta::Max(), base::TimeDelta::Max()},
+ });
const base::TimeDelta kMaximumTestDuration = GetCacheSoftExpiryPeriod() +
GetCacheHardExpiryPeriod() +
@@ -1171,33 +1143,32 @@
// [F-------------------------F-----------------------F--X- - - - ->
//
TEST_F(FacetManagerTest, CancelPrefetch) {
- struct {
+ struct TestCases {
base::TimeDelta prefetch_length;
base::TimeDelta cancel_time;
size_t expected_num_fetches;
- } const kTestCases[] = {
+ };
+ const auto kTestCases = std::to_array<TestCases>({
{GetCacheHardExpiryPeriod(), GetShortTestPeriod(), 1},
{GetCacheHardExpiryPeriod(), GetCacheSoftExpiryPeriod(), 1},
{GetCacheHardExpiryPeriod(),
- GetCacheSoftExpiryPeriod() + GetShortTestPeriod(),
- 1},
+ GetCacheSoftExpiryPeriod() + GetShortTestPeriod(), 1},
{base::TimeDelta::Max(), GetShortTestPeriod(), 1},
{base::TimeDelta::Max(), GetCacheSoftExpiryPeriod(), 1},
{base::TimeDelta::Max(),
- GetCacheSoftExpiryPeriod() + GetShortTestPeriod(),
- 2},
+ GetCacheSoftExpiryPeriod() + GetShortTestPeriod(), 2},
{base::TimeDelta::Max(),
- GetCacheHardExpiryPeriod() + GetShortTestPeriod(),
- 2},
+ GetCacheHardExpiryPeriod() + GetShortTestPeriod(), 2},
{base::TimeDelta::Max(), 2 * GetCacheSoftExpiryPeriod(), 2},
{base::TimeDelta::Max(),
- 2 * GetCacheSoftExpiryPeriod() + GetShortTestPeriod(),
- 3}};
+ 2 * GetCacheSoftExpiryPeriod() + GetShortTestPeriod(), 3},
+ });
- const base::TimeDelta kExpectedFetchTimes[] = {
+ const auto kExpectedFetchTimes = std::to_array<base::TimeDelta>({
base::TimeDelta(),
GetCacheSoftExpiryPeriod(),
- 2 * GetCacheSoftExpiryPeriod()};
+ 2 * GetCacheSoftExpiryPeriod(),
+ });
for (size_t i = 0; i < std::size(kTestCases); ++i) {
SCOPED_TRACE(testing::Message() << "Test case: #" << i);
diff --git a/components/blocklist/opt_out_blocklist/opt_out_blocklist_unittest.cc b/components/blocklist/opt_out_blocklist/opt_out_blocklist_unittest.cc
index 5e282a3b..5d3c8ee 100644
--- a/components/blocklist/opt_out_blocklist/opt_out_blocklist_unittest.cc
+++ b/components/blocklist/opt_out_blocklist/opt_out_blocklist_unittest.cc
@@ -2,14 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifdef UNSAFE_BUFFERS_BUILD
-// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
-#pragma allow_unsafe_buffers
-#endif
-
#include "components/blocklist/opt_out_blocklist/opt_out_blocklist.h"
#include <algorithm>
+#include <array>
#include <map>
#include <memory>
#include <string>
@@ -585,12 +581,12 @@
TEST_F(OptOutBlocklistTest, HostIndifferentBlocklist) {
// Tests the block list behavior when a null OptOutStore is passed in.
- const std::string hosts[] = {
+ const auto hosts = std::to_array<std::string>({
"url_0.com",
"url_1.com",
"url_2.com",
"url_3.com",
- };
+ });
int host_indifferent_threshold = 4;
@@ -893,12 +889,12 @@
TEST_F(OptOutBlocklistTest, ObserverIsNotifiedOnUserBlocklisted) {
// Tests the block list behavior when a null OptOutStore is passed in.
- const std::string hosts[] = {
+ const auto hosts = std::to_array<std::string>({
"url_0.com",
"url_1.com",
"url_2.com",
"url_3.com",
- };
+ });
int host_indifferent_threshold = 4;
@@ -1087,12 +1083,12 @@
TEST_F(OptOutBlocklistTest, PassedReasonsWhenUserBlocklisted) {
// Test that IsLoadedAndAllow, push checked BlocklistReasons to the
// |passed_reasons| vector.
- const std::string hosts[] = {
+ const auto hosts = std::to_array<std::string>({
"http://www.url_0.com",
"http://www.url_1.com",
"http://www.url_2.com",
"http://www.url_3.com",
- };
+ });
auto session_policy =
std::make_unique<BlocklistData::Policy>(base::Seconds(1), 1u, 1);
@@ -1117,10 +1113,10 @@
BlocklistReason::kUserOptedOutInGeneral,
block_list_->IsLoadedAndAllowed(hosts[0], 1, false, &passed_reasons_));
- BlocklistReason expected_reasons[] = {
+ auto expected_reasons = std::to_array<BlocklistReason>({
BlocklistReason::kBlocklistNotLoaded,
BlocklistReason::kUserOptedOutInSession,
- };
+ });
EXPECT_EQ(std::size(expected_reasons), passed_reasons_.size());
for (size_t i = 0; i < passed_reasons_.size(); i++) {
EXPECT_EQ(expected_reasons[i], passed_reasons_[i]);
@@ -1153,11 +1149,11 @@
BlocklistReason::kUserOptedOutOfHost,
block_list_->IsLoadedAndAllowed(kTestHost1, 1, false, &passed_reasons_));
- BlocklistReason expected_reasons[] = {
+ auto expected_reasons = std::to_array<BlocklistReason>({
BlocklistReason::kBlocklistNotLoaded,
BlocklistReason::kUserOptedOutInSession,
BlocklistReason::kUserOptedOutInGeneral,
- };
+ });
EXPECT_EQ(std::size(expected_reasons), passed_reasons_.size());
for (size_t i = 0; i < passed_reasons_.size(); i++) {
EXPECT_EQ(expected_reasons[i], passed_reasons_[i]);
@@ -1190,13 +1186,13 @@
BlocklistReason::kAllowed,
block_list_->IsLoadedAndAllowed(kTestHost1, 1, false, &passed_reasons_));
- BlocklistReason expected_reasons[] = {
+ auto expected_reasons = std::to_array<BlocklistReason>({
BlocklistReason::kBlocklistNotLoaded,
BlocklistReason::kUserOptedOutInSession,
BlocklistReason::kUserOptedOutInGeneral,
BlocklistReason::kUserOptedOutOfHost,
BlocklistReason::kUserOptedOutOfType,
- };
+ });
EXPECT_EQ(std::size(expected_reasons), passed_reasons_.size());
for (size_t i = 0; i < passed_reasons_.size(); i++) {
EXPECT_EQ(expected_reasons[i], passed_reasons_[i]);
diff --git a/components/browsing_topics/browsing_topics_calculator_unittest.cc b/components/browsing_topics/browsing_topics_calculator_unittest.cc
index 195ee22..053691f 100644
--- a/components/browsing_topics/browsing_topics_calculator_unittest.cc
+++ b/components/browsing_topics/browsing_topics_calculator_unittest.cc
@@ -2,13 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifdef UNSAFE_BUFFERS_BUILD
-// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
-#pragma allow_unsafe_buffers
-#endif
-
#include "components/browsing_topics/browsing_topics_calculator.h"
+#include <array>
#include <memory>
#include "base/files/scoped_temp_dir.h"
@@ -51,10 +47,12 @@
constexpr char kHost6[] = "www.foo6.com";
Topic ExpectedRandomTopic(size_t index) {
- Topic kExpectedRandomTopicsForTaxonomyV1[5] = {
- Topic(101), Topic(102), Topic(103), Topic(104), Topic(105)};
- Topic kExpectedRandomTopicsForTaxonomyV2[5] = {
- Topic(176), Topic(177), Topic(180), Topic(183), Topic(184)};
+ std::array<Topic, 5> kExpectedRandomTopicsForTaxonomyV1 = {
+ Topic(101), Topic(102), Topic(103), Topic(104), Topic(105),
+ };
+ std::array<Topic, 5> kExpectedRandomTopicsForTaxonomyV2 = {
+ Topic(176), Topic(177), Topic(180), Topic(183), Topic(184),
+ };
if (blink::features::kBrowsingTopicsTaxonomyVersion.Get() == 1) {
return kExpectedRandomTopicsForTaxonomyV1[index];
diff --git a/components/certificate_transparency/chrome_ct_policy_enforcer_unittest.cc b/components/certificate_transparency/chrome_ct_policy_enforcer_unittest.cc
index c05ece9f..0d13c82 100644
--- a/components/certificate_transparency/chrome_ct_policy_enforcer_unittest.cc
+++ b/components/certificate_transparency/chrome_ct_policy_enforcer_unittest.cc
@@ -2,13 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifdef UNSAFE_BUFFERS_BUILD
-// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
-#pragma allow_unsafe_buffers
-#endif
-
#include "components/certificate_transparency/chrome_ct_policy_enforcer.h"
+#include <array>
#include <map>
#include <memory>
#include <string>
@@ -625,20 +621,23 @@
base::Time time_2016_3_0_25_11_25_0_0 =
CreateTime({2016, 3, 0, 25, 11, 25, 0, 0});
- const struct TestData {
+ struct TestData {
base::Time validity_start;
base::Time validity_end;
size_t scts_required;
- } kTestData[] = {{// Cert valid for -12 months (nonsensical), needs 2 SCTs.
- time_2016_3_0_25_11_25_0_0, time_2015_3_0_25_11_25_0_0, 2},
- {// Cert valid for 179 days, needs 2 SCTs.
- time_2015_3_0_25_11_25_0_0, time_2015_9_0_20_11_25_0_0, 2},
- {// Cert valid for exactly 180 days, needs only 2 SCTs.
- time_2015_3_0_25_11_25_0_0, time_2015_9_0_21_11_25_0_0, 2},
- {// Cert valid for barely over 180 days, needs 3 SCTs.
- time_2015_3_0_25_11_25_0_0, time_2015_9_0_21_11_25_1_0, 3},
- {// Cert valid for over 180 days, needs 3 SCTs.
- time_2015_3_0_25_11_25_0_0, time_2016_3_0_25_11_25_0_0, 3}};
+ };
+ const auto kTestData = std::to_array<TestData>({
+ {// Cert valid for -12 months (nonsensical), needs 2 SCTs.
+ time_2016_3_0_25_11_25_0_0, time_2015_3_0_25_11_25_0_0, 2},
+ {// Cert valid for 179 days, needs 2 SCTs.
+ time_2015_3_0_25_11_25_0_0, time_2015_9_0_20_11_25_0_0, 2},
+ {// Cert valid for exactly 180 days, needs only 2 SCTs.
+ time_2015_3_0_25_11_25_0_0, time_2015_9_0_21_11_25_0_0, 2},
+ {// Cert valid for barely over 180 days, needs 3 SCTs.
+ time_2015_3_0_25_11_25_0_0, time_2015_9_0_21_11_25_1_0, 3},
+ {// Cert valid for over 180 days, needs 3 SCTs.
+ time_2015_3_0_25_11_25_0_0, time_2016_3_0_25_11_25_0_0, 3},
+ });
for (size_t i = 0; i < std::size(kTestData); ++i) {
SCOPED_TRACE(i);
diff --git a/components/content_settings/core/browser/content_settings_utils_unittest.cc b/components/content_settings/core/browser/content_settings_utils_unittest.cc
index c0cabb15..f7054ff 100644
--- a/components/content_settings/core/browser/content_settings_utils_unittest.cc
+++ b/components/content_settings/core/browser/content_settings_utils_unittest.cc
@@ -2,15 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifdef UNSAFE_BUFFERS_BUILD
-// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
-#pragma allow_unsafe_buffers
-#endif
-
#include "components/content_settings/core/browser/content_settings_utils.h"
#include <stddef.h>
+#include <array>
#include <string>
#include "base/test/scoped_feature_list.h"
@@ -27,14 +23,14 @@
namespace {
// clang-format off
-const char* const kContentSettingNames[] = {
+const auto kContentSettingNames = std::to_array<const char *>({
"default",
"allow",
"block",
"ask",
"session_only",
"detect_important_content",
-};
+});
// clang-format on
static_assert(std::size(kContentSettingNames) == CONTENT_SETTING_NUM_SETTINGS,
diff --git a/components/crx_file/id_util_unittest.cc b/components/crx_file/id_util_unittest.cc
index c60a81e..239bf37 100644
--- a/components/crx_file/id_util_unittest.cc
+++ b/components/crx_file/id_util_unittest.cc
@@ -6,12 +6,14 @@
#include <stdint.h>
+#include <array>
+
#include "testing/gtest/include/gtest/gtest.h"
namespace crx_file::id_util {
TEST(IDUtilTest, GenerateID) {
- const uint8_t public_key_info[] = {
+ const auto public_key_info = std::to_array<uint8_t>({
0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81,
0x89, 0x02, 0x81, 0x81, 0x00, 0xb8, 0x7f, 0x2b, 0x20, 0xdc, 0x7c, 0x9b,
@@ -25,7 +27,8 @@
0x84, 0x32, 0x33, 0xf3, 0x17, 0x49, 0xbf, 0xe9, 0x96, 0xd0, 0xd6, 0x14,
0x6f, 0x13, 0x8d, 0xc5, 0xfc, 0x2c, 0x72, 0xba, 0xac, 0xea, 0x7e, 0x18,
0x53, 0x56, 0xa6, 0x83, 0xa2, 0xce, 0x93, 0x93, 0xe7, 0x1f, 0x0f, 0xe6,
- 0x0f, 0x02, 0x03, 0x01, 0x00, 0x01};
+ 0x0f, 0x02, 0x03, 0x01, 0x00, 0x01,
+ });
std::string extension_id =
GenerateId(std::string(reinterpret_cast<const char*>(&public_key_info[0]),
std::size(public_key_info)));
diff --git a/components/input/gesture_event_queue_unittest.cc b/components/input/gesture_event_queue_unittest.cc
index 4c7a122..0c59c51 100644
--- a/components/input/gesture_event_queue_unittest.cc
+++ b/components/input/gesture_event_queue_unittest.cc
@@ -2,15 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifdef UNSAFE_BUFFERS_BUILD
-// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
-#pragma allow_unsafe_buffers
-#endif
-
#include "components/input/gesture_event_queue.h"
#include <stddef.h>
+#include <array>
#include <memory>
#include <utility>
#include <vector>
@@ -325,12 +321,13 @@
EXPECT_FALSE(ScrollingInProgress());
// Verify that the coalescing queue contains the correct events.
- WebInputEvent::Type expected[] = {WebInputEvent::Type::kGestureScrollUpdate,
- WebInputEvent::Type::kGestureScrollUpdate,
- WebInputEvent::Type::kGestureScrollEnd};
+ auto expected = std::to_array<WebInputEvent::Type>({
+ WebInputEvent::Type::kGestureScrollUpdate,
+ WebInputEvent::Type::kGestureScrollUpdate,
+ WebInputEvent::Type::kGestureScrollEnd,
+ });
- for (unsigned i = 0; i < sizeof(expected) / sizeof(WebInputEvent::Type);
- i++) {
+ for (size_t i = 0; i < expected.size(); i++) {
WebGestureEvent merged_event = GestureEventQueueEventAt(i);
EXPECT_EQ(expected[i], merged_event.GetType());
}
@@ -363,12 +360,13 @@
EXPECT_EQ(0U, GestureEventDebouncingQueueSize());
// Verify that the coalescing queue contains the correct events.
- WebInputEvent::Type expected[] = {WebInputEvent::Type::kGestureScrollUpdate,
- WebInputEvent::Type::kGestureScrollEnd,
- WebInputEvent::Type::kGestureScrollBegin};
+ auto expected = std::to_array<WebInputEvent::Type>({
+ WebInputEvent::Type::kGestureScrollUpdate,
+ WebInputEvent::Type::kGestureScrollEnd,
+ WebInputEvent::Type::kGestureScrollBegin,
+ });
- for (unsigned i = 0; i < sizeof(expected) / sizeof(WebInputEvent::Type);
- i++) {
+ for (size_t i = 0; i < expected.size(); i++) {
WebGestureEvent merged_event = GestureEventQueueEventAt(i);
EXPECT_EQ(expected[i], merged_event.GetType());
}
@@ -428,11 +426,11 @@
EXPECT_TRUE(ScrollingInProgress());
// Verify that the coalescing queue contains the correct events.
- WebInputEvent::Type expected[] = {WebInputEvent::Type::kGestureScrollUpdate,
- WebInputEvent::Type::kGestureScrollUpdate};
+ auto expected = std::to_array<WebInputEvent::Type>(
+ {WebInputEvent::Type::kGestureScrollUpdate,
+ WebInputEvent::Type::kGestureScrollUpdate});
- for (unsigned i = 0; i < sizeof(expected) / sizeof(WebInputEvent::Type);
- i++) {
+ for (size_t i = 0; i < expected.size(); i++) {
WebGestureEvent merged_event = GestureEventQueueEventAt(i);
EXPECT_EQ(expected[i], merged_event.GetType());
}
@@ -468,11 +466,11 @@
EXPECT_TRUE(ScrollingInProgress());
// Verify that the coalescing queue contains the correct events.
- WebInputEvent::Type expected[] = {WebInputEvent::Type::kGestureScrollUpdate,
- WebInputEvent::Type::kGestureScrollUpdate};
+ auto expected = std::to_array<WebInputEvent::Type>(
+ {WebInputEvent::Type::kGestureScrollUpdate,
+ WebInputEvent::Type::kGestureScrollUpdate});
- for (unsigned i = 0; i < sizeof(expected) / sizeof(WebInputEvent::Type);
- i++) {
+ for(size_t i = 0; i < expected.size(); i++) {
WebGestureEvent merged_event = GestureEventQueueEventAt(i);
EXPECT_EQ(expected[i], merged_event.GetType());
}
diff --git a/components/input/touch_event_stream_validator_unittest.cc b/components/input/touch_event_stream_validator_unittest.cc
index 08504ea..a0d4374 100644
--- a/components/input/touch_event_stream_validator_unittest.cc
+++ b/components/input/touch_event_stream_validator_unittest.cc
@@ -2,15 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifdef UNSAFE_BUFFERS_BUILD
-// TODO(crbug.com/342213636): Remove this and spanify to fix the errors.
-#pragma allow_unsafe_buffers
-#endif
-
#include "components/input/touch_event_stream_validator.h"
#include <stddef.h>
+#include <array>
+
#include "components/input/web_touch_event_traits.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/input/synthetic_web_input_event_builders.h"
@@ -143,14 +140,14 @@
TouchEventStreamValidator validator;
std::string error_msg;
- WebInputEvent::Type kTouchTypes[4] = {
+ std::array<WebInputEvent::Type, 4> kTouchTypes = {
WebInputEvent::Type::kTouchStart,
WebInputEvent::Type::kTouchMove,
WebInputEvent::Type::kTouchEnd,
WebInputEvent::Type::kTouchCancel,
};
- WebTouchPoint::State kValidTouchPointStatesForType[4] = {
+ std::array<WebTouchPoint::State, 4> kValidTouchPointStatesForType = {
WebTouchPoint::State::kStatePressed,
WebTouchPoint::State::kStateMoved,
WebTouchPoint::State::kStateReleased,
diff --git a/components/password_manager/core/browser/password_store/password_store_unittest.cc b/components/password_manager/core/browser/password_store/password_store_unittest.cc
index 3dff396..35d4eee1 100644
--- a/components/password_manager/core/browser/password_store/password_store_unittest.cc
+++ b/components/password_manager/core/browser/password_store/password_store_unittest.cc
@@ -6,6 +6,7 @@
#include <stddef.h>
+#include <array>
#include <memory>
#include <utility>
@@ -249,7 +250,7 @@
TEST_F(PasswordStoreTest, UpdateLoginPrimaryKeyFields) {
base::HistogramTester histogram_tester;
/* clang-format off */
- static const PasswordFormData kTestCredentials[] = {
+ static const auto kTestCredentials = std::to_array<PasswordFormData>({
// The old credential.
{PasswordForm::Scheme::kHtml,
kTestWebRealm1,
@@ -263,7 +264,7 @@
kTestWebOrigin2,
"", u"", u"username_element_2", u"password_element_2",
u"username_value_2",
- u"", kTestLastUsageTime, 1}};
+ u"", kTestLastUsageTime, 1}});
/* clang-format on */
scoped_refptr<PasswordStore> store = CreatePasswordStore();
diff --git a/components/password_manager/core/browser/ui/credential_ui_entry_unittest.cc b/components/password_manager/core/browser/ui/credential_ui_entry_unittest.cc
index e1d42add..343eee5 100644
--- a/components/password_manager/core/browser/ui/credential_ui_entry_unittest.cc
+++ b/components/password_manager/core/browser/ui/credential_ui_entry_unittest.cc
@@ -4,6 +4,7 @@
#include "components/password_manager/core/browser/ui/credential_ui_entry.h"
+#include <array>
#include <vector>
#include "base/strings/utf_string_conversions.h"
@@ -189,7 +190,8 @@
TEST(CredentialUIEntryTest,
CredentialUIEntryFromFormsVectorWithDifferentNotes) {
std::vector<PasswordForm> forms;
- const std::u16string kNotes[] = {u"Note", u"", u"Another note"};
+ const auto kNotes =
+ std::to_array<std::u16string>({u"Note", u"", u"Another note"});
for (const auto& kNote : kNotes) {
PasswordForm form;
diff --git a/components/query_parser/query_parser_unittest.cc b/components/query_parser/query_parser_unittest.cc
index ee81064..4dde88f 100644
--- a/components/query_parser/query_parser_unittest.cc
+++ b/components/query_parser/query_parser_unittest.cc
@@ -11,6 +11,8 @@
#include <stddef.h>
+#include <array>
+
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -78,12 +80,12 @@
}
TEST_F(QueryParserTest, NumWords) {
- TestData data[] = {
- { "blah", 1 },
- { "foo \"bar baz\"", 3 },
- { "foo \"baz\"", 2 },
- { "foo \"bar baz\" blah", 4 },
- };
+ auto data = std::to_array<TestData>({
+ {"blah", 1},
+ {"foo \"bar baz\"", 3},
+ {"foo \"baz\"", 2},
+ {"foo \"bar baz\" blah", 4},
+ });
for (size_t i = 0; i < std::size(data); ++i) {
std::u16string query_string;
@@ -103,24 +105,25 @@
const size_t m1_end;
const size_t m2_start;
const size_t m2_end;
- } data[] = {
- { "foo", "fooey foo", true, 0, 3, 6, 9 },
- { "foo foo", "foo", true, 0, 3, 0, 0 },
- { "foo fooey", "fooey", true, 0, 5, 0, 0 },
- { "fooey foo", "fooey", true, 0, 5, 0, 0 },
- { "foo fooey bar", "bar fooey", true, 0, 3, 4, 9 },
- { "blah", "blah", true, 0, 4, 0, 0 },
- { "blah", "foo", false, 0, 0, 0, 0 },
- { "blah", "blahblah", true, 0, 4, 0, 0 },
- { "blah", "foo blah", true, 4, 8, 0, 0 },
- { "foo blah", "blah", false, 0, 0, 0, 0 },
- { "foo blah", "blahx foobar", true, 0, 4, 6, 9 },
- { "\"foo blah\"", "foo blah", true, 0, 8, 0, 0 },
- { "\"foo blah\"", "foox blahx", false, 0, 0, 0, 0 },
- { "\"foo blah\"", "foo blah", true, 0, 8, 0, 0 },
- { "\"foo blah\"", "\"foo blah\"", true, 1, 9, 0, 0 },
- { "foo blah", "\"foo bar blah\"", true, 1, 4, 9, 13 },
};
+ auto data = std::to_array<TestData2>({
+ {"foo", "fooey foo", true, 0, 3, 6, 9},
+ {"foo foo", "foo", true, 0, 3, 0, 0},
+ {"foo fooey", "fooey", true, 0, 5, 0, 0},
+ {"fooey foo", "fooey", true, 0, 5, 0, 0},
+ {"foo fooey bar", "bar fooey", true, 0, 3, 4, 9},
+ {"blah", "blah", true, 0, 4, 0, 0},
+ {"blah", "foo", false, 0, 0, 0, 0},
+ {"blah", "blahblah", true, 0, 4, 0, 0},
+ {"blah", "foo blah", true, 4, 8, 0, 0},
+ {"foo blah", "blah", false, 0, 0, 0, 0},
+ {"foo blah", "blahx foobar", true, 0, 4, 6, 9},
+ {"\"foo blah\"", "foo blah", true, 0, 8, 0, 0},
+ {"\"foo blah\"", "foox blahx", false, 0, 0, 0, 0},
+ {"\"foo blah\"", "foo blah", true, 0, 8, 0, 0},
+ {"\"foo blah\"", "\"foo blah\"", true, 1, 9, 0, 0},
+ {"foo blah", "\"foo bar blah\"", true, 1, 4, 9, 13},
+ });
for (size_t i = 0; i < std::size(data); ++i) {
query_parser::QueryNodeVector query_nodes;
QueryParser::ParseQueryNodes(base::UTF8ToUTF16(data[i].query),
@@ -151,12 +154,13 @@
const std::string w2;
const std::string w3;
const size_t word_count;
- } data[] = {
- { "foo", "foo", "", "", 1 },
- { "foo bar", "foo", "bar", "", 2 },
- { "\"foo bar\"", "foo", "bar", "", 2 },
- { "\"foo bar\" a", "foo", "bar", "a", 3 },
};
+ auto data = std::to_array<TestData2>({
+ {"foo", "foo", "", "", 1},
+ {"foo bar", "foo", "bar", "", 2},
+ {"\"foo bar\"", "foo", "bar", "", 2},
+ {"\"foo bar\" a", "foo", "bar", "a", 3},
+ });
for (size_t i = 0; i < std::size(data); ++i) {
std::vector<std::u16string> results;
QueryParser::ParseQueryWords(base::UTF8ToUTF16(data[i].text),
@@ -175,7 +179,8 @@
const std::string query;
const std::string find_in_text;
const bool matches;
- } data[] = {
+ };
+ auto data = std::to_array<TestData2>({
// Trivial cases.
{"blah", "blah", true},
{"blah", "foo", false},
@@ -198,7 +203,7 @@
{"\"foo blah\"", "foox blahx", false},
{"\"foo blah\"", "\"foo blah\"", true},
{"foo blah", "\"foo bar blah\"", true},
- };
+ });
for (size_t i = 0; i < std::size(data); ++i) {
SCOPED_TRACE(::testing::Message()
<< " Testing case i=" << i << " query=" << data[i].query
diff --git a/components/query_parser/snippet_unittest.cc b/components/query_parser/snippet_unittest.cc
index c5477d5ad..a1714c6 100644
--- a/components/query_parser/snippet_unittest.cc
+++ b/components/query_parser/snippet_unittest.cc
@@ -12,6 +12,7 @@
#include <stddef.h>
#include <algorithm>
+#include <array>
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
@@ -234,18 +235,19 @@
const std::string offsets_string;
const size_t expected_match_count;
const size_t expected_matches[10];
- } data[] = {
- { "0 0 1 2 0 0 4 1 0 0 1 5", 1, { 1, 6 } },
- { "0 0 1 4 0 0 2 1", 1, { 1, 5 } },
- { "0 0 4 1 0 0 2 1", 2, { 2, 3, 4, 5 } },
- { "0 0 0 1", 1, { 0, 1 } },
- { "0 0 0 1 0 0 0 2", 1, { 0, 2 } },
- { "0 0 1 1 0 0 1 2", 1, { 1, 3 } },
- { "0 0 1 2 0 0 4 3 0 0 3 1", 1, { 1, 7 } },
- { "0 0 1 4 0 0 2 5", 1, { 1, 7 } },
- { "0 0 1 2 0 0 1 1", 1, { 1, 3 } },
- { "0 0 1 1 0 0 5 2 0 0 10 1 0 0 3 10", 2, { 1, 2, 3, 13 } },
};
+ auto data = std::to_array<TestData>({
+ {"0 0 1 2 0 0 4 1 0 0 1 5", 1, {1, 6}},
+ {"0 0 1 4 0 0 2 1", 1, {1, 5}},
+ {"0 0 4 1 0 0 2 1", 2, {2, 3, 4, 5}},
+ {"0 0 0 1", 1, {0, 1}},
+ {"0 0 0 1 0 0 0 2", 1, {0, 2}},
+ {"0 0 1 1 0 0 1 2", 1, {1, 3}},
+ {"0 0 1 2 0 0 4 3 0 0 3 1", 1, {1, 7}},
+ {"0 0 1 4 0 0 2 5", 1, {1, 7}},
+ {"0 0 1 2 0 0 1 1", 1, {1, 3}},
+ {"0 0 1 1 0 0 5 2 0 0 10 1 0 0 3 10", 2, {1, 2, 3, 13}},
+ });
for (size_t i = 0; i < std::size(data); ++i) {
Snippet::MatchPositions matches;
Snippet::ExtractMatchPositions(data[i].offsets_string, "0", &matches);
diff --git a/components/safe_search_api/safe_search/safe_search_url_checker_client_unittest.cc b/components/safe_search_api/safe_search/safe_search_url_checker_client_unittest.cc
index d80bdb5..fd1878a2 100644
--- a/components/safe_search_api/safe_search/safe_search_url_checker_client_unittest.cc
+++ b/components/safe_search_api/safe_search/safe_search_url_checker_client_unittest.cc
@@ -2,13 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifdef UNSAFE_BUFFERS_BUILD
-// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
-#pragma allow_unsafe_buffers
-#endif
-
#include "components/safe_search_api/safe_search/safe_search_url_checker_client.h"
+#include <array>
#include <memory>
#include <utility>
@@ -47,13 +43,17 @@
return result;
}
-const char* kURLs[] = {
- "http://www.randomsite1.com", "http://www.randomsite2.com",
- "http://www.randomsite3.com", "http://www.randomsite4.com",
- "http://www.randomsite5.com", "http://www.randomsite6.com",
- "http://www.randomsite7.com", "http://www.randomsite8.com",
+auto kURLs = std::to_array<const char*>({
+ "http://www.randomsite1.com",
+ "http://www.randomsite2.com",
+ "http://www.randomsite3.com",
+ "http://www.randomsite4.com",
+ "http://www.randomsite5.com",
+ "http://www.randomsite6.com",
+ "http://www.randomsite7.com",
+ "http://www.randomsite8.com",
"http://www.randomsite9.com",
-};
+});
} // namespace
diff --git a/components/safe_search_api/url_checker_unittest.cc b/components/safe_search_api/url_checker_unittest.cc
index 7e75163..3b2737d 100644
--- a/components/safe_search_api/url_checker_unittest.cc
+++ b/components/safe_search_api/url_checker_unittest.cc
@@ -2,16 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifdef UNSAFE_BUFFERS_BUILD
-// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
-#pragma allow_unsafe_buffers
-#endif
-
#include "components/safe_search_api/url_checker.h"
#include <stddef.h>
#include <algorithm>
+#include <array>
#include <iterator>
#include <map>
#include <memory>
@@ -38,13 +34,17 @@
constexpr size_t kCacheSize = 2;
-const char* kURLs[] = {
- "http://www.randomsite1.com", "http://www.randomsite2.com",
- "http://www.randomsite3.com", "http://www.randomsite4.com",
- "http://www.randomsite5.com", "http://www.randomsite6.com",
- "http://www.randomsite7.com", "http://www.randomsite8.com",
+auto kURLs = std::to_array<const char*>({
+ "http://www.randomsite1.com",
+ "http://www.randomsite2.com",
+ "http://www.randomsite3.com",
+ "http://www.randomsite4.com",
+ "http://www.randomsite5.com",
+ "http://www.randomsite6.com",
+ "http://www.randomsite7.com",
+ "http://www.randomsite8.com",
"http://www.randomsite9.com",
-};
+});
ClientClassification ToAPIClassification(Classification classification,
bool uncertain) {
diff --git a/components/url_formatter/url_fixer_unittest.cc b/components/url_formatter/url_fixer_unittest.cc
index 3a56b208..f4a944e 100644
--- a/components/url_formatter/url_fixer_unittest.cc
+++ b/components/url_formatter/url_fixer_unittest.cc
@@ -12,6 +12,7 @@
#include <stddef.h>
#include <stdlib.h>
+#include <array>
#include <string>
#include "base/base_paths.h"
@@ -39,7 +40,7 @@
const url::Component ref;
};
-static const SegmentCase segment_cases[] = {
+const auto segment_cases = std::to_array<SegmentCase>({
{
"http://www.google.com/", "http", url::Component(0, 4), // scheme
url::Component(), // username
@@ -268,7 +269,7 @@
url::Component(43, 17), // query
url::Component(), // ref
},
-};
+});
typedef testing::Test URLFixerTest;
diff --git a/components/url_formatter/url_formatter_unittest.cc b/components/url_formatter/url_formatter_unittest.cc
index a88fc75..9876db3 100644
--- a/components/url_formatter/url_formatter_unittest.cc
+++ b/components/url_formatter/url_formatter_unittest.cc
@@ -12,6 +12,7 @@
#include <stddef.h>
#include <string.h>
+#include <array>
#include <vector>
#include "base/logging.h"
@@ -82,7 +83,7 @@
TEST(UrlFormatterTest, FormatUrl) {
FormatUrlTypes default_format_type = kFormatUrlOmitUsernamePassword;
// clang-format off
- const UrlTestData tests[] = {
+ const auto tests = std::to_array<UrlTestData>({
{"Empty URL", "", default_format_type, base::UnescapeRule::NORMAL, L"", 0},
{"Simple URL", "http://www.google.com/", default_format_type,
@@ -424,7 +425,7 @@
kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
base::UnescapeRule::NORMAL,
L"file:///Users/homedirname/folder/file.pdf/", 7},
- };
+ });
// clang-format on
for (size_t i = 0; i < std::size(tests); ++i) {
diff --git a/components/url_matcher/url_matcher_factory_unittest.cc b/components/url_matcher/url_matcher_factory_unittest.cc
index fbed100..815fe9c 100644
--- a/components/url_matcher/url_matcher_factory_unittest.cc
+++ b/components/url_matcher/url_matcher_factory_unittest.cc
@@ -2,15 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifdef UNSAFE_BUFFERS_BUILD
-// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
-#pragma allow_unsafe_buffers
-#endif
-
#include "components/url_matcher/url_matcher_factory.h"
#include <stddef.h>
+#include <array>
#include <memory>
#include <utility>
@@ -151,9 +147,13 @@
base::Value::Dict invalid_condition5;
invalid_condition5.Set(keys::kSchemesKey, std::move(scheme_list));
- const base::Value::Dict* invalid_conditions[] = {
- &invalid_condition1, &invalid_condition2, &invalid_condition3,
- &invalid_condition4, &invalid_condition5};
+ auto invalid_conditions = std::to_array<const base::Value::Dict*>({
+ &invalid_condition1,
+ &invalid_condition2,
+ &invalid_condition3,
+ &invalid_condition4,
+ &invalid_condition5,
+ });
for (size_t i = 0; i < std::size(invalid_conditions); ++i) {
error.clear();
@@ -296,45 +296,49 @@
kIsPathCaseSensitive ||
kIsQueryCaseSensitive;
- const UrlConditionCaseTest case_tests[] = {
- UrlConditionCaseTest(keys::kSchemesKey, true, kScheme, kSchemeUpper,
- kIsSchemeCaseSensitive, kIsSchemeLowerCaseEnforced,
- url),
- UrlConditionCaseTest(keys::kHostContainsKey, false, kHost, kHostUpper,
- kIsHostCaseSensitive, kIsHostLowerCaseEnforced, url),
- UrlConditionCaseTest(keys::kHostEqualsKey, false, kHost, kHostUpper,
- kIsHostCaseSensitive, kIsHostLowerCaseEnforced, url),
- UrlConditionCaseTest(keys::kHostPrefixKey, false, kHost, kHostUpper,
- kIsHostCaseSensitive, kIsHostLowerCaseEnforced, url),
- UrlConditionCaseTest(keys::kHostSuffixKey, false, kHost, kHostUpper,
- kIsHostCaseSensitive, kIsHostLowerCaseEnforced, url),
- UrlConditionCaseTest(keys::kPathContainsKey, false, kPath, kPathUpper,
- kIsPathCaseSensitive, kIsPathLowerCaseEnforced, url),
- UrlConditionCaseTest(keys::kPathEqualsKey, false, kPath, kPathUpper,
- kIsPathCaseSensitive, kIsPathLowerCaseEnforced, url),
- UrlConditionCaseTest(keys::kPathPrefixKey, false, kPath, kPathUpper,
- kIsPathCaseSensitive, kIsPathLowerCaseEnforced, url),
- UrlConditionCaseTest(keys::kPathSuffixKey, false, kPath, kPathUpper,
- kIsPathCaseSensitive, kIsPathLowerCaseEnforced, url),
- UrlConditionCaseTest(keys::kQueryContainsKey, false, kQuery, kQueryUpper,
- kIsQueryCaseSensitive, kIsQueryLowerCaseEnforced, url),
- UrlConditionCaseTest(keys::kQueryEqualsKey, false, kQuery, kQueryUpper,
- kIsQueryCaseSensitive, kIsQueryLowerCaseEnforced, url),
- UrlConditionCaseTest(keys::kQueryPrefixKey, false, kQuery, kQueryUpper,
- kIsQueryCaseSensitive, kIsQueryLowerCaseEnforced, url),
- UrlConditionCaseTest(keys::kQuerySuffixKey, false, kQuery, kQueryUpper,
- kIsQueryCaseSensitive, kIsQueryLowerCaseEnforced, url),
- // Excluding kURLMatchesKey because case sensitivity can be specified in the
- // RE2 expression.
- UrlConditionCaseTest(keys::kURLContainsKey, false, kUrl, kUrlUpper,
- kIsUrlCaseSensitive, kIsUrlLowerCaseEnforced, url),
- UrlConditionCaseTest(keys::kURLEqualsKey, false, kUrl, kUrlUpper,
- kIsUrlCaseSensitive, kIsUrlLowerCaseEnforced, url),
- UrlConditionCaseTest(keys::kURLPrefixKey, false, kUrl, kUrlUpper,
- kIsUrlCaseSensitive, kIsUrlLowerCaseEnforced, url),
- UrlConditionCaseTest(keys::kURLSuffixKey, false, kUrl, kUrlUpper,
- kIsUrlCaseSensitive, kIsUrlLowerCaseEnforced, url),
- };
+ const auto case_tests = std::to_array<UrlConditionCaseTest>({
+ UrlConditionCaseTest(keys::kSchemesKey, true, kScheme, kSchemeUpper,
+ kIsSchemeCaseSensitive, kIsSchemeLowerCaseEnforced,
+ url),
+ UrlConditionCaseTest(keys::kHostContainsKey, false, kHost, kHostUpper,
+ kIsHostCaseSensitive, kIsHostLowerCaseEnforced, url),
+ UrlConditionCaseTest(keys::kHostEqualsKey, false, kHost, kHostUpper,
+ kIsHostCaseSensitive, kIsHostLowerCaseEnforced, url),
+ UrlConditionCaseTest(keys::kHostPrefixKey, false, kHost, kHostUpper,
+ kIsHostCaseSensitive, kIsHostLowerCaseEnforced, url),
+ UrlConditionCaseTest(keys::kHostSuffixKey, false, kHost, kHostUpper,
+ kIsHostCaseSensitive, kIsHostLowerCaseEnforced, url),
+ UrlConditionCaseTest(keys::kPathContainsKey, false, kPath, kPathUpper,
+ kIsPathCaseSensitive, kIsPathLowerCaseEnforced, url),
+ UrlConditionCaseTest(keys::kPathEqualsKey, false, kPath, kPathUpper,
+ kIsPathCaseSensitive, kIsPathLowerCaseEnforced, url),
+ UrlConditionCaseTest(keys::kPathPrefixKey, false, kPath, kPathUpper,
+ kIsPathCaseSensitive, kIsPathLowerCaseEnforced, url),
+ UrlConditionCaseTest(keys::kPathSuffixKey, false, kPath, kPathUpper,
+ kIsPathCaseSensitive, kIsPathLowerCaseEnforced, url),
+ UrlConditionCaseTest(keys::kQueryContainsKey, false, kQuery, kQueryUpper,
+ kIsQueryCaseSensitive, kIsQueryLowerCaseEnforced,
+ url),
+ UrlConditionCaseTest(keys::kQueryEqualsKey, false, kQuery, kQueryUpper,
+ kIsQueryCaseSensitive, kIsQueryLowerCaseEnforced,
+ url),
+ UrlConditionCaseTest(keys::kQueryPrefixKey, false, kQuery, kQueryUpper,
+ kIsQueryCaseSensitive, kIsQueryLowerCaseEnforced,
+ url),
+ UrlConditionCaseTest(keys::kQuerySuffixKey, false, kQuery, kQueryUpper,
+ kIsQueryCaseSensitive, kIsQueryLowerCaseEnforced,
+ url),
+ // Excluding kURLMatchesKey because case sensitivity can be specified in
+ // the RE2 expression.
+ UrlConditionCaseTest(keys::kURLContainsKey, false, kUrl, kUrlUpper,
+ kIsUrlCaseSensitive, kIsUrlLowerCaseEnforced, url),
+ UrlConditionCaseTest(keys::kURLEqualsKey, false, kUrl, kUrlUpper,
+ kIsUrlCaseSensitive, kIsUrlLowerCaseEnforced, url),
+ UrlConditionCaseTest(keys::kURLPrefixKey, false, kUrl, kUrlUpper,
+ kIsUrlCaseSensitive, kIsUrlLowerCaseEnforced, url),
+ UrlConditionCaseTest(keys::kURLSuffixKey, false, kUrl, kUrlUpper,
+ kIsUrlCaseSensitive, kIsUrlLowerCaseEnforced, url),
+ });
for (size_t i = 0; i < std::size(case_tests); ++i) {
SCOPED_TRACE(base::StringPrintf("Iteration: %" PRIuS, i));
diff --git a/components/url_matcher/url_matcher_unittest.cc b/components/url_matcher/url_matcher_unittest.cc
index 29ddcbc..50c39134 100644
--- a/components/url_matcher/url_matcher_unittest.cc
+++ b/components/url_matcher/url_matcher_unittest.cc
@@ -2,15 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifdef UNSAFE_BUFFERS_BUILD
-// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
-#pragma allow_unsafe_buffers
-#endif
-
#include "components/url_matcher/url_matcher.h"
#include <stddef.h>
+#include <array>
#include <memory>
#include <utility>
@@ -186,7 +182,7 @@
MatcherStringPattern p1("foobar.com", 1);
MatcherStringPattern p2("foobar.com", 2);
// The first component of each test is expected to be < than the second.
- URLMatcherCondition test_smaller[][2] = {
+ auto test_smaller = std::to_array<std::array<URLMatcherCondition, 2>>({
{URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1),
URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, &p1)},
{URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1),
@@ -195,19 +191,19 @@
URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p2)},
{URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1),
URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX, nullptr)},
- };
+ });
for (size_t i = 0; i < std::size(test_smaller); ++i) {
EXPECT_TRUE(test_smaller[i][0] < test_smaller[i][1])
<< "Test " << i << " of test_smaller failed";
EXPECT_FALSE(test_smaller[i][1] < test_smaller[i][0])
<< "Test " << i << " of test_smaller failed";
}
- URLMatcherCondition test_equal[][2] = {
+ auto test_equal = std::to_array<std::array<URLMatcherCondition, 2>>({
{URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1),
URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, &p1)},
{URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, nullptr),
URLMatcherCondition(URLMatcherCondition::HOST_PREFIX, nullptr)},
- };
+ });
for (size_t i = 0; i < std::size(test_equal); ++i) {
EXPECT_FALSE(test_equal[i][0] < test_equal[i][1])
<< "Test " << i << " of test_equal failed";