Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add try/except blocks around external text file read operations to catch encoding-related exceptions #31

Merged
merged 7 commits into from
Jun 23, 2022
17 changes: 15 additions & 2 deletions nsiqcppstyle_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,12 @@ def __init__(self, filename, data=None):
self.data = data
if data is None:
f = open(filename)
self.data = f.read()
try:
self.data = f.read()
except UnicodeDecodeError as ex:
console.Out.Ci("[ERROR] UnicodeDecodeError in CppLexerNavigator: " + str(ex))
console.Out.Ci("[ERROR] Exception occurred reading file '%s', convert from UTF16LE to UTF8" % (filename))
raise ex
self.lines = self.data.splitlines()
lexer.input(self.data)
index = 0
Expand Down Expand Up @@ -983,7 +988,15 @@ def Peek(self):

def ProcessFile(ruleManager, file, data=None):
# print file
lexer = CppLexerNavigator(file, data)
try:
lexer = CppLexerNavigator(file, data)
except:
# If an exception was thrown (i.e., UnicodeDecodeError), it was
# caught, process, logged to stdout, and the exception was raised
# again. At this point in the code, there is nothing else to do
# other than skip the processing of the file, which is why we
# just return.
return
ContructContextInfo(lexer)
# Run Rules
lexer.Reset()
Expand Down
7 changes: 6 additions & 1 deletion nsiqcppstyle_exe.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,12 @@ def GetActiveFilter(self):
def GetFilterFile(self, filterfile):
if not os.path.exists(filterfile):
return None
f = open(filterfile, 'r')
try:
f = open(filterfile, 'r')
except UnicodeDecodeError as ex:
console.Out.Ci("[ERROR] UnicodeDecodeError in GetFilterFile: " + str(ex))
console.Out.Ci("[ERROR] Exception occurred reading file '%s', convert from UTF16LE to UTF8" % (filtername))
raise ex
return f

##############################################################################
Expand Down
4 changes: 4 additions & 0 deletions nsiqcppstyle_lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,10 @@ def validate_file(self, filename):
f = open(filename)
lines = f.readlines()
f.close()
except UnicodeDecodeError as ex:
console.Out.Ci("[ERROR] UnicodeDecodeError in validate_file: " + str(ex))
console.Out.Ci("[ERROR] Exception occurred reading file '%s', convert from UTF16LE to UTF8" % (filename))
raise ex
except IOError:
return # Couldn't find the file. Don't worry about it

Expand Down