From c556c205a9eefb3d23f168762598eb30ec3c2f99 Mon Sep 17 00:00:00 2001 From: Sergei Zimmerman <145775305+xokdvium@users.noreply.github.com> Date: Fri, 8 Nov 2024 15:44:20 +0300 Subject: [PATCH] fix(libutil/posix-source-accessor.cc): get rid of use-after-move bug Naming class member variables the same as constructor arguments is a very slippery slope because of how member variable names get resolved. Compiler is not very helpful here and we need static analysis to forbid this kind of stuff. The following example illustrates the cause quite well: ```cpp struct B { B(int) {} }; struct A { A(int b): b([&](){ return b; static_assert(std::is_same_v); }()) { static_assert(std::is_same_v); } void member() { static_assert(std::is_same_v); } B b; }; int main() { A(1).member(); } ``` From N4861 6.5.1 Unqualified name lookup: > In all the cases listed in [basic.lookup.unqual], the scopes are searched > for a declaration in the order listed in each of the respective categories; > name lookup ends as soon as a declaration is found for the name. > If no declaration is found, the program is ill-formed. In the affected code there was a use-after-move for all accesses in the constructor body, but this UB wasn't triggered. These types of errors are trivial to catch via clang-tidy's [clang-analyzer-cplusplus.Move]. (cherry picked from commit 3e0129ce3b9eb094d4a3cc8023884f372f1d7ff6) --- src/libutil/posix-source-accessor.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libutil/posix-source-accessor.cc b/src/libutil/posix-source-accessor.cc index d09ea4a87..1fed3a617 100644 --- a/src/libutil/posix-source-accessor.cc +++ b/src/libutil/posix-source-accessor.cc @@ -7,8 +7,8 @@ namespace nix { -PosixSourceAccessor::PosixSourceAccessor(std::filesystem::path && root) - : root(std::move(root)) +PosixSourceAccessor::PosixSourceAccessor(std::filesystem::path && argRoot) + : root(std::move(argRoot)) { assert(root.empty() || root.is_absolute()); displayPrefix = root.string();