diff options
| author | Marcel Braun <marcel.braun@ost.ch> | 2026-05-05 16:00:11 +0200 |
|---|---|---|
| committer | Marcel Braun <marcel.braun@ost.ch> | 2026-05-05 16:00:11 +0200 |
| commit | 9f6353679f4052b2f685ad74994bfb6d54678d44 (patch) | |
| tree | afbf9240bf976f1d03a0d804ead492e73b26906c /kernel/include | |
| parent | 156925495d7bc8bd684a346e2ab9eea12f8187cc (diff) | |
| download | kernel-9f6353679f4052b2f685ad74994bfb6d54678d44.tar.xz kernel-9f6353679f4052b2f685ad74994bfb6d54678d44.zip | |
Add check for valid path length
Diffstat (limited to 'kernel/include')
| -rw-r--r-- | kernel/include/kernel/filesystem/path.hpp | 32 |
1 files changed, 23 insertions, 9 deletions
diff --git a/kernel/include/kernel/filesystem/path.hpp b/kernel/include/kernel/filesystem/path.hpp index 298ac5f..4845bf1 100644 --- a/kernel/include/kernel/filesystem/path.hpp +++ b/kernel/include/kernel/filesystem/path.hpp @@ -1,41 +1,54 @@ #ifndef TEACH_OS_KERNEL_FILESYSTEM_PATH_HPP #define TEACH_OS_KERNEL_FILESYSTEM_PATH_HPP +#include <kernel/filesystem/constants.hpp> + +#include <kstd/string> + #include <ranges> #include <string_view> -#include <kstd/string> namespace kernel::filesystem::path { /** @brief Provides utilities for handling filesystem paths, including validation and splitting into components. - */ + */ + + /** + @brief Checks if the given path is within the maximum allowed length. + @param path The path to check. + @return true if the path length is valid, false otherwise. + */ + auto inline is_valid_path_length(std::string_view path) -> bool + { + return path.length() < kernel::filesystem::constants::max_path_length; + } /** @brief Checks if the given path is a valid absolute path (starts with '/'). @param path The path to check. @return true if the path is a valid absolute path, false otherwise. - */ + */ auto inline is_valid_absolute_path(std::string_view path) -> bool { - return !path.empty() && path.front() == '/'; + return !path.empty() && path.front() == '/' && is_valid_path_length(path); } /** @brief Checks if the given path is a valid relative path (doesn't start with '/'). @param path The path to check. @return true if the path is a valid relative path, false otherwise. - */ + */ auto inline is_valid_relative_path(std::string_view path) -> bool { - return !path.empty() && path.front() != '/'; + return !path.empty() && path.front() != '/' && is_valid_path_length(path); } /** @brief Checks if the given path is a valid path (either absolute or relative). @param path The path to check. @return true if the path is a valid path, false otherwise. - */ + */ auto inline is_valid_path(std::string_view path) -> bool { return is_valid_absolute_path(path) || is_valid_relative_path(path); @@ -45,11 +58,12 @@ namespace kernel::filesystem::path @brief Splits the given path into its components. @param path The path to split. @return A range of strings representing the components of the path. - */ + */ auto inline split(std::string_view path) { return std::views::split(path, '/') | std::views::filter([](auto const & part) { return !part.empty(); }) | - std::views::transform([](auto const & part) { return kstd::string(std::string_view(part.begin(), part.end())); }); + std::views::transform( + [](auto const & part) { return kstd::string(std::string_view(part.begin(), part.end())); }); } } // namespace kernel::filesystem::path |
