aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorswarmer <swarmer@yandex-team.com>2024-07-24 22:39:30 +0300
committerswarmer <swarmer@yandex-team.com>2024-07-24 22:51:20 +0300
commit81c8d5e42918da4bd9dbdd7b45a8b2471c8d9c84 (patch)
tree970f97d875bdfbd90d7b61a38740fa4e2a669f44
parentb935fd3c34569828f2ae73eb31a6ac843e2e3023 (diff)
downloadydb-81c8d5e42918da4bd9dbdd7b45a8b2471c8d9c84.tar.gz
check the lifetime bounds of a TStringBuf constructed from a TString, if TString is a std::string
04931f9cabff5a8b2a55ba25df5e3b546be4b745
-rw-r--r--util/generic/strbase.h18
-rw-r--r--util/generic/strbuf.h6
2 files changed, 24 insertions, 0 deletions
diff --git a/util/generic/strbase.h b/util/generic/strbase.h
index 86e5c0f2f2..c451dd752f 100644
--- a/util/generic/strbase.h
+++ b/util/generic/strbase.h
@@ -552,3 +552,21 @@ private:
return toCopy;
}
};
+
+/**
+ * @def Y_STRING_LIFETIME_BOUND
+ *
+ * The attribute on a string-like function parameter can be used to tell the compiler
+ * that function return value may refer that parameter.
+ * this macro differs from the Y_LIFETIME_BOUND in that it does not check
+ * the lifetime of copy-on-write strings if that implementation is used.
+ */
+#if defined(TSTRING_IS_STD_STRING)
+ #define Y_STRING_LIFETIME_BOUND Y_LIFETIME_BOUND
+#else
+ // It is difficult to determine the lifetime of a copy-on-write
+ // string using static analysis, as some copies of the string may
+ // extend the buffer's lifetime.
+ // Therefore, checking the lifetime of such strings has not yet been implemented.
+ #define Y_STRING_LIFETIME_BOUND
+#endif
diff --git a/util/generic/strbuf.h b/util/generic/strbuf.h
index a7a5eba079..2c06b0ff47 100644
--- a/util/generic/strbuf.h
+++ b/util/generic/strbuf.h
@@ -124,6 +124,12 @@ public:
{
}
+ template <typename T>
+ inline TBasicStringBuf(const TBasicString<TCharType, T>& str Y_STRING_LIFETIME_BOUND) noexcept
+ : TStringView(str.data(), str.size())
+ {
+ }
+
template <typename T, typename A>
inline TBasicStringBuf(const std::basic_string<TCharType, T, A>& str Y_LIFETIME_BOUND) noexcept
: TStringView(str)