Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions addons/cert.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def str05(data):
# STR07-C
# Use the bounds-checking interfaces for string manipulation
def str07(data):
if(data.standards.c=='c89' or data.standards.c=='c99'):
if data.standards.c=='c89' or data.standards.c=='c99':
return
for token in data.tokenlist:
if not isFunctionCall(token, ('strcpy', 'strcat')):
Expand All @@ -324,7 +324,7 @@ def str07(data):
continue
if args[1].isString:
continue
reportError(token, 'style', 'Use the bounds-checking interfaces %s_s()' % (token.str), 'STR07-C')
reportError(token, 'style', 'Use the bounds-checking interfaces %s_s()' % token.str, 'STR07-C')

# STR11-C
# Do not specify the bound of a character array initialized with a string literal
Expand Down
10 changes: 4 additions & 6 deletions addons/misra.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import argparse
import codecs
import string
from collections import defaultdict

try:
from itertools import izip as zip
Expand Down Expand Up @@ -1119,7 +1118,7 @@ def get_num_significant_naming_chars(self, cfg):
def misra_2_7(self, data):
for func in data.functions:
# Skip function with no parameter
if (len(func.argument) == 0):
if len(func.argument) == 0:
continue
# Setup list of function parameters
func_param_list = list()
Expand All @@ -1130,11 +1129,11 @@ def misra_2_7(self, data):
if (scope.type == "Function") and (scope.function == func):
# Search function body: remove referenced function parameter from list
token = scope.bodyStart
while (token.next != None and token != scope.bodyEnd and len(func_param_list) > 0):
if (token.variable != None and token.variable in func_param_list):
while token.next is not None and token != scope.bodyEnd and len(func_param_list) > 0:
if token.variable is not None and token.variable in func_param_list:
func_param_list.remove(token.variable)
token = token.next
if (len(func_param_list) > 0):
if len(func_param_list) > 0:
# At least one parameter has not been referenced in function body
self.reportError(func.tokenDef, 2, 7)

Expand Down Expand Up @@ -3074,7 +3073,6 @@ def executeCheck(self, rule_num, check_function, *args):
check_function(*args)

def parseDump(self, dumpfile):
filename = '.'.join(dumpfile.split('.')[:-1])
data = cppcheckdata.parsedump(dumpfile)

typeBits['CHAR'] = data.platform.char_bit
Expand Down
9 changes: 5 additions & 4 deletions addons/misra_9.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def __init__(self, elementType, name, valueType, dimensions = None):
self.numInits = 0
self.childIndex = -1

self.flexibleToken = None
self.isFlexible = False
self.structureViolationToken = None

Expand All @@ -24,7 +25,7 @@ def __repr__(self):
inits += 'D'
if not (self.isPositional or self.isDesignated) and self.numInits == 0:
inits += '_'
if (self.numInits > 1):
if self.numInits > 1:
inits += str(self.numInits)

attrs = ["childIndex", "elementType", "valueType"]
Expand Down Expand Up @@ -59,7 +60,7 @@ def getInitDump(self):
t.append('D')
if self.numInits == 0:
t.append('_')
if (self.numInits > 1):
if self.numInits > 1:
t.append(str(self.numInits))

myDump = "".join(t)
Expand Down Expand Up @@ -165,7 +166,7 @@ def markStuctureViolation(self, token):
self.structureViolationToken = token

def markAsFlexibleArray(self, token):
self.flexibleToken = token;
self.flexibleToken = token
self.isFlexible = True

def markAsCurrent(self):
Expand Down Expand Up @@ -371,7 +372,7 @@ def unwindAndContinue(self):

self.root = self.root.parent

if self.token.astParent == None:
if self.token.astParent is None:
self.token = None
break

Expand Down
1 change: 0 additions & 1 deletion addons/test/test-misra.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import pytest
import re
import sys
import subprocess

from .util import dump_create, dump_remove, convert_json_output

Expand Down
18 changes: 9 additions & 9 deletions htmlreport/cppcheck-htmlreport
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ if __name__ == '__main__':
with io.open(source_filename, 'r', encoding=options.source_encoding) as input_file:
content = input_file.read()
except IOError:
if (error['id'] == 'unmatchedSuppression'):
if error['id'] == 'unmatchedSuppression':
continue # file not found, bail out
else:
sys.stderr.write("ERROR: Source file '%s' not found.\n" %
Expand Down Expand Up @@ -592,13 +592,13 @@ if __name__ == '__main__':
output_file.write('\n <tr><th>Line</th><th>Id</th><th>CWE</th><th>Severity</th><th>Message</th></tr>')
for filename, data in sorted(files.items()):
if filename in decode_errors: # don't print a link but a note
output_file.write("\n <tr><td colspan=\"5\">%s</td></tr>" % (filename))
output_file.write("\n <tr><td colspan=\"5\">%s</td></tr>" % filename)
output_file.write("\n <tr><td colspan=\"5\"> Could not generated due to UnicodeDecodeError</td></tr>")
else:
if filename.endswith('*'): # assume unmatched suppression
output_file.write(
"\n <tr><td colspan=\"5\">%s</td></tr>" %
(filename))
filename)
else:
output_file.write(
"\n <tr><td colspan=\"5\"><a href=\"%s\">%s</a></td></tr>" %
Expand Down Expand Up @@ -640,7 +640,7 @@ if __name__ == '__main__':
output_file.write('\n </table>')
output_file.write(HTML_FOOTER % contentHandler.versionCppcheck)

if (decode_errors):
if decode_errors:
sys.stderr.write("\nGenerating html failed for the following files: " + ' '.join(decode_errors))
sys.stderr.write("\nConsider changing source-encoding (for example: \"htmlreport ... --source-encoding=\"iso8859-1\"\"\n")

Expand All @@ -653,7 +653,7 @@ if __name__ == '__main__':
stats_countlist = {}

for filename, data in sorted(files.items()):
if (filename == ''):
if filename == '':
continue
stats_tmplist = []
for error in sorted(data['errors'], key=lambda k: k['line']):
Expand All @@ -679,11 +679,11 @@ if __name__ == '__main__':
for filename in stats_countlist:
try: # also bail out if we have a file with no sev-results
_sum += stats_countlist[filename][sev]
stats_templist[filename] = (int)(stats_countlist[filename][sev]) # file : amount,
stats_templist[filename] = int(stats_countlist[filename][sev]) # file : amount,
except KeyError:
continue
# don't print "0 style" etc, if no style warnings were found
if (_sum == 0):
if _sum == 0:
continue
except KeyError:
continue
Expand All @@ -697,12 +697,12 @@ if __name__ == '__main__':
for i in stats_list_sorted: # printing loop
# for aesthetics: if it's the first iteration of the loop, get
# the max length of the number string
if (it == 0):
if it == 0:
LENGTH = len(str(i[1])) # <- length of longest number, now get the difference and try to make other numbers align to it

stats_file.write("&#160;" * 3 + str(i[1]) + "&#160;" * (1 + LENGTH - len(str(i[1]))) + "<a href=\"" + files[i[0]]['htmlfile'] + "\"> " + i[0] + "</a><br>\n")
it += 1
if (it == 10): # print only the top 10
if it == 10: # print only the top 10
break
stats_file.write("</p>\n")

Expand Down
2 changes: 1 addition & 1 deletion htmlreport/test_htmlreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def runCheck(source_filename=None, xml_version='1', xml_filename=None):
with open(os.path.join(output_directory, 'index.html')) as index_file:
index_contents = index_file.read()

yield (index_contents, output_directory)
yield index_contents, output_directory

shutil.rmtree(output_directory)

Expand Down
2 changes: 0 additions & 2 deletions test/bug-hunting/cve.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import glob
import logging
import os
import re
import shutil
import sys
import subprocess

Expand Down
3 changes: 1 addition & 2 deletions test/bug-hunting/itc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# https://github.com/regehr/itc-benchmarks


import glob
import os
import re
import shutil
Expand Down Expand Up @@ -82,7 +81,7 @@ def check(filename):
missing = []
for w in wanted:
if w not in actual:
missing.append(w);
missing.append(w)
if len(missing) > 0:
print('wanted:' + str(wanted))
print('actual:' + str(actual))
Expand Down
2 changes: 1 addition & 1 deletion test/cli/test-clang-import.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
import re
import subprocess
from testutils import create_gui_project_file, cppcheck
from testutils import cppcheck


def get_debug_section(title, stdout):
Expand Down
25 changes: 12 additions & 13 deletions test/cli/test-helloworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def cppcheck_local(args):
os.chdir('helloworld')
ret, stdout, stderr = cppcheck(args)
os.chdir(cwd)
return (ret, stdout, stderr)
return ret, stdout, stderr

def getRelativeProjectPath():
return 'helloworld'
Expand All @@ -24,7 +24,7 @@ def getAbsoluteProjectPath():
def getVsConfigs(stdout, filename):
ret = []
for line in stdout.split('\n'):
if not line.startswith('Checking %s ' % (filename)):
if not line.startswith('Checking %s ' % filename):
continue
if not line.endswith('...'):
continue
Expand All @@ -38,7 +38,7 @@ def test_relative_path():
ret, stdout, stderr = cppcheck(['--template=cppcheck1', 'helloworld'])
filename = os.path.join('helloworld', 'main.c')
assert ret == 0
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
assert stderr == '[%s:5]: (error) Division by zero.\n' % filename


def test_local_path():
Expand All @@ -51,7 +51,7 @@ def test_absolute_path():
ret, stdout, stderr = cppcheck(['--template=cppcheck1', prjpath])
filename = os.path.join(prjpath, 'main.c')
assert ret == 0
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
assert stderr == '[%s:5]: (error) Division by zero.\n' % filename

def test_addon_local_path():
ret, stdout, stderr = cppcheck_local(['--addon=misra', '--template=cppcheck1', '.'])
Expand All @@ -72,31 +72,30 @@ def test_addon_relative_path():
ret, stdout, stderr = cppcheck(['--addon=misra', '--template=cppcheck1', prjpath])
filename = os.path.join(prjpath, 'main.c')
assert ret == 0
assert stdout == 'Checking %s ...\n' % (filename)
assert stdout == ('Checking %s ...\n'
'Checking %s: SOME_CONFIG...\n' % (filename, filename))
assert stderr == ('[%s:5]: (error) Division by zero.\n'
'[%s:1]: (style) misra violation (use --rule-texts=<file> to get proper output)\n' % (filename, filename))

def test_addon_relative_path():
def test_addon_with_gui_project():
project_file = 'helloworld/test.cppcheck'
create_gui_project_file(project_file, paths=['.'], addon='misra')
ret, stdout, stderr = cppcheck(['--template=cppcheck1', '--project=' + project_file])
filename = os.path.join('helloworld', 'main.c')
assert ret == 0
assert stdout == 'Checking %s ...\n' % (filename)
assert stdout == 'Checking %s ...\n' % filename
assert stderr == ('[%s:5]: (error) Division by zero.\n'
'[%s:1]: (style) misra violation (use --rule-texts=<file> to get proper output)\n' % (filename, filename))

def test_basepath_relative_path():
prjpath = getRelativeProjectPath()
ret, stdout, stderr = cppcheck([prjpath, '--template=cppcheck1', '-rp=' + prjpath])
filename = os.path.join(prjpath, 'main.c')
assert ret == 0
assert stderr == '[main.c:5]: (error) Division by zero.\n'

def test_basepath_absolute_path():
prjpath = getAbsoluteProjectPath()
ret, stdout, stderr = cppcheck(['--template=cppcheck1', prjpath, '-rp=' + prjpath])
filename = os.path.join(prjpath, 'main.c')
assert ret == 0
assert stderr == '[main.c:5]: (error) Division by zero.\n'

Expand All @@ -112,15 +111,15 @@ def test_vs_project_relative_path():
filename = os.path.join(prjpath, 'main.c')
assert ret == 0
assert getVsConfigs(stdout, filename) == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64'
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
assert stderr == '[%s:5]: (error) Division by zero.\n' % filename

def test_vs_project_absolute_path():
prjpath = getAbsoluteProjectPath()
ret, stdout, stderr = cppcheck(['--template=cppcheck1', '--project=' + os.path.join(prjpath, 'helloworld.vcxproj')])
filename = os.path.join(prjpath, 'main.c')
assert ret == 0
assert getVsConfigs(stdout, filename) == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64'
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
assert stderr == '[%s:5]: (error) Division by zero.\n' % filename

def test_cppcheck_project_local_path():
ret, stdout, stderr = cppcheck_local(['--template=cppcheck1', '--platform=win64', '--project=helloworld.cppcheck'])
Expand All @@ -134,15 +133,15 @@ def test_cppcheck_project_relative_path():
filename = os.path.join(prjpath, 'main.c')
assert ret == 0
assert getVsConfigs(stdout, filename) == 'Debug|x64'
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
assert stderr == '[%s:5]: (error) Division by zero.\n' % filename

def test_cppcheck_project_absolute_path():
prjpath = getAbsoluteProjectPath()
ret, stdout, stderr = cppcheck(['--template=cppcheck1', '--platform=win64', '--project=' + os.path.join(prjpath, 'helloworld.cppcheck')])
filename = os.path.join(prjpath, 'main.c')
assert ret == 0
assert getVsConfigs(stdout, filename) == 'Debug|x64'
assert stderr == '[%s:5]: (error) Division by zero.\n' % (filename)
assert stderr == '[%s:5]: (error) Division by zero.\n' % filename

def test_suppress_command_line():
prjpath = getRelativeProjectPath()
Expand Down
1 change: 0 additions & 1 deletion test/cli/test-inline-suppress.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import json
import os
import re
from testutils import cppcheck

def create_unused_function_compile_commands():
Expand Down
8 changes: 4 additions & 4 deletions test/cli/test-proj2.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def cppcheck_local(args):
os.chdir('proj2')
ret, stdout, stderr = cppcheck(args)
os.chdir(cwd)
return (ret, stdout, stderr)
return ret, stdout, stderr

def test_file_filter():
ret, stdout, stderr = cppcheck(['proj2/','--file-filter=proj2/a/*'])
Expand Down Expand Up @@ -138,17 +138,17 @@ def test_gui_project_loads_absolute_vs_solution():
assert stdout.find('Checking %s Release|Win32...' % file2) >= 0
assert stdout.find('Checking %s Release|x64...' % file2) >= 0

def test_gui_project_loads_relative_vs_solution():
def test_gui_project_loads_relative_vs_solution_2():
create_gui_project_file('test.cppcheck', root_path='proj2', import_project='proj2/proj2.sln')
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'])
assert stderr == ERR_A + ERR_B

def test_gui_project_loads_relative_vs_solution():
def test_gui_project_loads_relative_vs_solution_with_exclude():
create_gui_project_file('test.cppcheck', root_path='proj2', import_project='proj2/proj2.sln', exclude_paths=['b'])
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'])
assert stderr == ERR_A

def test_gui_project_loads_absolute_vs_solution():
def test_gui_project_loads_absolute_vs_solution_2():
create_gui_project_file('test.cppcheck',
root_path=realpath('proj2'),
import_project=realpath('proj2/proj2.sln'))
Expand Down
2 changes: 0 additions & 2 deletions test/cli/test-suppress-syntaxError.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

# python -m pytest test-suppress-syntaxError.py

import os
import re
from testutils import cppcheck

def test_j2():
Expand Down
3 changes: 1 addition & 2 deletions test/cli/testutils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

import logging
import os
import shutil
import subprocess

# Create Cppcheck project file
Expand Down Expand Up @@ -32,7 +31,7 @@ def create_gui_project_file(project_file, root_path=None, import_project=None, p
cppcheck_xml += ' </suppressions>\n'
if addon:
cppcheck_xml += ' <addons>\n'
cppcheck_xml += ' <addon>%s</addon>\n' % (addon)
cppcheck_xml += ' <addon>%s</addon>\n' % addon
cppcheck_xml += ' </addons>\n'
cppcheck_xml += '</project>\n'

Expand Down
Loading