[libcxx-commits] [libcxx] 86b6dfc - [libc++] Fix Coverity warning about use-after-move (#78780)
via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Jan 21 13:24:08 PST 2024
Author: Louis Dionne
Date: 2024-01-21T16:24:04-05:00
New Revision: 86b6dfc619695b5bfb1880b2ed9abb4a6805fbe6
URL: https://github.com/llvm/llvm-project/commit/86b6dfc619695b5bfb1880b2ed9abb4a6805fbe6
DIFF: https://github.com/llvm/llvm-project/commit/86b6dfc619695b5bfb1880b2ed9abb4a6805fbe6.diff
LOG: [libc++] Fix Coverity warning about use-after-move (#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
Added:
Modified:
libcxx/include/variant
Removed:
################################################################################
diff --git a/libcxx/include/variant b/libcxx/include/variant
index ac69645a0fab0d..6063739e52c86b 100644
--- a/libcxx/include/variant
+++ b/libcxx/include/variant
@@ -808,14 +808,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