srcanamdw/codescanner/scripts/accessArrayElementWithoutCheck2.py
changeset 1 22878952f6e2
equal deleted inserted replaced
0:509e4801c378 1:22878952f6e2
       
     1 # #################################################################
       
     2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # 
       
     5 # Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
       
     6 # 
       
     7 # * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
       
     8 # * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
       
     9 # * Neither the name of Nokia Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
       
    10 # 
       
    11 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
       
    12 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS 
       
    13 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
       
    14 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
       
    15 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.#
       
    16 #
       
    17 # accessArrayElementWithoutCheck2.py
       
    18 #
       
    19 # Checks : Array element accessed by [] without checking range.
       
    20 #
       
    21 # Reason : Whenever an element in an array is accessed, the index 
       
    22 # should first be checked to ensure that it is within range. 
       
    23 # CodeScanner checks for explicit calls to a Count() or Length() 
       
    24 # function; so if the array index is checked in a different way, 
       
    25 # it gives false positives. Accessing an invalid index can cause 
       
    26 # a panic.
       
    27 #
       
    28 # #################################################################
       
    29 
       
    30 script = CScript("accessArrayElementWithoutCheck2")
       
    31 script.iReString = r"""
       
    32 	\w+
       
    33 	\s*		# optional whitespace
       
    34 	\)*		# optional closing bracket
       
    35 	\s*		# optional whitespace
       
    36 	\[
       
    37 	"""
       
    38 script.iFileExts = ["cpp"]
       
    39 script.iCategory = KCategoryCodeReviewGuides
       
    40 script.iIgnore = KIgnoreCommentsAndQuotes
       
    41 script.iSeverity = KSeverityLow
       
    42 
       
    43 accessArrayCountFn = re.compile("""
       
    44 	(\.|->)
       
    45 	\s*
       
    46 	Count
       
    47 	\s*
       
    48 	\(
       
    49 	\s*
       
    50 	\)
       
    51 	""", re.VERBOSE)
       
    52 
       
    53 accessArrayLengthFn = re.compile("""
       
    54 	(\.|->)
       
    55 	\s*
       
    56 	Length
       
    57 	\s*
       
    58 	\(
       
    59 	\s*
       
    60 	\)
       
    61 	""", re.VERBOSE)
       
    62 
       
    63 accessArrayDeclareNew = re.compile("""
       
    64 	\s*
       
    65 	=
       
    66 	\s*
       
    67 	new
       
    68 	\s*
       
    69 	""", re.VERBOSE)
       
    70 
       
    71 bufferArrayDeclaration = re.compile("""
       
    72 	\w+		# array type
       
    73 	\s*
       
    74 	<
       
    75 	\s*
       
    76 	[0-9]+
       
    77 	\s*
       
    78 	>
       
    79 	\s*
       
    80 	\w+		# array name
       
    81 	\s*
       
    82 	;
       
    83 	""", re.VERBOSE)
       
    84 
       
    85 accessArrayDeclaration = re.compile("""
       
    86 	\w+		# array type
       
    87 	\s*
       
    88 	(\*|&)*
       
    89 	\s*
       
    90 	\[
       
    91 	.*		# array size
       
    92 	\]
       
    93 	\s*
       
    94 	;
       
    95 	""", re.VERBOSE)
       
    96 
       
    97 constArrayDeclaration = re.compile("""
       
    98 	\w+		# array type
       
    99 	[0-9<>*& ]*	# optional array element
       
   100 	\s+
       
   101 	\w+		# array name
       
   102 	\s*
       
   103 	\[
       
   104 	.*		# array size
       
   105 	\]
       
   106 	\s*
       
   107 	=
       
   108 	[^=]
       
   109 	""", re.VERBOSE)
       
   110 
       
   111 def arrayAccessCompare2(lines, currentline, rematch, filename):
       
   112 	line = lines[currentline]
       
   113 	m = rematch.search(line)
       
   114 
       
   115 	if m:
       
   116 		if (accessArrayDeclareNew.search(line)):
       
   117 			return 0
       
   118 
       
   119 		if (accessArrayDeclaration.search(line)):
       
   120 			return 0
       
   121 
       
   122 		if (constArrayDeclaration.search(line)):
       
   123 			return 0
       
   124 
       
   125 		openBracketPos = line.find("[")
       
   126 		cutLine = line[openBracketPos:]
       
   127 		closeBracketPos = cutLine.find("]") + len(line) - len(cutLine)
       
   128 		if (closeBracketPos > openBracketPos) and (openBracketPos >= 0):
       
   129 			variable = line[openBracketPos:closeBracketPos]
       
   130 			variable = TrimVariableName(variable)
       
   131 
       
   132 			if (len(variable) == 0):
       
   133 				return 0
       
   134 
       
   135 			# if a constant index assume it's going to be ok!
       
   136 			if (variable[0] == "E") or (variable[0] == "K"):
       
   137 				return 0
       
   138 
       
   139 			variableIsNumeric = 1
       
   140 			varPos = 0
       
   141 			while (varPos < len(variable)):
       
   142 				if (variable[varPos] < '0') or (variable[varPos] > '9'):
       
   143 					variableIsNumeric = 0
       
   144 					break
       
   145 				varPos = varPos + 1
       
   146 
       
   147 			linePos = openBracketPos
       
   148 			while (linePos > 0) and (isNonAlpha(line[linePos])):
       
   149 				linePos = linePos - 1
       
   150 			arrayNameEnd = linePos + 1
       
   151 			while (linePos >= 0) and (isNonAlpha(line[linePos]) == 0):
       
   152 				linePos = linePos - 1
       
   153 			arrayNameStart = linePos + 1
       
   154 		
       
   155 			arrayName = ""
       
   156 			if (arrayNameStart >= 0):
       
   157 				arrayName = line[arrayNameStart:arrayNameEnd]
       
   158 				arrayName = TrimVariableName(arrayName)
       
   159 
       
   160 				if (len(arrayName) > 0):
       
   161 					# if a constant array assume it's going to be ok!
       
   162 					if (arrayName[0] == "E") or (arrayName[0] == "K"):
       
   163 						return 0
       
   164 
       
   165 			if (variableIsNumeric):
       
   166 				if (len(arrayName) > 2):
       
   167 					if (arrayName[0] == "i") and (arrayName[1] >= 'A') and (arrayName[1] <= 'Z'):
       
   168 						return 0 
       
   169 
       
   170 			i = currentline
       
   171 
       
   172 			while (i >= 0) and (i >= scanner.iCurrentMethodStart):
       
   173 				line = lines[i]
       
   174 				
       
   175 				# check to see if index is compared to array size
       
   176 				if (len(arrayName) > 0):
       
   177 					arrayNamePos = line.find(arrayName)
       
   178 					if (arrayNamePos >= 0):
       
   179 						cutLine = line[arrayNamePos + len(arrayName):]
       
   180 						if (accessArrayCountFn.search(cutLine)):
       
   181 							return 0
       
   182 						if (accessArrayLengthFn.search(cutLine)):
       
   183 							return 0
       
   184 
       
   185 					if (variableIsNumeric == 1):
       
   186 						doCheck = 0
       
   187 						if (accessArrayDeclaration.search(line)):
       
   188 							openBracketPos = line.find("[")
       
   189 							cutLine = line[openBracketPos:]
       
   190 							closeBracketPos = cutLine.find("]") + len(line) - len(cutLine)
       
   191 							if (closeBracketPos > openBracketPos) and (openBracketPos >= 0):
       
   192 								declVariable = line[openBracketPos:closeBracketPos]
       
   193 								declVariable = TrimVariableName(declVariable)
       
   194 								if (len(declVariable) > 0):
       
   195 									doCheck = 1
       
   196 
       
   197 						if (bufferArrayDeclaration.search(line)):
       
   198 							openBracketPos = line.find("<")
       
   199 							cutLine = line[openBracketPos:]
       
   200 							closeBracketPos = cutLine.find(">") + len(line) - len(cutLine)
       
   201 							if (closeBracketPos > openBracketPos) and (openBracketPos >= 0):
       
   202 								declVariable = line[openBracketPos:closeBracketPos]
       
   203 								declVariable = TrimVariableName(declVariable)
       
   204 								if (len(declVariable) > 0):
       
   205 									doCheck = 1
       
   206 
       
   207 						if (doCheck == 1):
       
   208 							# if a constant index assume it's going to be ok!
       
   209 							if (declVariable[0] == "E") or (declVariable[0] == "K"):
       
   210 								return 0
       
   211 
       
   212 							declVariableIsNumeric = 1
       
   213 							varPos = 0
       
   214 							while (varPos < len(declVariable)):
       
   215 								if (declVariable[varPos] < '0') or (declVariable[varPos] > '9'):
       
   216 									declVariableIsNumeric = 0
       
   217 									break
       
   218 								varPos = varPos + 1
       
   219 							if (declVariableIsNumeric == 1):
       
   220 								if (int(variable) < int(declVariable)):
       
   221 									return 0
       
   222 
       
   223 				i = i - 1
       
   224 			return 1
       
   225 
       
   226 	return 0
       
   227 
       
   228 script.iCompare	= arrayAccessCompare2
       
   229 scanner.AddScript(script)