]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fix: Don't crash with no or nonexistent source file with base_dir
authorJoel Rosdahl <joel@rosdahl.net>
Fri, 9 Sep 2022 07:09:06 +0000 (09:09 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Fri, 9 Sep 2022 07:58:12 +0000 (09:58 +0200)
Regression in fec405539bca90ebd0992ae6606bd10eec3a64da.

Fixes #1155.

src/util/path.cpp
test/suites/basedir.bash
unittest/test_util_path.cpp

index 5f564cc218950d9e005255aa9cdbe851e2719480..a1e797ded33a0570279beaeae87408e3c718716f 100644 (file)
@@ -52,6 +52,9 @@ is_absolute_path(std::string_view path)
 bool
 path_starts_with(std::string_view path, std::string_view prefix)
 {
+  if (path.empty()) {
+    return false;
+  }
   for (size_t i = 0, j = 0; i < path.length() && j < prefix.length();
        ++i, ++j) {
 #ifdef _WIN32
index 5486b1e50cf1998e04f2bf62ce3749c8ed36b8a8..a5aa46e9684f222664a0c29d1ed8e869d7669472 100644 (file)
@@ -17,6 +17,9 @@ SUITE_basedir() {
     # -------------------------------------------------------------------------
     TEST "Enabled CCACHE_BASEDIR"
 
+    CCACHE_BASEDIR=/ $CCACHE_COMPILE --version
+    expect_stat no_input_file 1
+
     cd dir1
     CCACHE_BASEDIR="`pwd`" $CCACHE_COMPILE -I`pwd`/include -c src/test.c
     expect_stat direct_cache_hit 0
index a4a4876aaa5d2091e04f3d8715c3dfcb49c10314..43defcada2e37b6a998184234207a1b39c36707d 100644 (file)
@@ -120,6 +120,8 @@ TEST_CASE("util::to_absolute_path_no_drive")
 
 TEST_CASE("util::path_starts_with")
 {
+  CHECK(!util::path_starts_with("", ""));
+  CHECK(!util::path_starts_with("", "/"));
   CHECK(util::path_starts_with("/foo/bar", "/foo"));
   CHECK(!util::path_starts_with("/batz/bar", "/foo"));
   CHECK(!util::path_starts_with("/foo/bar", "/foo/baz"));