Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-tidy] Check request: bugprone-avoid-dangling-in-std-filesystem #128705

Open
denzor200 opened this issue Feb 25, 2025 · 1 comment
Open
Labels
check-request Request for a new check in clang-tidy clang-tidy

Comments

@denzor200
Copy link

Needs a check that will find usage of invalidated references to something that was owned by std::filesystem::directory_iterator before the invalidation. Since std::filesystem::directory_iterator's increment has to invalidate all copies of the previous value of *this(look at https://en.cppreference.com/w/cpp/filesystem/directory_iterator/increment ), this check will suggest to make a copy instead of taking a reference.

BEFORE

namespace fs = std::filesystem;
fs::directory_iterator it("sandbox");
fs::directory_iterator end;
while (it != end) {
    const fs::directory_entry& dir_entry = *it;
    const fs::path& dir_path = it->path();
    ++it;
    std::cout << dir_entry;                         // UB
    std::cout << " (" << dir_path << ")" << '\n';   // UB
}

AFTER

namespace fs = std::filesystem;
fs::directory_iterator it("sandbox");
fs::directory_iterator end;
while (it != end) {
    const fs::directory_entry dir_entry = *it;
    const fs::path dir_path = it->path();
    ++it;
    std::cout << dir_entry;
    std::cout << " (" << dir_path << ")" << '\n';
}

https://godbolt.org/z/3Wzoq4rP4

All of the above applies to std::filesystem::recursive_directory_iterator as well.

@llvmbot
Copy link
Member

llvmbot commented Feb 25, 2025

@llvm/issue-subscribers-clang-tidy

Author: Denis Mikhailov (denzor200)

Needs a check that will find usage of invalidated references to something that was owned by std::filesystem::directory_iterator before the invalidation. Since std::filesystem::directory_iterator's increment has to invalidate all copies of the previous value of *this(look at https://en.cppreference.com/w/cpp/filesystem/directory_iterator/increment ), this check will suggest to make a copy instead of taking a reference.

BEFORE

namespace fs = std::filesystem;
fs::directory_iterator it("sandbox");
fs::directory_iterator end;
while (it != end) {
    const fs::directory_entry&amp; dir_entry = *it;
    const fs::path&amp; dir_path = it-&gt;path();
    ++it;
    std::cout &lt;&lt; dir_entry;                         // UB
    std::cout &lt;&lt; " (" &lt;&lt; dir_path &lt;&lt; ")" &lt;&lt; '\n';   // UB
}

AFTER

namespace fs = std::filesystem;
fs::directory_iterator it("sandbox");
fs::directory_iterator end;
while (it != end) {
    const fs::directory_entry dir_entry = *it;
    const fs::path dir_path = it-&gt;path();
    ++it;
    std::cout &lt;&lt; dir_entry;
    std::cout &lt;&lt; " (" &lt;&lt; dir_path &lt;&lt; ")" &lt;&lt; '\n';
}

https://godbolt.org/z/3Wzoq4rP4

All of the above applies to std::filesystem::recursive_directory_iterator as well.

@EugeneZelenko EugeneZelenko added the check-request Request for a new check in clang-tidy label Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
check-request Request for a new check in clang-tidy clang-tidy
Projects
None yet
Development

No branches or pull requests

3 participants