Repository navigation
feat: implementation of gsl::dyn_array - #1228
Conversation
There was a problem hiding this comment.
Pull request overview
Implements a new gsl::dyn_array<T, Allocator> type, adds supporting feature-detection/utility macros, and introduces a dedicated test suite for the new container.
Changes:
- Add
gsl::dyn_arrayimplementation and iterator type ininclude/gsl/dyn_array, including allocator-aware storage, range-based constructors, and basic accessors. - Extend
include/gsl/utilwith feature macros for ranges/concepts, constexpr support, and aGSL_TYPE_IS_ITERATORhelper plus a pre-C++20is_iteratortrait. - Add
tests/dyn_array_tests.cppto exercise basic construction, indexing, iteration, ranges interoperability, algorithms, and limited constexpr behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 10 comments.
| File | Description |
|---|---|
| include/gsl/dyn_array | Introduces the core dyn_array container and its iterator, but currently has several correctness issues (missing include of gsl/util for required macros, raw allocation without proper element construction/destruction, broken copy-assignment, a mis-declared operator== intended as move assignment, and const accessors that break const-correctness and rely on const_cast). |
| include/gsl/util | Adds macros (GSL_HAS_RANGES, GSL_HAS_CONCEPTS, GSL_CONSTEXPR_ALLOCATOR, GSL_CONSTEXPR_SINCE_CPP20, GSL_TYPE_IS_ITERATOR) and an is_iterator trait; however, the new code depends on <iterator> and concept types without including <iterator> and has a minor mismatch in an #endif comment. |
| tests/dyn_array_tests.cpp | Provides a good initial test suite for basic dyn_array functionality and ranges integration, but includes one ineffective std::all_of call (begin(), begin()) and a misleading #endif comment, and does not yet cover copy assignment or const-usage paths that are currently mis-implemented in the header. |
Comments suppressed due to low confidence (3)
include/gsl/dyn_array:151
- The postfix increment/decrement operators for
dyn_array_iteratorreturndyn_array_iterator&and mutate the same object before returning it, whereas standard iterator semantics (and the existingspan_iteratorin this codebase) return the previous iterator value by value. This divergence can cause subtle issues with algorithms that rely on the usual post-increment semantics; it would be safer and more consistent to follow thespan_iteratorpattern and return a copy.
constexpr auto operator++(int) -> dyn_array_iterator&
{
auto& rv = *this;
++(*this);
return rv;
}
constexpr auto operator--() -> dyn_array_iterator&
{
Expects(_pos > 0);
_pos--;
return *this;
}
constexpr auto operator--(int) -> dyn_array_iterator&
{
auto& rv = *this;
--(*this);
return rv;
}
include/gsl/dyn_array:245
- The copy-assignment operator currently constructs and returns a new
dyn_array(return dyn_array{other};) instead of assigning into*this, and it returns adyn_array&reference to a temporary. This is ill-formed and does not provide the usual copy-assignment semantics; the operator should either be deleted or implemented to reassign the current object and then return*this.
constexpr dyn_array(const dyn_array& other) : dyn_array(other.begin(), other.end()) {}
constexpr auto operator=(const dyn_array& other) -> dyn_array& { return dyn_array{other}; }
include/gsl/dyn_array:80
dyn_array_baseallocates and deallocates raw storage viaAllocator::allocate/Allocator::deallocatebut never constructs or destroys the containedTobjects, while higher-level code (dyn_arrayconstructors andoperator[]) treats that storage as fully-constructedTinstances. For any non-trivially constructible or destructibleT(e.g., types with non-trivial constructors, destructors, or virtual functions), this results in undefined behavior whenstd::fill/std::copywrite into the uninitialized storage and when clients access elements viaoperator[], which can corrupt object state or vtables and be exploitable for memory corruption or data exfiltration in security‑sensitive code. To avoid this, ensure that elements are properly constructed and destroyed (e.g., viastd::allocator_traits<Allocator>::construct/destroyor by constrainingTto trivial types and documenting that restriction) before they are read or written throughdyn_array's API.
template <typename T, typename Allocator = std::allocator<T>>
class dyn_array_base : private Allocator
{
using pointer = typename dyn_array_traits<T>::pointer;
using size_type = typename dyn_array_traits<T>::size_type;
const class dyn_array_impl
{
public:
constexpr dyn_array_impl(pointer data, size_type count) : _data{data}, _count{count}
{
Ensures((_count == 0 && _data == nullptr) || (_count > 0 && _data != nullptr));
}
constexpr auto data() const { return _data; }
constexpr auto count() const { return _count; }
private:
pointer _data;
size_type _count;
} _impl;
public:
constexpr dyn_array_base() : _impl{nullptr, 0} {}
constexpr dyn_array_base(size_type count)
: _impl{count == 0 ? nullptr : Allocator::allocate(count), count}
{}
GSL_CONSTEXPR_SINCE_CPP20 ~dyn_array_base()
{
if (impl().data()) Allocator::deallocate(impl().data(), impl().count());
}
constexpr auto impl() const -> const dyn_array_impl& { return _impl; }
bb8b58e to
ee30bbe
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.
Comments suppressed due to low confidence (2)
include/gsl/dyn_array:237
- The constructors that allocate storage and then call
std::fill/std::copywrite into the raw memory returned byAllocator::allocatewithout first constructing the elements, which is undefined behavior for non-trivialT. These constructors should either use uninitialized algorithms together withstd::allocator_traits<Allocator>(e.g.,uninitialized_fill_n/uninitialized_copy) or explicitly construct each element before assigning to it.
explicit constexpr dyn_array(size_type count, const T& value, const Allocator& alloc = {})
: base{count, alloc}
{
std::fill(begin(), end(), value);
}
GSL_TYPE_IS_ITERATOR(InputIt)
constexpr dyn_array(InputIt first, InputIt last, const Allocator& alloc = {})
: base{gsl::narrow<size_type>(std::distance(first, last)), alloc}
{
std::copy(first, last, begin());
include/gsl/dyn_array:262
dyn_array::operator=(const dyn_array&)constructs a temporarydyn_arrayand then returns a reference to that temporary (return dyn_array{other};), which leaves callers with a dangling reference. In common patterns like chained assignment (a = b = c;), this causes the second assignment to read from adyn_arraywhose storage has already been deallocated, leading to a use-after-free and potential memory corruption. Rewrite this copy-assignment operator to assign into*this(e.g., via a proper copy/copy‑and‑swap implementation) and return*thisrather than a temporary.
constexpr auto operator=(const dyn_array& other) -> dyn_array& { return dyn_array{other}; }
0c59648 to
cd047be
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (1)
include/gsl/dyn_array:79
dyn_array_baseusesAllocator::allocateandAllocator::deallocatedirectly without ever invokingstd::allocator_traits<Allocator>::construct/destroyon the elements, whiledyn_arraylater reads from and writes toimpl().data()viastd::fill/std::copyas if it contained fully constructedTobjects. For non-implicit-lifetime or non-trivially destructible types this is undefined behavior: elements are never properly constructed and their destructors are never run before deallocation, which can lead to memory corruption or resource/resource-leak issues whendyn_arrayis instantiated with complex element types under attacker-controlled data. To fix this, either restrictdyn_arrayto trivial/implicit-lifetime element types or explicitly manage object lifetimes using allocator traits to construct and destroy elements before use and before releasing storage.
constexpr dyn_array_base(const Allocator& alloc) : Allocator{alloc}, _impl{nullptr, 0} {}
constexpr dyn_array_base(size_type count, const Allocator& alloc)
: Allocator{alloc}, _impl{count == 0 ? nullptr : Allocator::allocate(count), count}
{}
GSL_CONSTEXPR_SINCE_CPP20 ~dyn_array_base()
{
if (impl().data()) Allocator::deallocate(impl().data(), impl().count());
}
Co-authored-by: carsonRadtke <10507970+carsonRadtke@users.noreply.github.com>
Co-authored-by: carsonRadtke <10507970+carsonRadtke@users.noreply.github.com>
…uration Co-authored-by: carsonRadtke <10507970+carsonRadtke@users.noreply.github.com>
Co-authored-by: carsonRadtke <10507970+carsonRadtke@users.noreply.github.com>
f3c853c to
a9e3435
Compare
Carson Radtke (carsonRadtke)
left a comment
There was a problem hiding this comment.
Copilot, please address these comments.
|
|
||
| ## <a name="H-dyn_array" />`<dyn_array>` | ||
|
|
||
| This header contains an owning dynamically allocated array type whose size is fixed between assignments. |
There was a problem hiding this comment.
| This header contains an owning dynamically allocated array type whose size is fixed between assignments. | |
| This header contains an owning dynamically allocated array type whose size is fixed at construction. |
| ``` | ||
|
|
||
| `gsl::dyn_array` owns a contiguous sequence of `T` objects allocated with `Allocator`. | ||
| The number of elements is established when the object is constructed and remains unchanged until the object is copy-assigned. |
There was a problem hiding this comment.
| The number of elements is established when the object is constructed and remains unchanged until the object is copy-assigned. | |
| The number of elements is established when the object is constructed and remains unchanged. |
| dyn_array& operator=(dyn_array&&) = delete; | ||
| ``` | ||
|
|
||
| Copy assignment replaces the contents with copies of the elements in `other`. |
There was a problem hiding this comment.
This statement is not true. Delete this line and update the following line to reflect that copy assignment is explicitly deleted.
| constexpr auto cend() const; | ||
| ``` | ||
|
|
||
| Returns an iterator to the first element or to one past the last element. |
There was a problem hiding this comment.
Maybe mention that this iterator can never be invalidated?
a75212b
into
microsoft:main
#1228 was merged with a handful of unresolved comments. This PR resolves those comments.
#1228 was merged with a handful of unresolved comments. This PR resolves those comments.
cpp-gsl 5.0.0 Created-by: HarmonybrewBot Commit-by: HarmonybrewBot Merged-by: HarmonybrewBot Description: Created by `brew bump` --- Created with `brew bump-formula-pr`.<details> <summary>release notes</summary> <pre>## What's Changed * Update return by value threshold in gsl::not_null by @ozaktash in microsoft/GSL#1205 * copilot: Provide more project context for the Copilot coding agent by @carsonRadtke in microsoft/GSL#1207 * Document differences between hardened std::span and gsl::span by @carsonRadtke with @Copilot in microsoft/GSL#1206 * Add constexpr to not_null comparison operators by @carsonRadtke with @Copilot in microsoft/GSL#1208 * fix: all pipeline failures by @carsonRadtke in microsoft/GSL#1221 * export proper syntax for GSL_SUPPRESS for new VS by @carsonRadtke in microsoft/GSL#1213 * Revert unintended change to GSL_SUPPRESS by @beinhaerter in microsoft/GSL#1226 * use cmake presets instead of cmake settings by @carsonRadtke in microsoft/GSL#1219 * Fix std::hash compilation error for gsl::not_null<std::shared_ptr<T>> by @carsonRadtke with @Copilot in microsoft/GSL#1210 * infra: individual test executables by @carsonRadtke in microsoft/GSL#1212 * doc: Update URL in pointers to F.16 C++ Core Guidelines by @maflcko in microsoft/GSL#1232 * doc: Fix to use lower-case anchors in links to C++ Core Guidelines by @maflcko in microsoft/GSL#1234 * gsl::not_null: C.85: Make swap noexcept by @maflcko in microsoft/GSL#1235 * gsl::not_null: C.89: Make a hash noexcept by @maflcko in microsoft/GSL#1236 * ci: Bump to actions/checkout@v6 by @maflcko in microsoft/GSL#1237 * ci: Use microsoft/setup-msbuild@v3 by @maflcko in microsoft/GSL#1240 * narrow_cast instead of static_cast by @beinhaerter in microsoft/GSL#1250 * ci: Bump actions/setup-java@v5 by @maflcko in microsoft/GSL#1249 * remove space in GSL_SUPPRESS by @beinhaerter in microsoft/GSL#1248 * clang-format improvements by @beinhaerter in microsoft/GSL#1251 * feat: implementation of gsl::dyn_array by @carsonRadtke in microsoft/GSL#1228 * ci: fix failing actions by @carsonRadtke in microsoft/GSL#1255 * docs: fix false statements in gsl::dyn_array docs by @carsonRadtke in microsoft/GSL#1254 * dyn_array: fix include tree by @carsonRadtke in microsoft/GSL#1261 * Bump GSL to v5.0.0 by @carsonRadtke in microsoft/GSL#1262 * docs: fix http:// to https:// in CppCoreGuidelines links by @Rajkaran-122 in microsoft/GSL#1267 * Fix floating-to-integral narrow undefined behavior by @carsonRadtke in microsoft/GSL#1266 * Ensure iterators convert to const iterators by @carsonRadtke in microsoft/GSL#1264 * Prevent external construction of checked iterators by @carsonRadtke in microsoft/GSL#1263 ## New Contributors * @ozaktash made their first contribution in microsoft/GSL#1205 * @maflcko made their first contribution in microsoft/GSL#1232 * @Rajkaran-122 made their first contribution in microsoft/GSL#1267 **Full Changelog**: https://github.com/microsoft/GSL/compare/v4.2.2...v5.0.0</pre> <p>View the full release notes at <a href="https://github.com/microsoft/GSL/releases/tag/v5.0.0">https://github.com/microsoft/GSL/releases/tag/v5.0.0</a>.</p> </details> <hr> See merge request: Harmonybrew/homebrew-core!17783
resolves: #1169
Implement gsl::dyn_array<T, Allocator> as specified by the CppCoreGuidelines here:
https://github.com/isocpp/CppCoreGuidelines/blob/master/docs/dyn_array.md
Currently the implementation is feature complete and all tests on modern compilers (with supported language versions) are passing.
What needs to be done:
What is done (this is a running checklist; I'll delete and rewrite this message before merging):