Skip to content

Commit f447521

Browse files
authored
fixed #13570 - PathMatch could no longer match a file after it tried to match a directory (#7242)
1 parent fe6a2e3 commit f447521

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

lib/pathmatch.cpp

Lines changed: 8 additions & 6 deletions
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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3038,4 +3038,30 @@ def test_debug_template(tmp_path):
30383038
assert stdout.find('### Symbol database ###') == -1
30393039
assert stdout.find('##AST') == -1
30403040
assert stdout.find('### Template Simplifier pass ') != -1
3041+
assert stderr.splitlines() == []
3042+
3043+
3044+
def test_file_ignore_2(tmp_path): # #13570
3045+
tests_path = tmp_path / 'tests'
3046+
os.mkdir(tests_path)
3047+
3048+
lib_path = tmp_path / 'lib'
3049+
os.mkdir(lib_path)
3050+
3051+
test_file_1 = lib_path / 'test_1.c'
3052+
with open(test_file_1, 'wt'):
3053+
pass
3054+
3055+
args = [
3056+
'-itests',
3057+
'-itest_1.c',
3058+
'.'
3059+
]
3060+
3061+
exitcode, stdout, stderr = cppcheck(args, cwd=tmp_path)
3062+
assert exitcode == 1, stdout
3063+
assert stdout.splitlines() == [
3064+
'cppcheck: error: could not find or open any of the paths given.',
3065+
'cppcheck: Maybe all paths were ignored?'
3066+
]
30413067
assert stderr.splitlines() == []

test/testpathmatch.cpp

Lines changed: 19 additions & 0 deletions
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)