Skip to content

[libc++] Fix Coverity warning about use-after-move #78780

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
merged 1 commit into from
Jan 21, 2024

Conversation

ldionne
Copy link
Member

@ldionne ldionne commented Jan 19, 2024

While the code is technically correct because the index is never actually moved from (and anyway that wouldn't matter since it's an integer), it's still better style not to access an object after it has been moved-from. Since this is so easy to do, just save the index in a temporary variable.

rdar://120501577

While the code is technically correct because the index is never
actually moved from (and anyway that wouldn't matter since it's
an integer), it's still better style not to access an object after
it has been moved-from. Since this is so easy to do, just save the
index in a temporary variable.

rdar://120501577
@ldionne ldionne requested a review from a team as a code owner January 19, 2024 20:45
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Jan 19, 2024
@llvmbot
Copy link
Member

llvmbot commented Jan 19, 2024

@llvm/pr-subscribers-libcxx

Author: Louis Dionne (ldionne)

Changes

While the code is technically correct because the index is never actually moved from (and anyway that wouldn't matter since it's an integer), it's still better style not to access an object after it has been moved-from. Since this is so easy to do, just save the index in a temporary variable.

rdar://120501577


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

1 Files Affected:

  • (modified) libcxx/include/variant (+3-2)
diff --git a/libcxx/include/variant b/libcxx/include/variant
index 6179b2a1a0ab619..717080204e9c8bd 100644
--- a/libcxx/include/variant
+++ b/libcxx/include/variant
@@ -801,14 +801,15 @@ protected:
   _LIBCPP_HIDE_FROM_ABI static void __generic_construct(__ctor& __lhs, _Rhs&& __rhs) {
     __lhs.__destroy();
     if (!__rhs.valueless_by_exception()) {
+      auto __rhs_index = __rhs.index();
       __visitation::__base::__visit_alt_at(
-          __rhs.index(),
+          __rhs_index,
           [](auto& __lhs_alt, auto&& __rhs_alt) {
             __construct_alt(__lhs_alt, std::forward<decltype(__rhs_alt)>(__rhs_alt).__value);
           },
           __lhs,
           std::forward<_Rhs>(__rhs));
-      __lhs.__index = __rhs.index();
+      __lhs.__index = __rhs_index;
     }
   }
 };

Copy link
Member

@mordante mordante left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM!

@ldionne ldionne merged commit 86b6dfc into llvm:main Jan 21, 2024
@ldionne ldionne deleted the review/fix-variant-warning branch January 21, 2024 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants