-
Notifications
You must be signed in to change notification settings - Fork 1.6k
<format>: Detect some invalid contiguous ranges in range_formatter
#5187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Stephan T. Lavavej (StephanTLavavej)
merged 5 commits into
microsoft:main
from
frederick-vs-ja:contiguously-small
Jan 14, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6dc0e6d
Detect some invalid contiguous ranges in `range_formatter`
frederick-vs-ja a3bd42c
Address review comments
frederick-vs-ja 97791ab
Merge branch 'main' into contiguously-small
frederick-vs-ja 3abba55
Merge branch 'main' into contiguously-small
StephanTLavavej 2471e3e
Include `<cassert>`.
StephanTLavavej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
4 changes: 4 additions & 0 deletions
4
tests/std/tests/P2286R8_text_formatting_range_string_death/env.lst
This file contains hidden or 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,4 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| RUNALL_INCLUDE ..\usual_latest_matrix.lst |
113 changes: 113 additions & 0 deletions
113
tests/std/tests/P2286R8_text_formatting_range_string_death/test.cpp
This file contains hidden or 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,113 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <cassert> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <format> | ||
| #include <ranges> | ||
|
|
||
| #include <range_algorithm_support.hpp> | ||
| #include <test_death.hpp> | ||
|
|
||
| using namespace std; | ||
|
|
||
| struct based_huge_iterator { | ||
| const char* base_; | ||
| _Signed128 offset_; | ||
|
|
||
| using iterator_category = contiguous_iterator_tag; | ||
| using difference_type = _Signed128; | ||
| using value_type = char; | ||
|
|
||
| const char& operator*() const noexcept { | ||
| return base_[static_cast<ptrdiff_t>(offset_)]; | ||
| } | ||
|
|
||
| based_huge_iterator& operator++() noexcept { | ||
| ++offset_; | ||
| return *this; | ||
| } | ||
| based_huge_iterator operator++(int) noexcept { | ||
| auto old = *this; | ||
| ++*this; | ||
| return old; | ||
| } | ||
|
|
||
| based_huge_iterator& operator--() noexcept { | ||
| --offset_; | ||
| return *this; | ||
| } | ||
| based_huge_iterator operator--(int) noexcept { | ||
| auto old = *this; | ||
| --*this; | ||
| return old; | ||
| } | ||
|
|
||
| based_huge_iterator& operator+=(difference_type n) noexcept { | ||
| offset_ += n; | ||
| return *this; | ||
| } | ||
|
|
||
| based_huge_iterator& operator-=(difference_type n) noexcept { | ||
| offset_ -= n; | ||
| return *this; | ||
| } | ||
|
|
||
| const char* operator->() const noexcept { | ||
| return base_ + static_cast<ptrdiff_t>(offset_); | ||
| } | ||
|
|
||
| const char& operator[](difference_type n) const noexcept { | ||
| return base_[static_cast<ptrdiff_t>(offset_ + n)]; | ||
| } | ||
|
|
||
| friend based_huge_iterator operator+(based_huge_iterator i, difference_type n) noexcept { | ||
| return {i.base_, i.offset_ + n}; | ||
| } | ||
| friend based_huge_iterator operator+(difference_type n, based_huge_iterator i) noexcept { | ||
| return {i.base_, i.offset_ + n}; | ||
| } | ||
|
|
||
| friend based_huge_iterator operator-(based_huge_iterator i, difference_type n) noexcept { | ||
| return {i.base_, i.offset_ - n}; | ||
| } | ||
| friend difference_type operator-(based_huge_iterator i, based_huge_iterator j) noexcept { | ||
| assert(i.base_ == j.base_); | ||
| return i.offset_ - j.offset_; | ||
|
frederick-vs-ja marked this conversation as resolved.
|
||
| } | ||
|
|
||
| friend auto operator<=>(const based_huge_iterator&, const based_huge_iterator&) = default; | ||
| }; | ||
|
|
||
| struct based_huge_view : ranges::view_interface<based_huge_view> { | ||
| const char* base_; | ||
| _Signed128 size_; | ||
|
|
||
| based_huge_iterator begin() const noexcept { | ||
| return {base_, 0}; | ||
| } | ||
|
|
||
| based_huge_iterator end() const noexcept { | ||
| return {base_, size_}; | ||
| } | ||
| }; | ||
|
|
||
| static_assert(ranges::contiguous_range<based_huge_view>); | ||
|
|
||
| void test_case_invalid_range_size() { | ||
| based_huge_view v{.base_ = "", .size_ = static_cast<size_t>(PTRDIFF_MAX) + 1}; | ||
| (void) format("{:s}", v); | ||
| } | ||
|
|
||
| int main(int argc, char** argv) { | ||
| std_testing::death_test_executive exec; | ||
|
|
||
| #ifdef _DEBUG | ||
| exec.add_death_tests({ | ||
| test_case_invalid_range_size, | ||
| }); | ||
| #endif // defined(_DEBUG) | ||
|
|
||
| return exec.run(argc, argv); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.