summaryrefslogtreecommitdiff
path: root/absl/strings
diff options
context:
space:
mode:
Diffstat (limited to 'absl/strings')
-rw-r--r--absl/strings/string_view.h6
-rw-r--r--absl/strings/string_view_test.cc11
2 files changed, 14 insertions, 3 deletions
diff --git a/absl/strings/string_view.h b/absl/strings/string_view.h
index ddc89341..9162bb33 100644
--- a/absl/strings/string_view.h
+++ b/absl/strings/string_view.h
@@ -168,13 +168,13 @@ class string_view {
string_view( // NOLINT(runtime/explicit)
const std::basic_string<char, std::char_traits<char>, Allocator>&
str) noexcept
- : ptr_(str.data()), length_(str.size()) {}
+ : ptr_(str.data()), length_(CheckLengthInternal(str.size())) {}
// Implicit constructor of a `string_view` from nul-terminated `str`. When
// accepting possibly null strings, use `absl::NullSafeStringView(str)`
// instead (see below).
constexpr string_view(const char* str) // NOLINT(runtime/explicit)
- : ptr_(str), length_(StrLenInternal(str)) {}
+ : ptr_(str), length_(CheckLengthInternal(StrLenInternal(str))) {}
// Implicit constructor of a `string_view` from a `const char*` and length.
constexpr string_view(const char* data, size_type len)
@@ -479,7 +479,7 @@ class string_view {
private:
static constexpr size_type kMaxSize =
- std::numeric_limits<size_type>::max() / 2 + 1;
+ std::numeric_limits<difference_type>::max();
// check whether __builtin_strlen is provided by the compiler.
// GCC doesn't have __has_builtin()
diff --git a/absl/strings/string_view_test.cc b/absl/strings/string_view_test.cc
index 3077d240..a26f6c3a 100644
--- a/absl/strings/string_view_test.cc
+++ b/absl/strings/string_view_test.cc
@@ -1068,6 +1068,17 @@ TEST(HugeStringView, TwoPointTwoGB) {
TEST(NonNegativeLenTest, NonNegativeLen) {
EXPECT_DEATH_IF_SUPPORTED(absl::string_view("xyz", -1), "len <= kMaxSize");
}
+
+TEST(LenExceedsMaxSizeTest, LenExceedsMaxSize) {
+ auto max_size = absl::string_view().max_size();
+
+ // This should construct ok (although the view itself is obviously invalid).
+ absl::string_view ok_view("", max_size);
+
+ // Adding one to the max should trigger an assertion.
+ EXPECT_DEATH_IF_SUPPORTED(absl::string_view("", max_size + 1),
+ "len <= kMaxSize");
+}
#endif // !defined(NDEBUG) && !defined(ABSL_HAVE_STD_STRING_VIEW)
class StringViewStreamTest : public ::testing::Test {