Skip to content

Commit 7746ef8

Browse files
committed
fixed #13570 - PathMatch could no longer match a file after it tried to match a directory
1 parent a3c0c6a commit 7746ef8

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

lib/pathmatch.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ bool PathMatch::match(const std::string &path) const
4545
std::string findpath = Path::fromNativeSeparators(path);
4646
if (!mCaseSensitive)
4747
strTolower(findpath);
48+
std::string finddir;
49+
if (!endsWith(findpath,'/'))
50+
finddir = removeFilename(findpath);
51+
else
52+
finddir = findpath;
4853

4954
const bool is_absolute = Path::isAbsolute(path);
5055

@@ -54,19 +59,16 @@ bool PathMatch::match(const std::string &path) const
5459

5560
// Filtering directory name
5661
if (endsWith(pathToMatch,'/')) {
57-
if (!endsWith(findpath,'/'))
58-
findpath = removeFilename(findpath);
59-
60-
if (pathToMatch.length() > findpath.length())
62+
if (pathToMatch.length() > finddir.length())
6163
continue;
6264
// Match relative paths starting with mask
6365
// -isrc matches src/foo.cpp
64-
if (findpath.compare(0, pathToMatch.size(), pathToMatch) == 0)
66+
if (finddir.compare(0, pathToMatch.size(), pathToMatch) == 0)
6567
return true;
6668
// Match only full directory name in middle or end of the path
6769
// -isrc matches myproject/src/ but does not match
6870
// myproject/srcfiles/ or myproject/mysrc/
69-
if (findpath.find("/" + pathToMatch) != std::string::npos)
71+
if (finddir.find("/" + pathToMatch) != std::string::npos)
7072
return true;
7173
}
7274
// Filtering filename

test/cli/other_test.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -2784,4 +2784,30 @@ def test_addon_suppr_cli_file_line(tmp_path):
27842784

27852785
def test_addon_suppr_cli_absfile_line(tmp_path):
27862786
test_file = tmp_path / 'test.c'
2787-
__test_addon_suppr(tmp_path, ['--suppress=misra-c2012-2.3:{}:3'.format(test_file)])
2787+
__test_addon_suppr(tmp_path, ['--suppress=misra-c2012-2.3:{}:3'.format(test_file)])
2788+
2789+
2790+
def test_file_ignore_2(tmp_path): # #13570
2791+
tests_path = tmp_path / 'tests'
2792+
os.mkdir(tests_path)
2793+
2794+
lib_path = tmp_path / 'lib'
2795+
os.mkdir(lib_path)
2796+
2797+
test_file_1 = lib_path / 'test_1.c'
2798+
with open(test_file_1, 'wt'):
2799+
pass
2800+
2801+
args = [
2802+
'-itests',
2803+
'-itest_1.c',
2804+
'.'
2805+
]
2806+
2807+
exitcode, stdout, stderr = cppcheck(args, cwd=tmp_path)
2808+
assert exitcode == 1, stdout
2809+
assert stdout.splitlines() == [
2810+
'cppcheck: error: could not find or open any of the paths given.',
2811+
'cppcheck: Maybe all paths were ignored?'
2812+
]
2813+
assert stderr.splitlines() == []

test/testpathmatch.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class TestPathMatch : public TestFixture {
5252
TEST_CASE(onemasklongerpath1);
5353
TEST_CASE(onemasklongerpath2);
5454
TEST_CASE(onemasklongerpath3);
55+
TEST_CASE(onemaskcwd);
5556
TEST_CASE(twomasklongerpath1);
5657
TEST_CASE(twomasklongerpath2);
5758
TEST_CASE(twomasklongerpath3);
@@ -60,10 +61,12 @@ class TestPathMatch : public TestFixture {
6061
TEST_CASE(filemaskdifferentcase);
6162
TEST_CASE(filemask2);
6263
TEST_CASE(filemask3);
64+
TEST_CASE(filemaskcwd);
6365
TEST_CASE(filemaskpath1);
6466
TEST_CASE(filemaskpath2);
6567
TEST_CASE(filemaskpath3);
6668
TEST_CASE(filemaskpath4);
69+
TEST_CASE(mixedallmatch);
6770
}
6871

6972
// Test empty PathMatch
@@ -146,6 +149,10 @@ class TestPathMatch : public TestFixture {
146149
ASSERT(srcMatcher.match("project/src/module/"));
147150
}
148151

152+
void onemaskcwd() const {
153+
ASSERT(!srcMatcher.match("./src"));
154+
}
155+
149156
void twomasklongerpath1() const {
150157
std::vector<std::string> masks = { "src/", "module/" };
151158
PathMatch match(std::move(masks));
@@ -189,6 +196,10 @@ class TestPathMatch : public TestFixture {
189196
ASSERT(fooCppMatcher.match("src/foo.cpp"));
190197
}
191198

199+
void filemaskcwd() const {
200+
ASSERT(fooCppMatcher.match("./lib/foo.cpp"));
201+
}
202+
192203
// Test PathMatch containing "src/foo.cpp"
193204
void filemaskpath1() const {
194205
ASSERT(srcFooCppMatcher.match("src/foo.cpp"));
@@ -205,6 +216,14 @@ class TestPathMatch : public TestFixture {
205216
void filemaskpath4() const {
206217
ASSERT(!srcFooCppMatcher.match("bar/foo.cpp"));
207218
}
219+
220+
void mixedallmatch() const { // #13570
221+
// when trying to match a directory against a directory entry it erroneously modified a local variable also used for file matching
222+
std::vector<std::string> masks = { "tests/", "file.c" };
223+
PathMatch match(std::move(masks));
224+
ASSERT(match.match("tests/"));
225+
ASSERT(match.match("lib/file.c"));
226+
}
208227
};
209228

210229
REGISTER_TEST(TestPathMatch)

0 commit comments

Comments
 (0)