Skip to content

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

Merged

Conversation

winner245
Copy link
Contributor

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.

@winner245 winner245 requested a review from a team as a code owner October 20, 2024 14:08
Copy link

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 @ followed by their GitHub username.

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.

@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Oct 20, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 20, 2024

@llvm/pr-subscribers-libcxx

Author: None (winner245)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/113086.diff

1 Files Affected:

  • (modified) libcxx/include/vector (+2)
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,

Copy link
Contributor

@frederick-vs-ja frederick-vs-ja left a 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?

@frederick-vs-ja
Copy link
Contributor

Looks like a missing piece of 8ff4d21 (https://reviews.llvm.org/D138601). CC @philnik777.

@winner245
Copy link
Contributor Author

@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,
Peng

@winner245
Copy link
Contributor Author

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!

@winner245 winner245 force-pushed the winner245/vector-ctor-exception-guard branch from f3d2a81 to faa653f Compare October 24, 2024 06:47
Copy link
Member

@ldionne ldionne left a 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.

@winner245 winner245 force-pushed the winner245/vector-ctor-exception-guard branch from faa653f to 86b491c Compare October 24, 2024 16:11
Copy link
Member

@ldionne ldionne left a 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!

@winner245
Copy link
Contributor Author

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.

@winner245 winner245 force-pushed the winner245/vector-ctor-exception-guard branch from 86b491c to ef3f34c Compare October 28, 2024 17:16
@winner245
Copy link
Contributor Author

Hi team, I have resolved the conflicts due to the split of libcxx/include/vector into 2 separate files vector.h and vector_bool.h. This PR is now ready to merge. Could someone with merge permissions please help me merge it into the main branch? Thanks in advance. Cheers,

Copy link
Contributor

@frederick-vs-ja frederick-vs-ja left a 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.

@frederick-vs-ja frederick-vs-ja merged commit d3b9855 into llvm:main Oct 29, 2024
62 checks passed
Copy link

@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!

NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this pull request Nov 4, 2024
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.
@winner245 winner245 deleted the winner245/vector-ctor-exception-guard branch November 8, 2024 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
exception-safety libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants