|
20 | 20 | #include <string> |
21 | 21 | #include <thread> |
22 | 22 |
|
| 23 | +#include <fmt/format.h> |
23 | 24 | #include <folly/lang/Assume.h> |
24 | 25 | #include <memcached/config_parser.h> |
25 | 26 | #include <memcached/engine.h> |
@@ -71,24 +72,32 @@ class CrashEngine : public EngineIface { |
71 | 72 | * means crashing it. |
72 | 73 | */ |
73 | 74 | cb::engine_errc initialize(const std::string&) override { |
74 | | - std::string mode_string(getenv("MEMCACHED_CRASH_TEST")); |
| 75 | + using namespace std::string_view_literals; |
| 76 | + const auto* ptr = getenv("MEMCACHED_CRASH_TEST"); |
| 77 | + if (!ptr) { |
| 78 | + fmt::print( |
| 79 | + stderr, |
| 80 | + "crash_engine::initialize: MEMCACHED_CRASH_TEST not set\n"); |
| 81 | + std::_Exit(EXIT_FAILURE); |
| 82 | + } |
| 83 | + std::string_view mode_string(ptr); |
75 | 84 | CrashMode mode; |
76 | | - if (mode_string == "segfault") { |
| 85 | + if (mode_string == "segfault"sv) { |
77 | 86 | mode = CrashMode::SegFault; |
78 | | - } else if (mode_string == "std_exception") { |
| 87 | + } else if (mode_string == "std_exception"sv) { |
79 | 88 | mode = CrashMode::UncaughtStdException; |
80 | | - } else if (mode_string == "std_exception_via_std_thread") { |
| 89 | + } else if (mode_string == "std_exception_via_std_thread"sv) { |
81 | 90 | mode = CrashMode::UncaughtStdExceptionViaStdThread; |
82 | | - } else if (mode_string == "std_exception_with_trace") { |
| 91 | + } else if (mode_string == "std_exception_with_trace"sv) { |
83 | 92 | mode = CrashMode::UncaughtStdExceptionWithTrace; |
84 | | - } else if (mode_string == "unknown_exception") { |
| 93 | + } else if (mode_string == "unknown_exception"sv) { |
85 | 94 | mode = CrashMode::UncaughtUnknownException; |
86 | 95 | } else { |
87 | | - fprintf(stderr, |
88 | | - "crash_engine::initialize: could not find a valid " |
89 | | - "CrashMode from MEMCACHED_CRASH_TEST env var ('%s')\n", |
90 | | - mode_string.c_str()); |
91 | | - exit(1); |
| 96 | + fmt::print(stderr, |
| 97 | + "crash_engine::initialize: could not find a valid " |
| 98 | + "CrashMode from MEMCACHED_CRASH_TEST env var ({:?})\n", |
| 99 | + mode_string); |
| 100 | + exit(EXIT_FAILURE); |
92 | 101 | } |
93 | 102 | if (mode == CrashMode::UncaughtStdExceptionViaStdThread) { |
94 | 103 | std::thread thread{[mode] { recursive_crash_function(25, mode); }}; |
|
0 commit comments