From 7c30aebe2d0771244e6b3a416ed8d461761821a9 Mon Sep 17 00:00:00 2001 From: Christian Blichmann Date: Thu, 5 Nov 2020 01:48:46 -0800 Subject: [PATCH] Use Abseil hash maps instead of `std::map` PiperOrigin-RevId: 340807499 Change-Id: I2689bd1d32be45e3085dcc7a0ba4b8fedd7d53b0 --- sandboxed_api/sandbox2/BUILD.bazel | 1 + sandboxed_api/sandbox2/CMakeLists.txt | 1 + sandboxed_api/sandbox2/client.cc | 14 +++++--------- sandboxed_api/sandbox2/client.h | 4 ++-- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/sandboxed_api/sandbox2/BUILD.bazel b/sandboxed_api/sandbox2/BUILD.bazel index 839a7e0..11bb0bc 100644 --- a/sandboxed_api/sandbox2/BUILD.bazel +++ b/sandboxed_api/sandbox2/BUILD.bazel @@ -343,6 +343,7 @@ cc_library( "//sandboxed_api/sandbox2/util:strerror", "//sandboxed_api/util:raw_logging", "@com_google_absl//absl/base:core_headers", + "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/memory", "@com_google_absl//absl/strings", ], diff --git a/sandboxed_api/sandbox2/CMakeLists.txt b/sandboxed_api/sandbox2/CMakeLists.txt index e6a746b..5b64f9e 100644 --- a/sandboxed_api/sandbox2/CMakeLists.txt +++ b/sandboxed_api/sandbox2/CMakeLists.txt @@ -328,6 +328,7 @@ add_library(sandbox2_client STATIC add_library(sandbox2::client ALIAS sandbox2_client) target_link_libraries(sandbox2_client PRIVATE absl::core_headers + absl::flat_hash_map absl::memory absl::strings sandbox2::logsink diff --git a/sandboxed_api/sandbox2/client.cc b/sandboxed_api/sandbox2/client.cc index 5a93020..01f2204 100644 --- a/sandboxed_api/sandbox2/client.cc +++ b/sandboxed_api/sandbox2/client.cc @@ -46,22 +46,18 @@ namespace sandbox2 { -constexpr uint32_t Client::kClient2SandboxReady; -constexpr uint32_t Client::kSandbox2ClientDone; -constexpr const char* Client::kFDMapEnvVar; - Client::Client(Comms* comms) : comms_(comms) { char* fdmap_envvar = getenv(kFDMapEnvVar); if (!fdmap_envvar) { return; } - std::map vars = + absl::flat_hash_map vars = absl::StrSplit(fdmap_envvar, ',', absl::SkipEmpty()); - for (const auto& var : vars) { + for (const auto& [name, mapped_fd] : vars) { int fd; - SAPI_RAW_CHECK(absl::SimpleAtoi(var.second, &fd), "failed to parse fd map"); - SAPI_RAW_CHECK(fd_map_.emplace(std::string{var.first}, fd).second, - "could not insert mapping into fd map (duplicate)"); + SAPI_RAW_CHECK(absl::SimpleAtoi(mapped_fd, &fd), "failed to parse fd map"); + SAPI_RAW_CHECK(fd_map_.emplace(std::string(name), fd).second, + "could not insert mapping into fd map (duplicate)"); } unsetenv(kFDMapEnvVar); } diff --git a/sandboxed_api/sandbox2/client.h b/sandboxed_api/sandbox2/client.h index 9cf63d1..8b1d0aa 100644 --- a/sandboxed_api/sandbox2/client.h +++ b/sandboxed_api/sandbox2/client.h @@ -19,10 +19,10 @@ #define SANDBOXED_API_SANDBOX2_CLIENT_H_ #include -#include #include #include +#include "absl/container/flat_hash_map.h" #include "sandboxed_api/sandbox2/comms.h" #include "sandboxed_api/sandbox2/logsink.h" #include "sandboxed_api/sandbox2/network_proxy/client.h" @@ -87,7 +87,7 @@ class Client { // In the pre-execve case, the sandboxee has to pass the information about // file descriptors to the new process. We set an environment variable for // this case that is parsed in the Client constructor if present. - std::map fd_map_; + absl::flat_hash_map fd_map_; std::string GetFdMapEnvVar() const;