diff options
author | Matt Diener <matt.diener@mongodb.com> | 2022-02-23 15:56:51 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-02-23 16:55:57 +0000 |
commit | 359ecf5230af8f0b9db7fb92fecdf68769d95d0e (patch) | |
tree | 9b2e13966543935de45b419f888072d5b160a8a4 | |
parent | d07e0bd356d517729f0e3ccae992a5c41e9d5f74 (diff) | |
download | mongo-359ecf5230af8f0b9db7fb92fecdf68769d95d0e.tar.gz |
SERVER-54595 StringData(const char*, size_t) only checks invariant in debug
-rw-r--r-- | src/mongo/base/string_data.h | 3 | ||||
-rw-r--r-- | src/mongo/base/string_data_test.cpp | 5 |
2 files changed, 6 insertions, 2 deletions
diff --git a/src/mongo/base/string_data.h b/src/mongo/base/string_data.h index fbadb26387b..10c6758689e 100644 --- a/src/mongo/base/string_data.h +++ b/src/mongo/base/string_data.h @@ -41,6 +41,7 @@ #include "mongo/platform/compiler.h" #include "mongo/stdx/type_traits.h" #include "mongo/util/ctype.h" +#include "mongo/util/debug_util.h" #define MONGO_ALLOW_INCLUDE_INVARIANT_H #include "mongo/util/invariant.h" #undef MONGO_ALLOW_INCLUDE_INVARIANT_H @@ -97,7 +98,7 @@ public: * characters in the half-open interval `[c, c + len)` must be valid. */ constexpr StringData(const char* c, size_t len) : StringData(c, len, TrustedInitTag()) { - if (!MONGO_likely(_data || (_size == 0))) + if (MONGO_unlikely(kDebugBuild && !_data && (_size != 0))) invariant(0, "StringData(nullptr,len) requires len==0"); } diff --git a/src/mongo/base/string_data_test.cpp b/src/mongo/base/string_data_test.cpp index 937f03bff09..c31d376c5b4 100644 --- a/src/mongo/base/string_data_test.cpp +++ b/src/mongo/base/string_data_test.cpp @@ -36,6 +36,7 @@ #include "mongo/base/simple_string_data_comparator.h" #include "mongo/base/string_data.h" +#include "mongo/config.h" #include "mongo/unittest/death_test.h" #include "mongo/unittest/unittest.h" @@ -110,11 +111,13 @@ TEST(Construction, Constexpr) { class StringDataDeathTest : public unittest::Test {}; +#if defined(MONGO_CONFIG_DEBUG_BUILD) DEATH_TEST(StringDataDeathTest, InvariantNullRequiresEmpty, "StringData(nullptr,len) requires len==0") { - StringData bad{nullptr, 1}; + [[maybe_unused]] StringData bad{nullptr, 1}; } +#endif TEST(Comparison, BothEmpty) { StringData empty(""); |