diff options
author | swarmer <swarmer@yandex-team.com> | 2024-09-18 04:57:23 +0300 |
---|---|---|
committer | swarmer <swarmer@yandex-team.com> | 2024-09-18 05:07:56 +0300 |
commit | 64478a9ae0190bb2d5839302b0f12c8b26146509 (patch) | |
tree | ae322d654eadaa4c0c6bc6faf562b227bd8aeea9 | |
parent | 04143505e98a1991b1607eccc47ce3cf2e1c7ea4 (diff) | |
download | ydb-64478a9ae0190bb2d5839302b0f12c8b26146509.tar.gz |
fix dereferencing of temporary string value in the default constructed TZtStringBuf. Enable check lifetime bounds of the TZtStringBuf
commit_hash:4aa9319e8b2d685402a8ea68fdc24fa07af82d1b
-rw-r--r-- | library/cpp/string_utils/ztstrbuf/ut/ya.make | 5 | ||||
-rw-r--r-- | library/cpp/string_utils/ztstrbuf/ya.make | 2 | ||||
-rw-r--r-- | library/cpp/string_utils/ztstrbuf/ztstrbuf.h | 17 | ||||
-rw-r--r-- | library/cpp/string_utils/ztstrbuf/ztstrbuf_ut.cpp | 28 |
4 files changed, 47 insertions, 5 deletions
diff --git a/library/cpp/string_utils/ztstrbuf/ut/ya.make b/library/cpp/string_utils/ztstrbuf/ut/ya.make new file mode 100644 index 0000000000..57ad2c4ac2 --- /dev/null +++ b/library/cpp/string_utils/ztstrbuf/ut/ya.make @@ -0,0 +1,5 @@ +UNITTEST_FOR(library/cpp/string_utils/ztstrbuf) + +SRCS(ztstrbuf_ut.cpp) + +END() diff --git a/library/cpp/string_utils/ztstrbuf/ya.make b/library/cpp/string_utils/ztstrbuf/ya.make index 9d8d3b7110..172b74dbba 100644 --- a/library/cpp/string_utils/ztstrbuf/ya.make +++ b/library/cpp/string_utils/ztstrbuf/ya.make @@ -6,3 +6,5 @@ SRCS( ) END() + +RECURSE_FOR_TESTS(ut) diff --git a/library/cpp/string_utils/ztstrbuf/ztstrbuf.h b/library/cpp/string_utils/ztstrbuf/ztstrbuf.h index 72a5218bf6..fcca39698b 100644 --- a/library/cpp/string_utils/ztstrbuf/ztstrbuf.h +++ b/library/cpp/string_utils/ztstrbuf/ztstrbuf.h @@ -16,22 +16,29 @@ class TZtStringBuf: public TStringBuf { public: - constexpr TZtStringBuf(const char* s) + constexpr TZtStringBuf(const char* s Y_LIFETIME_BOUND) noexcept : TStringBuf(s) { } - TZtStringBuf(const TString& s) + TZtStringBuf(const TString& s Y_LIFETIME_BOUND) noexcept : TStringBuf(s) { } - TZtStringBuf() - : TZtStringBuf(TString{}) + TZtStringBuf(const std::string& s Y_LIFETIME_BOUND) noexcept + : TStringBuf(s) { } - const char* c_str() const { + constexpr TZtStringBuf() noexcept + : TZtStringBuf("") + { + } + + TZtStringBuf(const TStringBuf&) = delete; + + constexpr const char* c_str() const noexcept { return data(); } }; diff --git a/library/cpp/string_utils/ztstrbuf/ztstrbuf_ut.cpp b/library/cpp/string_utils/ztstrbuf/ztstrbuf_ut.cpp new file mode 100644 index 0000000000..d8215abdfa --- /dev/null +++ b/library/cpp/string_utils/ztstrbuf/ztstrbuf_ut.cpp @@ -0,0 +1,28 @@ +#include "ztstrbuf.h" + +#include <library/cpp/testing/unittest/registar.h> + +Y_UNIT_TEST_SUITE(TZtStringBufTest) { + Y_UNIT_TEST(EmptyString) { + TZtStringBuf s0{}; + UNIT_ASSERT_VALUES_EQUAL(s0, TString{""}); + UNIT_ASSERT_VALUES_EQUAL(s0.c_str(), TString{""}); + } + + Y_UNIT_TEST(Constness) { + constexpr TZtStringBuf s0{"bar"}; + static_assert(s0[0] == 'b'); + static_assert(s0[3] == '\0'); + static_assert(s0.data()[2] == 'r'); + UNIT_ASSERT_VALUES_EQUAL(s0, TString{"bar"}); + } + + Y_UNIT_TEST(FromString) { + TString str0{"foo"}; + TZtStringBuf s0 = str0; + UNIT_ASSERT_VALUES_EQUAL(s0, "foo"); + std::string str1{"bar"}; + TZtStringBuf s1 = str1; + UNIT_ASSERT_VALUES_EQUAL(s1, "bar"); + } +} |