Skip to content

Commit 3e0129c

Browse files
committed
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<decltype(b), int>); }()) { static_assert(std::is_same_v<decltype(b), int>); } void member() { static_assert(std::is_same_v<decltype(b), B>); } 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].
1 parent aeffdef commit 3e0129c

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

src/libutil/posix-source-accessor.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
namespace nix {
99

10-
PosixSourceAccessor::PosixSourceAccessor(std::filesystem::path && root)
11-
: root(std::move(root))
10+
PosixSourceAccessor::PosixSourceAccessor(std::filesystem::path && argRoot)
11+
: root(std::move(argRoot))
1212
{
1313
assert(root.empty() || root.is_absolute());
1414
displayPrefix = root.string();

0 commit comments

Comments
 (0)