Skip to content
C++: Normalize path spelling without claiming filesystem safety

C++: Normalize path spelling without claiming filesystem safety

November 11, 2026

A generated report may display reports/./daily/../summary.txt more clearly as reports/summary.txt. Lexical normalization simplifies path components without touching the filesystem. It cannot establish whether the path exists or where symlinks lead.

Prepare a scratch directory

Tested on Linux with GCC 14.2.0 in C++20 mode. You need Bash, a C++20 compiler and standard library, a text editor, and the mktemp, rm, and rmdir utilities. Keep the same terminal open. Stop if setup fails, before saving the source:

start_directory="$PWD"
work_directory="$(mktemp -d)" || exit 1
cd -- "$work_directory" || exit 1

Run the example

Save as cpp-path-lexical-normalization-limits.cpp:

#include <cassert>
#include <filesystem>
#include <iostream>
#include <string_view>

int main() {
    namespace fs = std::filesystem;
    for (std::string_view spelling : {"reports/./daily/../summary.txt",
                                      "../reports/./summary.txt", "a/.."}) {
        const fs::path original{spelling};
        const auto normalized = original.lexically_normal();
        std::cout << spelling << " -> " << normalized.generic_string() << '\n';
    }
    assert(fs::path("reports/./daily/../summary.txt").lexically_normal() ==
           fs::path("reports/summary.txt"));
    assert(fs::path("../reports/./summary.txt").lexically_normal() ==
           fs::path("../reports/summary.txt"));
    assert(fs::path("a/..").lexically_normal() == fs::path("."));
}

Compile and run:

g++ -std=c++20 -Wall -Wextra -Werror -pedantic cpp-path-lexical-normalization-limits.cpp -o cpp-path-lexical-normalization-limits &&
./cpp-path-lexical-normalization-limits

Observed output:

reports/./daily/../summary.txt -> reports/summary.txt
../reports/./summary.txt -> ../reports/summary.txt
a/.. -> .

How it works

The first example removes a dot component and cancels an ordinary component followed by a parent component. A leading parent component in a relative path remains because there is no earlier component to cancel. Normalizing the last example leaves the current-directory spelling.

generic_string makes the display use generic path separators. The test ran on Linux; native path parsing differs across platforms, especially for drive names and backslashes. These examples do not create any of the displayed paths.

Limits and choices

Do not use this function alone to confine file access beneath a directory. Symlinks can make a lexical path resolve somewhere else, and filesystem state can change between checking and opening. Even removing a component followed by a parent component can change filesystem meaning when that component is a symlink. Canonicalization performs filesystem resolution but does not by itself solve concurrent replacement races. Keep lexical normalization for spelling-oriented tasks unless the application has a separate, appropriate access-control design.

The C++ working draft specifies the library operation used here. The example uses the C++20 interface tested above; newer draft additions are outside its scope.

Clean up

rm -- cpp-path-lexical-normalization-limits.cpp cpp-path-lexical-normalization-limits
cd -- "$start_directory" || exit 1
rmdir -- "$work_directory"
unset start_directory work_directory

If the directory is not empty, inspect the remaining files before removing anything else.

Acknowledgements

This post was created with assistance from AI.

Last updated on