Skip to content

Commit 466c252

Browse files
committed
csgrep --tool: filter defects by the tool that detected them
Closes: #51
1 parent 5a35bd9 commit 466c252

8 files changed

+582
-1
lines changed

src/csgrep.cc

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "csfilter.hh"
2323
#include "cswriter.hh"
2424
#include "json-writer.hh"
25+
#include "parser-common.hh"
2526
#include "regex.hh"
2627
#include "version.hh"
2728

@@ -194,6 +195,26 @@ class MsgPredicate: public IPredicate {
194195
}
195196
};
196197

198+
class ToolPredicate: public IPredicate {
199+
private:
200+
const ImpliedAttrDigger digger_;
201+
const RE re_;
202+
203+
public:
204+
ToolPredicate(const RE &re):
205+
re_(re)
206+
{
207+
}
208+
209+
bool matchDef(const Defect &defOrig) const override {
210+
// detect tool in case it is not explicitly specified
211+
Defect def = defOrig;
212+
digger_.inferToolFromChecker(&def, /* onlyIfMissing */ true);
213+
214+
return boost::regex_search(def.tool, re_);
215+
}
216+
};
217+
197218
class KeyEventPredicate: public IPredicate {
198219
private:
199220
const RE re_;
@@ -498,7 +519,8 @@ bool chainFiltersCore(
498519
&& appendPredIfNeeded<KeyEventPredicate> (pf, vm, flags, "event")
499520
&& appendPredIfNeeded<MsgPredicate> (pf, vm, flags, "msg")
500521
&& appendPredIfNeeded<PathPredicate> (pf, vm, flags, "path")
501-
&& appendPredIfNeeded<SrcAnnotPredicate> (pf, vm, flags, "src-annot");
522+
&& appendPredIfNeeded<SrcAnnotPredicate> (pf, vm, flags, "src-annot")
523+
&& appendPredIfNeeded<ToolPredicate> (pf, vm, flags, "tool");
502524
}
503525

504526
bool chainFilters(
@@ -579,6 +601,7 @@ int main(int argc, char *argv[])
579601
("event", po::value<string>(), "defect matches if its key event matches the given regex (each defect has exactly one key event, which determines its location in the code)")
580602
("error", po::value<string>(), "defect matches if the message of its key event matches the given regex")
581603
("msg", po::value<string>(), "defect matches if any of its messages matches the given regex")
604+
("tool", po::value<string>(), "defect matches if it was detected by tool that matches the given regex")
582605
("annot", po::value<string>(), "defect matches if its annotation matches the given regex")
583606
("src-annot", po::value<string>(), "defect matches if an annotation in the _source_ file matches the given regex")
584607

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--tool gcc
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
Error: GCC_ANALYZER_WARNING (CWE-401):
2+
logrotate-3.18.0/config.c: scope_hint: In function 'readAddress'
3+
logrotate-3.18.0/config.c:376:8: warning[-Wanalyzer-malloc-leak]: leak of 'key'
4+
logrotate-3.18.0/config.c:14: included_from: Included from here.
5+
logrotate-3.18.0/config.c:3: included_from: Included from here.
6+
logrotate-3.18.0/config.c:1354:25: note: in expansion of macro 'freeLogItem'
7+
# 374| char *address = isolateValue(configFile, lineNum, key, startPtr, buf, length);
8+
# 375|
9+
# 376|-> if (address != NULL) {
10+
# 377| /* validate the address */
11+
# 378| const char *chptr = address;
12+
13+
Error: TOCTOU (CWE-367):
14+
logrotate-3.18.0/config.c:648: fs_check_call: Calling function "stat" to perform check on "path".
15+
logrotate-3.18.0/config.c:666: toctou: Calling function "opendir" that uses "path" after a check function. This can cause a time-of-check, time-of-use race condition.
16+
# 646| struct logInfo defConfigBackup;
17+
# 647|
18+
# 648|-> if (stat(path, &sb)) {
19+
# 649| message(MESS_ERROR, "cannot stat %s: %s\n", path, strerror(errno));
20+
# 650| return 1;
21+
22+
Error: GCC_ANALYZER_WARNING (CWE-401):
23+
logrotate-3.18.0/config.c: scope_hint: In function 'readConfigFile'
24+
logrotate-3.18.0/config.c:1149:28: warning[-Wanalyzer-malloc-leak]: leak of 'key'
25+
logrotate-3.18.0/config.c:14: included_from: Included from here.
26+
logrotate-3.18.0/config.c:3: included_from: Included from here.
27+
# 1147| free(key);
28+
# 1148| key = isolateLine(&start, &buf, length);
29+
# 1149|-> if (key == NULL)
30+
# 1150| continue;
31+
# 1151|
32+
33+
Error: GCC_ANALYZER_WARNING (CWE-401):
34+
logrotate-3.18.0/config.c:1180:28: warning[-Wanalyzer-malloc-leak]: leak of 'key'
35+
logrotate-3.18.0/config.c:14: included_from: Included from here.
36+
logrotate-3.18.0/config.c:3: included_from: Included from here.
37+
# 1178| free(key);
38+
# 1179| key = isolateLine(&start, &buf, length);
39+
# 1180|-> if (key == NULL)
40+
# 1181| continue;
41+
# 1182|
42+
43+
Error: GCC_ANALYZER_WARNING (CWE-401):
44+
logrotate-3.18.0/config.c:1196:28: warning[-Wanalyzer-malloc-leak]: leak of 'key'
45+
logrotate-3.18.0/config.c:14: included_from: Included from here.
46+
logrotate-3.18.0/config.c:3: included_from: Included from here.
47+
# 1194| free(key);
48+
# 1195| key = isolateLine(&start, &buf, length);
49+
# 1196|-> if (key == NULL)
50+
# 1197| continue;
51+
# 1198|
52+
53+
Error: GCC_ANALYZER_WARNING (CWE-401):
54+
logrotate-3.18.0/config.c:1263:28: warning[-Wanalyzer-malloc-leak]: leak of 'key'
55+
logrotate-3.18.0/config.c:14: included_from: Included from here.
56+
logrotate-3.18.0/config.c:3: included_from: Included from here.
57+
# 1261| key = isolateValue(configFile, lineNum, "shred cycles",
58+
# 1262| &start, &buf, length);
59+
# 1263|-> if (key == NULL)
60+
# 1264| continue;
61+
# 1265| newlog->shred_cycles = (int)strtoul(key, &chptr, 0);
62+
63+
Error: GCC_ANALYZER_WARNING (CWE-401):
64+
logrotate-3.18.0/config.c:1304:28: warning[-Wanalyzer-malloc-leak]: leak of 'key'
65+
logrotate-3.18.0/config.c:14: included_from: Included from here.
66+
logrotate-3.18.0/config.c:3: included_from: Included from here.
67+
# 1302| key = isolateValue(configFile, lineNum, "rotate count", &start,
68+
# 1303| &buf, length);
69+
# 1304|-> if (key == NULL)
70+
# 1305| continue;
71+
# 1306| newlog->rotateCount = (int)strtol(key, &chptr, 0);
72+
73+
Error: GCC_ANALYZER_WARNING (CWE-401):
74+
logrotate-3.18.0/config.c:1317:28: warning[-Wanalyzer-malloc-leak]: leak of 'key'
75+
logrotate-3.18.0/config.c:14: included_from: Included from here.
76+
logrotate-3.18.0/config.c:3: included_from: Included from here.
77+
# 1315| key = isolateValue(configFile, lineNum, "start count", &start,
78+
# 1316| &buf, length);
79+
# 1317|-> if (key == NULL)
80+
# 1318| continue;
81+
# 1319| newlog->logStart = (int)strtoul(key, &chptr, 0);
82+
83+
Error: GCC_ANALYZER_WARNING (CWE-401):
84+
logrotate-3.18.0/config.c:1329:28: warning[-Wanalyzer-malloc-leak]: leak of 'key'
85+
logrotate-3.18.0/config.c:14: included_from: Included from here.
86+
logrotate-3.18.0/config.c:3: included_from: Included from here.
87+
# 1327| key = isolateValue(configFile, lineNum, "minage count", &start,
88+
# 1328| &buf, length);
89+
# 1329|-> if (key == NULL)
90+
# 1330| continue;
91+
# 1331| newlog->rotateMinAge = (int)strtoul(key, &chptr, 0);
92+
93+
Error: GCC_ANALYZER_WARNING (CWE-401):
94+
logrotate-3.18.0/config.c:1341:28: warning[-Wanalyzer-malloc-leak]: leak of 'key'
95+
logrotate-3.18.0/config.c:14: included_from: Included from here.
96+
logrotate-3.18.0/config.c:3: included_from: Included from here.
97+
# 1339| key = isolateValue(configFile, lineNum, "maxage count", &start,
98+
# 1340| &buf, length);
99+
# 1341|-> if (key == NULL)
100+
# 1342| continue;
101+
# 1343| newlog->rotateAge = (int)strtoul(key, &chptr, 0);
102+
103+
Error: GCC_ANALYZER_WARNING (CWE-401):
104+
logrotate-3.18.0/config.c:1405:28: warning[-Wanalyzer-malloc-leak]: leak of 'key'
105+
logrotate-3.18.0/config.c:14: included_from: Included from here.
106+
logrotate-3.18.0/config.c:3: included_from: Included from here.
107+
# 1403| key = isolateValue(configFile, lineNum, "tabooext", &start,
108+
# 1404| &buf, length);
109+
# 1405|-> if (key == NULL)
110+
# 1406| continue;
111+
# 1407| endtag = key;
112+
113+
Error: GCC_ANALYZER_WARNING (CWE-401):
114+
logrotate-3.18.0/config.c:1464:28: warning[-Wanalyzer-malloc-leak]: leak of 'key'
115+
logrotate-3.18.0/config.c:14: included_from: Included from here.
116+
logrotate-3.18.0/config.c:3: included_from: Included from here.
117+
# 1462| key = isolateValue(configFile, lineNum, "taboopat", &start,
118+
# 1463| &buf, length);
119+
# 1464|-> if (key == NULL)
120+
# 1465| continue;
121+
# 1466|
122+
123+
Error: GCC_ANALYZER_WARNING (CWE-401):
124+
logrotate-3.18.0/config.c:1514:28: warning[-Wanalyzer-malloc-leak]: leak of 'key'
125+
logrotate-3.18.0/config.c:14: included_from: Included from here.
126+
logrotate-3.18.0/config.c:3: included_from: Included from here.
127+
# 1512| key = isolateValue(configFile, lineNum, "include", &start,
128+
# 1513| &buf, length);
129+
# 1514|-> if (key == NULL)
130+
# 1515| continue;
131+
# 1516|
132+
133+
Error: RESOURCE_LEAK (CWE-772):
134+
logrotate-3.18.0/logrotate.c:338: alloc_arg: "fgetfilecon_raw" allocates memory that is stored into "srcCtx".
135+
logrotate-3.18.0/logrotate.c:351: leaked_storage: Variable "srcCtx" going out of scope leaks the storage it points to.
136+
# 349| if (getfscreatecon_raw(pPrevCtx) < 0) {
137+
# 350| message(MESS_ERROR, "getting default context: %s\n", strerror(errno));
138+
# 351|-> return selinux_enforce;
139+
# 352| }
140+
# 353|
141+
142+
Error: RESOURCE_LEAK (CWE-772):
143+
logrotate-3.18.0/logrotate.c:793: alloc_arg: "setSecCtx" allocates memory that is stored into "prevCtx".
144+
logrotate-3.18.0/logrotate.c:796: leaked_storage: Variable "prevCtx" going out of scope leaks the storage it points to.
145+
# 794| /* error msg already printed */
146+
# 795| close(inFile);
147+
# 796|-> return 1;
148+
# 797| }
149+
# 798|
150+
151+
Error: RESOURCE_LEAK (CWE-772):
152+
logrotate-3.18.0/logrotate.c:1178: alloc_arg: "setSecCtx" allocates memory that is stored into "prevCtx".
153+
logrotate-3.18.0/logrotate.c:1180: leaked_storage: Variable "prevCtx" going out of scope leaks the storage it points to.
154+
# 1178| if (setSecCtx(fdcurr, currLog, &prevCtx) != 0) {
155+
# 1179| /* error msg already printed */
156+
# 1180|-> goto fail;
157+
# 1181| }
158+
# 1182| #ifdef WITH_ACL
159+
160+
Error: TOCTOU (CWE-367):
161+
logrotate-3.18.0/logrotate.c:1934: fs_check_call: Calling function "stat" to perform check on "oldName".
162+
logrotate-3.18.0/logrotate.c:1962: toctou: Calling function "rename" that uses "oldName" after a check function. This can cause a time-of-check, time-of-use race condition.
163+
# 1932| struct stat fst_buf;
164+
# 1933|
165+
# 1934|-> if (stat(oldName, &fst_buf)) {
166+
# 1935| if (errno == ENOENT) {
167+
# 1936| message(MESS_DEBUG, "old log %s does not exist\n",
168+
169+
Error: RESOURCE_LEAK (CWE-772):
170+
logrotate-3.18.0/logrotate.c:2032: alloc_arg: "setSecCtxByName" allocates memory that is stored into "savedContext".
171+
logrotate-3.18.0/logrotate.c:2034: leaked_storage: Variable "savedContext" going out of scope leaks the storage it points to.
172+
# 2032| if (setSecCtxByName(log->files[logNum], &savedContext) != 0) {
173+
# 2033| /* error msg already printed */
174+
# 2034|-> return 1;
175+
# 2035| }
176+
# 2036| #ifdef WITH_ACL
177+
178+
Error: RESOURCE_LEAK (CWE-772):
179+
logrotate-3.18.0/logrotate.c:2533: alloc_arg: "setSecCtx" allocates memory that is stored into "prevCtx".
180+
logrotate-3.18.0/logrotate.c:2537: leaked_storage: Variable "prevCtx" going out of scope leaks the storage it points to.
181+
# 2535| free(tmpFilename);
182+
# 2536| close(fdcurr);
183+
# 2537|-> return 1;
184+
# 2538| }
185+
# 2539|
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
Error: GCC_ANALYZER_WARNING (CWE-401):
2+
logrotate-3.18.0/config.c: scope_hint: In function 'readAddress'
3+
logrotate-3.18.0/config.c:376:8: warning[-Wanalyzer-malloc-leak]: leak of 'key'
4+
logrotate-3.18.0/config.c:14: included_from: Included from here.
5+
logrotate-3.18.0/config.c:3: included_from: Included from here.
6+
logrotate-3.18.0/config.c:1354:25: note: in expansion of macro 'freeLogItem'
7+
# 374| char *address = isolateValue(configFile, lineNum, key, startPtr, buf, length);
8+
# 375|
9+
# 376|-> if (address != NULL) {
10+
# 377| /* validate the address */
11+
# 378| const char *chptr = address;
12+
13+
Error: GCC_ANALYZER_WARNING (CWE-401):
14+
logrotate-3.18.0/config.c: scope_hint: In function 'readConfigFile'
15+
logrotate-3.18.0/config.c:1149:28: warning[-Wanalyzer-malloc-leak]: leak of 'key'
16+
logrotate-3.18.0/config.c:14: included_from: Included from here.
17+
logrotate-3.18.0/config.c:3: included_from: Included from here.
18+
# 1147| free(key);
19+
# 1148| key = isolateLine(&start, &buf, length);
20+
# 1149|-> if (key == NULL)
21+
# 1150| continue;
22+
# 1151|
23+
24+
Error: GCC_ANALYZER_WARNING (CWE-401):
25+
logrotate-3.18.0/config.c:1180:28: warning[-Wanalyzer-malloc-leak]: leak of 'key'
26+
logrotate-3.18.0/config.c:14: included_from: Included from here.
27+
logrotate-3.18.0/config.c:3: included_from: Included from here.
28+
# 1178| free(key);
29+
# 1179| key = isolateLine(&start, &buf, length);
30+
# 1180|-> if (key == NULL)
31+
# 1181| continue;
32+
# 1182|
33+
34+
Error: GCC_ANALYZER_WARNING (CWE-401):
35+
logrotate-3.18.0/config.c:1196:28: warning[-Wanalyzer-malloc-leak]: leak of 'key'
36+
logrotate-3.18.0/config.c:14: included_from: Included from here.
37+
logrotate-3.18.0/config.c:3: included_from: Included from here.
38+
# 1194| free(key);
39+
# 1195| key = isolateLine(&start, &buf, length);
40+
# 1196|-> if (key == NULL)
41+
# 1197| continue;
42+
# 1198|
43+
44+
Error: GCC_ANALYZER_WARNING (CWE-401):
45+
logrotate-3.18.0/config.c:1263:28: warning[-Wanalyzer-malloc-leak]: leak of 'key'
46+
logrotate-3.18.0/config.c:14: included_from: Included from here.
47+
logrotate-3.18.0/config.c:3: included_from: Included from here.
48+
# 1261| key = isolateValue(configFile, lineNum, "shred cycles",
49+
# 1262| &start, &buf, length);
50+
# 1263|-> if (key == NULL)
51+
# 1264| continue;
52+
# 1265| newlog->shred_cycles = (int)strtoul(key, &chptr, 0);
53+
54+
Error: GCC_ANALYZER_WARNING (CWE-401):
55+
logrotate-3.18.0/config.c:1304:28: warning[-Wanalyzer-malloc-leak]: leak of 'key'
56+
logrotate-3.18.0/config.c:14: included_from: Included from here.
57+
logrotate-3.18.0/config.c:3: included_from: Included from here.
58+
# 1302| key = isolateValue(configFile, lineNum, "rotate count", &start,
59+
# 1303| &buf, length);
60+
# 1304|-> if (key == NULL)
61+
# 1305| continue;
62+
# 1306| newlog->rotateCount = (int)strtol(key, &chptr, 0);
63+
64+
Error: GCC_ANALYZER_WARNING (CWE-401):
65+
logrotate-3.18.0/config.c:1317:28: warning[-Wanalyzer-malloc-leak]: leak of 'key'
66+
logrotate-3.18.0/config.c:14: included_from: Included from here.
67+
logrotate-3.18.0/config.c:3: included_from: Included from here.
68+
# 1315| key = isolateValue(configFile, lineNum, "start count", &start,
69+
# 1316| &buf, length);
70+
# 1317|-> if (key == NULL)
71+
# 1318| continue;
72+
# 1319| newlog->logStart = (int)strtoul(key, &chptr, 0);
73+
74+
Error: GCC_ANALYZER_WARNING (CWE-401):
75+
logrotate-3.18.0/config.c:1329:28: warning[-Wanalyzer-malloc-leak]: leak of 'key'
76+
logrotate-3.18.0/config.c:14: included_from: Included from here.
77+
logrotate-3.18.0/config.c:3: included_from: Included from here.
78+
# 1327| key = isolateValue(configFile, lineNum, "minage count", &start,
79+
# 1328| &buf, length);
80+
# 1329|-> if (key == NULL)
81+
# 1330| continue;
82+
# 1331| newlog->rotateMinAge = (int)strtoul(key, &chptr, 0);
83+
84+
Error: GCC_ANALYZER_WARNING (CWE-401):
85+
logrotate-3.18.0/config.c:1341:28: warning[-Wanalyzer-malloc-leak]: leak of 'key'
86+
logrotate-3.18.0/config.c:14: included_from: Included from here.
87+
logrotate-3.18.0/config.c:3: included_from: Included from here.
88+
# 1339| key = isolateValue(configFile, lineNum, "maxage count", &start,
89+
# 1340| &buf, length);
90+
# 1341|-> if (key == NULL)
91+
# 1342| continue;
92+
# 1343| newlog->rotateAge = (int)strtoul(key, &chptr, 0);
93+
94+
Error: GCC_ANALYZER_WARNING (CWE-401):
95+
logrotate-3.18.0/config.c:1405:28: warning[-Wanalyzer-malloc-leak]: leak of 'key'
96+
logrotate-3.18.0/config.c:14: included_from: Included from here.
97+
logrotate-3.18.0/config.c:3: included_from: Included from here.
98+
# 1403| key = isolateValue(configFile, lineNum, "tabooext", &start,
99+
# 1404| &buf, length);
100+
# 1405|-> if (key == NULL)
101+
# 1406| continue;
102+
# 1407| endtag = key;
103+
104+
Error: GCC_ANALYZER_WARNING (CWE-401):
105+
logrotate-3.18.0/config.c:1464:28: warning[-Wanalyzer-malloc-leak]: leak of 'key'
106+
logrotate-3.18.0/config.c:14: included_from: Included from here.
107+
logrotate-3.18.0/config.c:3: included_from: Included from here.
108+
# 1462| key = isolateValue(configFile, lineNum, "taboopat", &start,
109+
# 1463| &buf, length);
110+
# 1464|-> if (key == NULL)
111+
# 1465| continue;
112+
# 1466|
113+
114+
Error: GCC_ANALYZER_WARNING (CWE-401):
115+
logrotate-3.18.0/config.c:1514:28: warning[-Wanalyzer-malloc-leak]: leak of 'key'
116+
logrotate-3.18.0/config.c:14: included_from: Included from here.
117+
logrotate-3.18.0/config.c:3: included_from: Included from here.
118+
# 1512| key = isolateValue(configFile, lineNum, "include", &start,
119+
# 1513| &buf, length);
120+
# 1514|-> if (key == NULL)
121+
# 1515| continue;
122+
# 1516|
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--tool='^coverity$'

0 commit comments

Comments
 (0)