-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcoverity-email-report-parse
executable file
·279 lines (233 loc) · 7.3 KB
/
coverity-email-report-parse
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env python3
# Copyright 2019 Kees Cook <[email protected]>
# License: GPLv2+
#
# Coverity produces way too many false positives for this automation to
# actually be useful. e.g. IS_ERR() constantly triggers false UAF warnings
# because coverity can't tell that non-NULL doesn't mean unallocated.
#
# Regardless, this will perform "git blame" analysis on reported Coverity
# failures and direct the warning to the identified authors and related
# people. (And is currently designed to only get run by me for linux-next.)
#
import os, sys, re
import subprocess, tempfile
# ** CID 1487378: Null pointer dereferences (REVERSE_INULL)
#
# _____
# *** CID nnnn: reason (name)
# /net/dsa/dsa2.c: 849 in dsa_switch_probe()
# nnn code
# Ignore "^** CID"
# Collect all CIDs/files between ^___ lines
if len(sys.argv) > 1:
report = sys.argv[1]
fd = open(report, "r")
else:
report = "stdin"
fd = sys.stdin
me = '[email protected]'
ready = False
path_next = False
hits = []
concat = ""
cid = None
reason = None
# curl -sS https://scan.coverity.com/projects/linux-next-weekly-scan | grep "Version:"
# <p>Version: next-20191025</p>
proc = subprocess.Popen(['curl', '-sS', 'https://scan.coverity.com/projects/linux-next-weekly-scan'], stdout = subprocess.PIPE)
output = proc.stdout.read().decode("utf-8")
match = re.search(r'Version:\s+([^<]+)<', output)
if not match:
raise ValueError("Bad version from Coverity project page")
branch = match.group(1)
def commit_from_line(file_name, code_line):
proc = subprocess.Popen(['git', 'blame', '-lL', code_line+','+code_line, branch, '--', file_name], stdout = subprocess.PIPE)
output = proc.stdout.read().decode("utf-8")
return output.split(' ', 1)[0]
def parse_commit(commit):
print(f"Parsing commit {commit} ...")
proc = subprocess.Popen(['git', 'log', '-1', commit], stdout=subprocess.PIPE)
output = proc.stdout.read().decode("utf-8")
author = None
subject = None
date = None
others = set()
for line in output.splitlines():
line = line.rstrip()
if line.startswith('Author: '):
author = line.split(" ", 1)[1].strip()
continue
if line.startswith('Date: ') or line.startswith('CommitDate: ') or line.startswith('AuthorDate: '):
date = line.split(" ", 1)[1].strip()
continue
if subject == None:
match = re.search(r'^ (.*)$', line)
if match:
subject = match.group(1)
continue
match = re.search(r'^ .*-by:\s+(.*@.*)$', line)
if match:
others.add(match.group(1))
match = re.search(r'^ Cc:\s+(.*@.*)$', line)
if match:
others.add(match.group(1))
# Gather full list of people based on get_maintainers.pl too.
patch = subprocess.Popen(['git', 'show', commit], stdout=subprocess.PIPE)
output = subprocess.check_output(['./scripts/get_maintainer.pl',
'--email',
'--no-rolestats'], stdin=patch.stdout).decode('utf-8')
patch.wait()
for line in output.splitlines():
others.add(line.strip())
if author in others:
others.remove(author)
return subject, date, author, others
def send_email():
info = set()
funcs = set()
cids = set()
issues = set()
for file_name,file_offset,function,cid,reason in hits:
info.add("%s %s in %s: %s (CID %s)" % (file_name, file_offset, function, reason, cid))
funcs.add(function)
reason = reason.strip()
if reason.startswith("("):
reason = reason[1:len(reason)-1]
else:
reason = reason.split(" (", 1)[0]
reason = reason.strip()
if len(reason) == 0:
raise ValueError("empty Coverity reason")
cids.add('%s ("%s")' % (cid, reason))
issues.add('%s: %s' % (function, reason))
if len(issues) > 1:
subject = "Multiple issues in %s" % (", ".join(funcs))
else:
for issue in issues:
subject = issue
subject = "Coverity: %s" % (subject)
body = "From: coverity-bot <%s>\n\n" % (me)
body += """Hello!
This is an experimental semi-automated report about issues detected by
Coverity from a scan of %s as part of the linux-next scan project:
https://scan.coverity.com/projects/linux-next-weekly-scan
You're getting this email because you were associated with the identified
lines of code (noted below) that were touched by commits:
""" % (branch)
seen = set()
commits = set()
for file_name,file_offset,function,cid,reason in hits:
what = "%s %s" % (file_name, file_offset)
if what in seen:
continue
seen.add(what)
commit = commit_from_line(file_name, file_offset)
commits.add(commit)
to = set()
cc = set()
fixes = []
for commit in commits:
name, date, author, others = parse_commit(commit)
to.add(author)
cc |= others
fix = '%s ("%s")' % (commit[0:12], name)
fixes.append(fix)
body += " %s\n %s\n" % (date, fix)
body += "\nCoverity reported the following:\n\n"
body += concat
body += """
If this is a false positive, please let us know so we can mark it as
such, or teach the Coverity rules to be smarter. If not, please make
sure fixes get into linux-next. :) For patches fixing this, please
include these lines (but double-check the "Fixes" first):
Reported-by: coverity-bot <%s>
""" % (me)
for cid in cids:
body += 'Addresses-Coverity-ID: %s\n' % (cid)
for fix in fixes:
body += 'Fixes: %s\n' % (fix)
body += """
Thanks for your attention!
--
Coverity-bot
"""
cc = list(cc)
cc.append("Gustavo A. R. Silva <[email protected]>")
cc.append("[email protected]")
cc.append("[email protected]")
template = tempfile.NamedTemporaryFile(mode='w', prefix='mutt-', encoding='utf-8')
template.write(body)
template.flush()
cc_args = []
for person in cc:
cc_args += ['-c', person]
cmd = ['mutt-batch'] + cc_args + ['-H', template.name, '-b', me, '-s', subject, '-e', 'unset signature'] + list(to)
print("To: %s" % (",\n\t".join(to)))
if len(cc) > 0:
print("Cc: %s" % (",\n\t".join(cc)))
print("Subject: %s\n" % (subject))
print("%s\n" % (body))
want = ''
while not want in ['e','s','k','q']:
print("(E)dit, (S)end, s(K)ip, (Q)uit?")
want = sys.stdin.readline().strip().lower()
if want == 'q':
print("Quit")
sys.exit(0)
if want == 'e':
# Send with edit:
print("Edit email")
os.spawnvp(os.P_WAIT, 'mutt', cmd)
return
if want == 'k':
print("Skip email")
return
if want == 's':
print("Send email")
# To auto-send:
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=open("/dev/null", "r"))
output = proc.stdout.read().decode("utf-8")
if len(output) != 0:
print(output)
return
raise ValueError("Impossible want value: '%s'" % (want))
line_num = 0
for line in fd:
line_num += 1
line = line.rstrip()
# Flush reporting queue!
if line.startswith("_________"):
if not ready:
ready = True
if len(hits) == 0:
continue
send_email()
hits = []
concat = ""
continue
if line.startswith('** CID '):
ready = False
continue
if not ready:
continue
if len(line.strip()) == 0:
continue
# Avoid MUAs hiding lines starting with ">" as if they were quotes.
if line.startswith('>>>'):
line = line.replace(">>>", "vvv", 1)
match = re.search(r'^\*\*\* CID (\d+):\s+(.*)$', line)
if match:
cid = match.group(1)
reason = match.group(2)
concat += line + "\n"
continue
match = re.search(r'^/(\S+):\s+(\d+)\s+in\s+(.*)$', line)
if match:
file_name = match.group(1)
file_offset = match.group(2)
function = match.group(3)
hits.append( (file_name, file_offset, function, cid, reason) )
concat += f"{file_name}:{file_offset} in {function}\n"
continue
concat += line + "\n"