I`l trying to catch wrong paths in input args in my code and found that behavior. code example:
#include <iostream>
#include <filesystem>
using namespace std;
namespace fs = std::filesystem;
int main() {
fs::path p = "/home";
cout << p << endl; // "/home"
cout << fs::exists(p) << endl; // 1
try {
p = p / "../..";
cout << p << endl; // "/home/../.."
cout << fs::exists(p) << endl; // 1
} catch (...) {
cout << "catched" << endl;
}
p = fs::canonical(p);
cout << p << endl; // "/"
cout << fs::exists(p) << endl; // 1
return 0;
}
How to catch out of bounds of the root with the standard capabilities? Is this a bug or a feature?
Like everyone else said, you cannot "go out of bounds" with relative parent directory (..).
That said, you can check the depth of a given path:
Live On Coliru
On my machine prints: