-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Add exception guard for constructor vector(n, x, a) #113086
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
Add exception guard for constructor vector(n, x, a) #113086
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-libcxx Author: None (winner245) ChangesAdded exception guard to the Full diff: https://github.com/llvm/llvm-project/pull/113086.diff 1 Files Affected:
diff --git a/libcxx/include/vector b/libcxx/include/vector
index dc31f31838264c..700cb51f41eeb2 100644
--- a/libcxx/include/vector
+++ b/libcxx/include/vector
@@ -487,10 +487,12 @@ public:
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
vector(size_type __n, const value_type& __x, const allocator_type& __a)
: __alloc_(__a) {
+ auto __guard = std::__make_exception_guard(__destroy_vector(*this));
if (__n > 0) {
__vallocate(__n);
__construct_at_end(__n, __x);
}
+ __guard.__complete();
}
template <class _InputIterator,
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I've found the memory leaking case that can be fixed by this PR (Godbolt link).
Could you add a new .pass.cpp
to libcxx/test/libcxx/containers/sequences/vector/vector.cons
which asserts that memory leak doesn't raise from this constructor when an exception is thrown?
Looks like a missing piece of 8ff4d21 (https://reviews.llvm.org/D138601). CC @philnik777. |
@frederick-vs-ja Thank you for your guidance and for providing the memory leak test case. I will include the test in a .pass.cpp file and make sure my change passes the test. Also, I really appreciate you pointing out that my PR is related to 8ff4d21 by @philnik777. I will definitely look into that as well. Best regards, |
Hi team, Following @frederick-vs-ja's suggestion and reviewing the prior work by @philnik777, I have included an additional exception test in the existing file libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp. This new test checks for memory leaks in scenarios where vector(size_type, value_type, const allocator_type&) might throw, ensuring no memory is leaked in such cases. I have compiled the entire project and run the new test to confirm that this change does not introduce new problems. Thank you, @frederick-vs-ja, for your valuable suggestion! |
f3d2a81
to
faa653f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for the fix. Can you please rebase your patch onto main
? That will fix the CI issues, which have been fixed on the latest main
.
faa653f
to
86b491c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM once CI is green. Thanks!
Thank you @ldionne so much for your prompt fix. The failing checks are now passing. Once again, I would like to thank you both, @frederick-vs-ja and @ldionne, for your patience and guidance. I am so proud to have the opportunity to work with you. |
86b491c
to
ef3f34c
Compare
Hi team, I have resolved the conflicts due to the split of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm merging this, hoping everything is OK.
@winner245 Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Added exception guard to the `vector(n, x, a)` constructor to enhance exception safety. This change ensures that the `vector(n, x, a)` constructor is consistent with other constructors, such as `vector(n)`, `vector(n, x)`, `vector(n, a)`, in terms of exception safety.
Added exception guard to the
vector(n, x, a)
constructor to enhance exception safety. This change ensures that thevector(n, x, a)
constructor is consistent with other constructors, such asvector(n)
,vector(n, x)
,vector(n, a)
, in terms of exception safety.