Skip to content

Commit 6b4de25

Browse files
committed
Use find in Defaults::Value() to check for presence
`at` throws an exception when the value is missing. `at` is not recommended. https://abseil.io/tips/224 has similar advice on `std::vector::at`, go/totw/132#safe-lookup recommends a specific alternative for maps. This fixes the error ``` libc++abi: terminating due to uncaught exception of type std::out_of_range: absl::btree_map::at ``` Full invocation: ``` $ bazel run //cuttlefish/package:cvd -- fetch --default_build=git_main/aosp_cf_x86_64_only_phone-trunk_staging-userdebug --target_directory=$HOME/dl $ bazel run //cuttlefish/package:cvd -- create --host_path=$HOME/dl --product_path=$HOME/dl --host_substitutions=bin/assemble_cvd,bin/run_cvd ... SetFlagDefaultsFromConfig: No flag defaults to override. libc++abi: terminating due to uncaught exception of type std::out_of_range: absl::btree_map::at 11-18 16:15:21.590 3669390 3669390 E cvd_internal_start: subprocess.cpp:207 Subprocess 3669416 was interrupted by a signal 'Aborted' (6) 11-18 16:15:21.591 3669390 3669390 E cvd_internal_start: main.cc:296 assemble_cvd returned -1 11-18 16:15:21.591 3667884 3667884 I cvd : start.cpp:547 Device launch failed, cleaning up ``` Bug: b/461899525
1 parent 513a4c4 commit 6b4de25

File tree

1 file changed

+8
-1
lines changed
  • base/cvd/cuttlefish/host/libs/config/defaults

1 file changed

+8
-1
lines changed

base/cvd/cuttlefish/host/libs/config/defaults/defaults.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <string_view>
2020

2121
#include <android-base/file.h>
22+
#include "absl/container/btree_map.h"
2223

2324
#include "cuttlefish/host/libs/config/defaults/defaults.h"
2425
#include "cuttlefish/common/libs/key_equals_value/key_equals_value.h"
@@ -33,7 +34,13 @@ Defaults::Defaults(std::map<std::string, std::string> defaults) {
3334
}
3435

3536
std::optional<std::string_view> Defaults::Value(std::string_view k) const {
36-
return defaults_.at(k);
37+
absl::btree_map<std::string, std::string>::const_iterator it =
38+
defaults_.find(k);
39+
if (it == defaults_.end()) {
40+
return {};
41+
} else {
42+
return it->second;
43+
}
3744
}
3845

3946
std::optional<bool> Defaults::BoolValue(std::string_view k) const {

0 commit comments

Comments
 (0)