From: Joel Rosdahl Date: Fri, 9 Sep 2022 07:09:06 +0000 (+0200) Subject: fix: Don't crash with no or nonexistent source file with base_dir X-Git-Tag: v4.7~74 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e2840d7d4651bd52b16b06b8358ef054f8d494a9;p=thirdparty%2Fccache.git fix: Don't crash with no or nonexistent source file with base_dir Regression in fec405539bca90ebd0992ae6606bd10eec3a64da. Fixes #1155. --- diff --git a/src/util/path.cpp b/src/util/path.cpp index 5f564cc21..a1e797ded 100644 --- a/src/util/path.cpp +++ b/src/util/path.cpp @@ -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 diff --git a/test/suites/basedir.bash b/test/suites/basedir.bash index 5486b1e50..a5aa46e96 100644 --- a/test/suites/basedir.bash +++ b/test/suites/basedir.bash @@ -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 diff --git a/unittest/test_util_path.cpp b/unittest/test_util_path.cpp index a4a4876aa..43defcada 100644 --- a/unittest/test_util_path.cpp +++ b/unittest/test_util_path.cpp @@ -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"));