[libcxx-commits] [libcxx] [libc++] Fix Coverity warning about use-after-move (PR #78780)

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jan 19 12:46:16 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Louis Dionne (ldionne)

<details>
<summary>Changes</summary>

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
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;
     }
   }
 };

``````````

</details>


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


More information about the libcxx-commits mailing list