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

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jan 19 12:45:42 PST 2024


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

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

>From f08d13715a25d15f3d6c59e5373027587e373503 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Fri, 19 Jan 2024 15:43:27 -0500
Subject: [PATCH] [libc++] Fix Coverity warning about use-after-move

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
---
 libcxx/include/variant | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/libcxx/include/variant b/libcxx/include/variant
index 6179b2a1a0ab61..717080204e9c8b 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;
     }
   }
 };



More information about the libcxx-commits mailing list