Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc++] Assert that lengths fit in difference_type
This can help flag accidentally passing in negative values into the `string_view` constructor. This aligns with a similar check in `absl::string_view`. Fixes #61100 Reviewed By: #libc, Mordante Differential Revision: https://reviews.llvm.org/D145981
- Loading branch information
Showing
2 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
libcxx/test/libcxx/strings/string.view/assert.ctor.length.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// REQUIRES: has-unix-headers | ||
|
||
// UNSUPPORTED: c++03, c++11 | ||
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0|12.0}} | ||
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1 | ||
|
||
// Construct a string_view from an invalid length | ||
// constexpr basic_string_view( const _CharT* s, size_type len ) | ||
|
||
#include <string_view> | ||
|
||
#include "check_assertion.h" | ||
|
||
int main(int, char**) { | ||
char c = 0; | ||
TEST_LIBCPP_ASSERT_FAILURE( | ||
std::string_view(&c, -1), "string_view::string_view(_CharT *, size_t): length does not fit in difference_type"); | ||
return 0; | ||
} |