diff --git a/src/fuse_readdir_factory.cpp b/src/fuse_readdir_factory.cpp index 5039e4ce..91d578d5 100644 --- a/src/fuse_readdir_factory.cpp +++ b/src/fuse_readdir_factory.cpp @@ -22,11 +22,14 @@ #include "fuse_readdir_cosr.hpp" #include "fuse_readdir_seq.hpp" +#include "from_string.hpp" + #include #include #include #include #include +#include #include // Regex: @@ -56,11 +59,18 @@ _read_cfg(const std::string_view str_, re); if(matched) { - type = match[1]; + int rv = 0; + + // A count too large for an int leaves type empty, so the value + // is rejected as invalid instead of throwing. if(match[2].matched) - concurrency = std::stoi(match[2]); - if(match[3].matched) - max_queue_depth = std::stoi(match[3]); + rv = str::from(std::string_view(match[2].first,match[2].length()), + &concurrency); + if((rv == 0) && match[3].matched) + rv = str::from(std::string_view(match[3].first,match[3].length()), + &max_queue_depth); + if(rv == 0) + type = match[1]; } if(concurrency == 0) @@ -80,7 +90,11 @@ _read_cfg(const std::string_view str_, if(max_queue_depth <= 0) max_queue_depth = 2; - max_queue_depth *= concurrency; + // Reject a queue depth whose product overflows instead of wrapping. + if(max_queue_depth > (std::numeric_limits::max() / concurrency)) + type.clear(); + else + max_queue_depth *= concurrency; type_ = type; concurrency_ = concurrency; diff --git a/tests/tests.cpp b/tests/tests.cpp index 50827b28..2f1dd70c 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -7,6 +7,7 @@ #include "fs_movefile_and_open.hpp" #include "from_string.hpp" #include "fuse_link.hpp" +#include "fuse_readdir_factory.hpp" #include "fuse_rename.hpp" #include "fuse_symlink.hpp" #include "hashset.hpp" @@ -3761,6 +3762,18 @@ test_fs_movefile_and_open_same_branch() std::filesystem::remove_all(tmp_dir); } +// Counts that don't fit an int, or whose queue depth product +// overflows, must be rejected rather than throw or wrap. +void +test_readdir_factory_rejects_oversized_counts() +{ + TEST_CHECK(FUSE::ReadDirFactory::valid("cor:4:2")); + TEST_CHECK(!FUSE::ReadDirFactory::valid("cor:99999999999")); + TEST_CHECK(!FUSE::ReadDirFactory::valid("cosr:2:99999999999")); + TEST_CHECK(!FUSE::ReadDirFactory::valid("cor:2147483647:2147483647")); + TEST_CHECK(FUSE::ReadDirFactory::make("cor:99999999999") == nullptr); +} + void test_hashset_put_and_size() { @@ -4468,6 +4481,7 @@ TEST_LIST = {"fs_copyfile_basic",test_fs_copyfile_basic}, {"fs_copyfile_source_changes_cleanup_tmpfiles",test_fs_copyfile_source_changes_cleanup_tmpfiles}, {"fs_movefile_and_open_same_branch",test_fs_movefile_and_open_same_branch}, + {"readdir_factory_rejects_oversized_counts",test_readdir_factory_rejects_oversized_counts}, {"str_eq_nullptr",test_str_eq_nullptr}, {"str_startswith_char_nullptr",test_str_startswith_char_nullptr}, {"str_from_u64_suffixes",test_str_from_u64_suffixes},