Skip to content

Commit

Permalink
Migrate base::{size,empty,data} to STL equivalents in //sandbox.
Browse files Browse the repository at this point in the history
Bug: 1299695
Change-Id: I2ddc1ce93d264f5a6e01bfe4d677a5345b9f0eee
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3492363
Auto-Submit: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Owners-Override: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/main@{#975504}
  • Loading branch information
zetafunction authored and Chromium LUCI CQ committed Feb 27, 2022
1 parent 12554e2 commit f42ae6c
Show file tree
Hide file tree
Showing 17 changed files with 40 additions and 57 deletions.
3 changes: 1 addition & 2 deletions sandbox/linux/bpf_dsl/policy_compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#include "base/bits.h"
#include "base/check_op.h"
#include "base/cxx17_backports.h"
#include "sandbox/linux/bpf_dsl/bpf_dsl.h"
#include "sandbox/linux/bpf_dsl/bpf_dsl_impl.h"
#include "sandbox/linux/bpf_dsl/codegen.h"
Expand Down Expand Up @@ -451,7 +450,7 @@ CodeGen::Node PolicyCompiler::Trap(TrapRegistry::TrapFnc fnc,
}

bool PolicyCompiler::IsRequiredForUnsafeTrap(int sysno) {
for (size_t i = 0; i < base::size(kSyscallsRequiredForUnsafeTraps); ++i) {
for (size_t i = 0; i < std::size(kSyscallsRequiredForUnsafeTraps); ++i) {
if (sysno == kSyscallsRequiredForUnsafeTraps[i]) {
return true;
}
Expand Down
5 changes: 2 additions & 3 deletions sandbox/linux/bpf_dsl/syscall_set_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <stddef.h>
#include <stdint.h>

#include "base/cxx17_backports.h"
#include "sandbox/linux/bpf_dsl/linux_syscall_ranges.h"
#include "sandbox/linux/tests/unit_tests.h"

Expand Down Expand Up @@ -86,12 +85,12 @@ SANDBOX_TEST(SyscallSet, InvalidSyscalls) {
size_t i = 0;
for (uint32_t sysnum : set) {
if (!SyscallSet::IsValid(sysnum)) {
SANDBOX_ASSERT(i < base::size(kExpected));
SANDBOX_ASSERT(i < std::size(kExpected));
SANDBOX_ASSERT(kExpected[i] == sysnum);
++i;
}
}
SANDBOX_ASSERT(i == base::size(kExpected));
SANDBOX_ASSERT(i == std::size(kExpected));
}
}

Expand Down
3 changes: 1 addition & 2 deletions sandbox/linux/bpf_dsl/test_trap_registry_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include <stddef.h>

#include "base/cxx17_backports.h"
#include "base/memory/raw_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"

Expand Down Expand Up @@ -38,7 +37,7 @@ TEST(TestTrapRegistry, TrapIDs) {

// Add traps twice to test that IDs are reused correctly.
for (int i = 0; i < 2; ++i) {
for (size_t j = 0; j < base::size(funcs); ++j) {
for (size_t j = 0; j < std::size(funcs); ++j) {
// Trap IDs start at 1.
EXPECT_EQ(j + 1, traps.Add(funcs[j].fnc, funcs[j].aux, true));
}
Expand Down
3 changes: 1 addition & 2 deletions sandbox/linux/seccomp-bpf-helpers/sigsys_handlers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <unistd.h>

#include "base/check.h"
#include "base/cxx17_backports.h"
#include "base/debug/crash_logging.h"
#include "base/posix/eintr_wrapper.h"
#include "build/build_config.h"
Expand Down Expand Up @@ -183,7 +182,7 @@ void SetSeccompCrashKey(const struct arch_seccomp_data& args) {
memset(crash_key, '\0', crash_key_length);

size_t offset = 0;
for (size_t i = 0; i < base::size(values); ++i) {
for (size_t i = 0; i < std::size(values); ++i) {
const char* strings[2] = { prefixes[i], values[i] };
for (auto* string : strings) {
size_t string_len = strlen(string);
Expand Down
7 changes: 3 additions & 4 deletions sandbox/linux/seccomp-bpf/syscall_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include <vector>

#include "base/cxx17_backports.h"
#include "base/memory/page_size.h"
#include "base/memory/raw_ptr.h"
#include "base/posix/eintr_wrapper.h"
Expand Down Expand Up @@ -97,8 +96,8 @@ intptr_t CopySyscallArgsToAux(const struct arch_seccomp_data& args, void* aux) {
// |aux| is our BPF_AUX pointer.
std::vector<uint64_t>* const seen_syscall_args =
static_cast<std::vector<uint64_t>*>(aux);
BPF_ASSERT(base::size(args.args) == 6);
seen_syscall_args->assign(args.args, args.args + base::size(args.args));
BPF_ASSERT(std::size(args.args) == 6);
seen_syscall_args->assign(args.args, args.args + std::size(args.args));
return -ENOMEM;
}

Expand Down Expand Up @@ -137,7 +136,7 @@ BPF_TEST(Syscall,
// implementation details of kernel BPF filters and we will need to document
// the expected behavior very clearly.
int syscall_args[6];
for (size_t i = 0; i < base::size(syscall_args); ++i) {
for (size_t i = 0; i < std::size(syscall_args); ++i) {
syscall_args[i] = kExpectedValue + i;
}

Expand Down
3 changes: 1 addition & 2 deletions sandbox/linux/services/credentials.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/cxx17_backports.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
Expand Down Expand Up @@ -236,7 +235,7 @@ bool Credentials::HasAnyCapability() {

PCHECK(sys_capget(&hdr, data) == 0);

for (size_t i = 0; i < base::size(data); ++i) {
for (size_t i = 0; i < std::size(data); ++i) {
if (data[i].effective || data[i].permitted || data[i].inheritable) {
return true;
}
Expand Down
5 changes: 2 additions & 3 deletions sandbox/linux/services/namespace_sandbox.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

#include "base/check_op.h"
#include "base/command_line.h"
#include "base/cxx17_backports.h"
#include "base/environment.h"
#include "base/files/scoped_file.h"
#include "base/posix/eintr_wrapper.h"
Expand Down Expand Up @@ -78,7 +77,7 @@ void TerminationSignalHandler(int sig) {
// Return a special exit code so that the process is detected as terminated by
// a signal.
const size_t sig_idx = static_cast<size_t>(sig);
if (sig_idx < base::size(g_signal_exit_codes)) {
if (sig_idx < std::size(g_signal_exit_codes)) {
_exit(g_signal_exit_codes[sig_idx]);
}

Expand Down Expand Up @@ -261,7 +260,7 @@ bool NamespaceSandbox::InstallTerminationSignalHandler(
}

const size_t sig_idx = static_cast<size_t>(sig);
CHECK_LT(sig_idx, base::size(g_signal_exit_codes));
CHECK_LT(sig_idx, std::size(g_signal_exit_codes));

DCHECK_GE(exit_code, 0);
DCHECK_LT(exit_code, 256);
Expand Down
3 changes: 1 addition & 2 deletions sandbox/linux/suid/client/setuid_sandbox_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <utility>

#include "base/command_line.h"
#include "base/cxx17_backports.h"
#include "base/environment.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
Expand Down Expand Up @@ -51,7 +50,7 @@ void UnsetExpectedEnvironmentVariables(base::EnvironmentMap* env_map) {
kSandboxNETNSEnvironmentVarName,
};

for (size_t i = 0; i < base::size(environment_vars); ++i) {
for (size_t i = 0; i < std::size(environment_vars); ++i) {
// Setting values in EnvironmentMap to an empty-string will make
// sure that they get unset from the environment via AlterEnvironment().
(*env_map)[environment_vars[i]] = base::NativeEnvironmentString();
Expand Down
5 changes: 2 additions & 3 deletions sandbox/linux/syscall_broker/broker_process_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "base/callback.h"
#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/cxx17_backports.h"
#include "base/files/file_util.h"
#include "base/files/scoped_file.h"
#include "base/logging.h"
Expand Down Expand Up @@ -608,7 +607,7 @@ SANDBOX_TEST_ALLOW_NOISE(BrokerProcess, MAYBE_RecvMsgDescriptorLeak) {

// Save one FD to send to the broker later, and close the others.
base::ScopedFD message_fd(available_fds[0]);
for (size_t i = 1; i < base::size(available_fds); i++) {
for (size_t i = 1; i < std::size(available_fds); i++) {
SANDBOX_ASSERT(0 == IGNORE_EINTR(close(available_fds[i])));
}

Expand All @@ -618,7 +617,7 @@ SANDBOX_TEST_ALLOW_NOISE(BrokerProcess, MAYBE_RecvMsgDescriptorLeak) {
// be assigned to newly-created descriptors allocated by the process.)
const rlim_t fd_limit =
1 + *std::max_element(available_fds,
available_fds + base::size(available_fds));
available_fds + std::size(available_fds));

struct rlimit rlim;
SANDBOX_ASSERT(0 == getrlimit(RLIMIT_NOFILE, &rlim));
Expand Down
16 changes: 8 additions & 8 deletions sandbox/linux/syscall_broker/broker_simple_message_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ TEST(BrokerSimpleMessage, SendAndRecvMsg) {
ExpectedResultValue* expected_results[] = {&data1_value};

BrokerSimpleMessageTestHelper::RecvMsg(&ipc_reader, expected_results,
base::size(expected_results));
std::size(expected_results));

wait_event.Wait();
}
Expand All @@ -457,7 +457,7 @@ TEST(BrokerSimpleMessage, SendAndRecvMsg) {
ExpectedResultValue* expected_results[] = {&int1_value};

BrokerSimpleMessageTestHelper::RecvMsg(&ipc_reader, expected_results,
base::size(expected_results));
std::size(expected_results));

wait_event.Wait();
}
Expand Down Expand Up @@ -485,7 +485,7 @@ TEST(BrokerSimpleMessage, SendAndRecvMsg) {
ExpectedResultValue* expected_results[] = {&data1_value, &int1_value};

BrokerSimpleMessageTestHelper::RecvMsg(&ipc_reader, expected_results,
base::size(expected_results));
std::size(expected_results));

wait_event.Wait();
}
Expand Down Expand Up @@ -518,7 +518,7 @@ TEST(BrokerSimpleMessage, SendAndRecvMsg) {
&data2_value, &int2_value};

BrokerSimpleMessageTestHelper::RecvMsg(&ipc_reader, expected_results,
base::size(expected_results));
std::size(expected_results));

wait_event.Wait();
}
Expand Down Expand Up @@ -550,7 +550,7 @@ TEST(BrokerSimpleMessage, SendRecvMsgSynchronous) {
FROM_HERE,
base::BindOnce(&BrokerSimpleMessageTestHelper::RecvMsgAndReply,
&ipc_reader, expected_results,
base::size(expected_results), reply_data1, -1));
std::size(expected_results), reply_data1, -1));

PostWaitableEventToThread(&message_thread, &wait_event);

Expand Down Expand Up @@ -583,7 +583,7 @@ TEST(BrokerSimpleMessage, SendRecvMsgSynchronous) {
FROM_HERE,
base::BindOnce(&BrokerSimpleMessageTestHelper::RecvMsgAndReply,
&ipc_reader, expected_results,
base::size(expected_results), reply_data1, -1));
std::size(expected_results), reply_data1, -1));

PostWaitableEventToThread(&message_thread, &wait_event);

Expand Down Expand Up @@ -617,7 +617,7 @@ TEST(BrokerSimpleMessage, SendRecvMsgSynchronous) {
FROM_HERE,
base::BindOnce(&BrokerSimpleMessageTestHelper::RecvMsgAndReply,
&ipc_reader, expected_results,
base::size(expected_results), reply_data1, -1));
std::size(expected_results), reply_data1, -1));

PostWaitableEventToThread(&message_thread, &wait_event);

Expand Down Expand Up @@ -655,7 +655,7 @@ TEST(BrokerSimpleMessage, SendRecvMsgSynchronous) {
FROM_HERE,
base::BindOnce(&BrokerSimpleMessageTestHelper::RecvMsgAndReply,
&ipc_reader, expected_results,
base::size(expected_results), reply_data1, -1));
std::size(expected_results), reply_data1, -1));

PostWaitableEventToThread(&message_thread, &wait_event);

Expand Down
3 changes: 1 addition & 2 deletions sandbox/mac/seatbelt_extension_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <unistd.h>

#include "base/command_line.h"
#include "base/cxx17_backports.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_file.h"
Expand All @@ -32,7 +31,7 @@ const char kSandboxProfile[] = R"(
)";

const char kTestData[] = "hello world";
constexpr int kTestDataLen = base::size(kTestData);
constexpr int kTestDataLen = std::size(kTestData);

const char kSwitchFile[] = "test-file";
const char kSwitchExtension[] = "test-extension";
Expand Down
9 changes: 4 additions & 5 deletions sandbox/policy/win/sandbox_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <vector>

#include "base/command_line.h"
#include "base/cxx17_backports.h"
#include "base/debug/activity_tracker.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
Expand Down Expand Up @@ -216,12 +215,12 @@ bool AddDirectory(int path,
// Compares the loaded |module| file name matches |module_name|.
bool IsExpandedModuleName(HMODULE module, const wchar_t* module_name) {
wchar_t path[MAX_PATH];
DWORD sz = ::GetModuleFileNameW(module, path, base::size(path));
if ((sz == base::size(path)) || (sz == 0)) {
DWORD sz = ::GetModuleFileNameW(module, path, std::size(path));
if ((sz == std::size(path)) || (sz == 0)) {
// XP does not set the last error properly, so we bail out anyway.
return false;
}
if (!::GetLongPathName(path, path, base::size(path)))
if (!::GetLongPathName(path, path, std::size(path)))
return false;
base::FilePath fname(path);
return (fname.BaseName().value() == module_name);
Expand Down Expand Up @@ -283,7 +282,7 @@ void BlocklistAddOneDll(const wchar_t* module_name,
// Eviction of injected DLLs is done by the sandbox so that the injected module
// does not get a chance to execute any code.
void AddGenericDllEvictionPolicy(TargetPolicy* policy) {
for (int ix = 0; ix != base::size(kTroublesomeDlls); ++ix)
for (int ix = 0; ix != std::size(kTroublesomeDlls); ++ix)
BlocklistAddOneDll(kTroublesomeDlls[ix], true, policy);
}

Expand Down
3 changes: 1 addition & 2 deletions sandbox/win/src/filesystem_policy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include <string>

#include "base/cxx17_backports.h"
#include "base/notreached.h"
#include "base/win/scoped_handle.h"
#include "base/win/windows_version.h"
Expand Down Expand Up @@ -346,7 +345,7 @@ std::wstring FixNTPrefixForMatch(const std::wstring& name) {

// NT prefix escaped for rule matcher
const wchar_t kNTPrefixEscaped[] = L"\\/?/?\\";
const int kNTPrefixEscapedLen = base::size(kNTPrefixEscaped) - 1;
const int kNTPrefixEscapedLen = std::size(kNTPrefixEscaped) - 1;

if (0 != mod_name.compare(0, kNTPrefixLen, kNTPrefix)) {
if (0 != mod_name.compare(0, kNTPrefixEscapedLen, kNTPrefixEscaped)) {
Expand Down
6 changes: 2 additions & 4 deletions sandbox/win/src/ipc_leak_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdlib.h>
#include <windows.h>

#include <memory>

#include <stdlib.h>

#include "base/cxx17_backports.h"
#include "base/memory/page_size.h"
#include "base/win/win_util.h"
#include "sandbox/win/src/crosscall_client.h"
Expand Down Expand Up @@ -274,7 +272,7 @@ TEST(IPCTest, IPCLeak) {
{TESTIPC_CREATENAMEDPIPEW, "TESTIPC_CREATENAMEDPIPEW",
INVALID_HANDLE_VALUE}};

static_assert(base::size(test_data) == TESTIPC_LAST, "Not enough tests.");
static_assert(std::size(test_data) == TESTIPC_LAST, "Not enough tests.");
for (auto test : test_data) {
TestRunner runner;
std::wstring command = std::wstring(L"IPC_Leak ");
Expand Down
5 changes: 2 additions & 3 deletions sandbox/win/src/sandbox_nt_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <memory>
#include <vector>

#include "base/cxx17_backports.h"
#include "base/files/file.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
Expand Down Expand Up @@ -286,8 +285,8 @@ TEST(SandboxNtUtil, CopyNameAndAttributes) {
sandbox::CopyNameAndAttributes(&object_attributes, &name, &name_len,
&attributes));
EXPECT_EQ(object_attributes.Attributes, attributes);
EXPECT_EQ(base::size(name_buffer), name_len);
EXPECT_EQ(0, wcsncmp(name.get(), name_buffer, base::size(name_buffer)));
EXPECT_EQ(std::size(name_buffer), name_len);
EXPECT_EQ(0, wcsncmp(name.get(), name_buffer, std::size(name_buffer)));
EXPECT_EQ(L'\0', name.get()[name_len]);
}

Expand Down
Loading

0 comments on commit f42ae6c

Please sign in to comment.