]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Assert in get_path_in_cache instead of throwing on bad preconditions
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 1 Dec 2019 20:48:37 +0000 (21:48 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 1 Dec 2019 20:50:49 +0000 (21:50 +0100)
A bad `levels` argument is an internal error and cannot be a result of
user input.

src/Util.cpp
src/Util.hpp
unittest/test_Util.cpp

index 871fbeb9a4bfb6b28bea9ccbe0d4932afa05f7e2..f624f73fbb862548bc7665ac163d07179f374e65 100644 (file)
@@ -221,6 +221,9 @@ get_path_in_cache(string_view cache_dir,
                   string_view name,
                   string_view suffix)
 {
+  assert(levels >= 1 && levels <= 8);
+  assert(levels < name.length());
+
   std::string path(cache_dir);
   path.reserve(path.size() + levels * 2 + 1 + name.length() - levels
                + suffix.length());
index 7b489b7ff623547a7d69216822395fe23b33df52..1c1ebacc525eb28225e8fd8827778e894f4d972b 100644 (file)
@@ -146,7 +146,7 @@ void get_level_1_files(const std::string& dir,
 // it. Additionally `levels` single-character, '/'-separated subpaths are split
 // from the beginning of `name` before joining them all.
 //
-// Throws if cache dir levels is greater than the length of `name`.
+// `levels` must be less than the length of `name` and in the interval [1,8].
 //
 // E.g. "ABCDEF" and ".foo" will become "/ccache/A/B/CDEF.foo" when the cache
 // directory is "/ccache" and cache dir levels is 2.
index 665d6650169a0e9627430efcf0cb2459f1a61564..a66905076aa665264455b9f0e2bad6bf8ad1b56c 100644 (file)
@@ -250,37 +250,10 @@ TEST_CASE("Util::get_level_1_files")
 
 TEST_CASE("Util::get_path_in_cache")
 {
-  {
-    std::string path =
-      Util::get_path_in_cache("/zz/ccache", 0, "ABCDEF", ".suffix");
-    CHECK(path == "/zz/ccache/ABCDEF.suffix");
-  }
-
-  {
-    std::string path =
-      Util::get_path_in_cache("/zz/ccache", 1, "ABCDEF", ".suffix");
-    CHECK(path == "/zz/ccache/A/BCDEF.suffix");
-  }
-
-  {
-    std::string path =
-      Util::get_path_in_cache("/zz/ccache", 4, "ABCDEF", ".suffix");
-    CHECK(path == "/zz/ccache/A/B/C/D/EF.suffix");
-  }
-
-  {
-    std::string path = Util::get_path_in_cache("/zz/ccache", 0, "", ".suffix");
-    CHECK(path == "/zz/ccache/.suffix");
-  }
-
-  {
-    std::string path =
-      Util::get_path_in_cache("/zz/ccache", 2, "AB", ".suffix");
-    CHECK(path == "/zz/ccache/A/B/.suffix");
-  }
-
-  REQUIRE_THROWS_WITH(Util::get_path_in_cache("/zz/ccache", 3, "AB", ".suffix"),
-                      EndsWith("string_view::at()"));
+  CHECK(Util::get_path_in_cache("/zz/ccache", 1, "ABCDEF", ".suffix")
+        == "/zz/ccache/A/BCDEF.suffix");
+  CHECK(Util::get_path_in_cache("/zz/ccache", 4, "ABCDEF", ".suffix")
+        == "/zz/ccache/A/B/C/D/EF.suffix");
 }
 
 TEST_CASE("Util::int_to_big_endian")