11/**
22 * @id cpp/misra/pointer-arithmetic-forms-an-invalid-pointer
3- * @name RULE-8-7-1: Pointer arithmetic shall not form an invalid pointer.
3+ * @name RULE-8-7-1: Pointer arithmetic shall not form an invalid pointer
44 * @description Pointers obtained as result of performing arithmetic should point to an initialized
55 * object, or an element right next to the last element of an array.
66 * @kind path-problem
7- * @precision high
7+ * @precision medium
88 * @problem.severity error
99 * @tags external/misra/id/rule-8-7-1
1010 * scope/system
@@ -68,7 +68,12 @@ class NarrowedHeapAllocationFunctionCall extends Cast {
6868 NarrowedHeapAllocationFunctionCall ( ) { alloc = this .getExpr ( ) }
6969
7070 int getMinNumElements ( ) {
71- result = alloc .getMinNumBytes ( ) / this .getUnderlyingType ( ) .( PointerType ) .getBaseType ( ) .getSize ( )
71+ exists ( int rawResult |
72+ rawResult =
73+ alloc .getMinNumBytes ( ) / this .getUnderlyingType ( ) .( PointerType ) .getBaseType ( ) .getSize ( )
74+ |
75+ result = rawResult .maximum ( 1 )
76+ )
7277 }
7378}
7479
@@ -209,26 +214,13 @@ module TrackArrayConfig implements DataFlow::ConfigSig {
209214 }
210215
211216 predicate isAdditionalFlowStep ( DataFlow:: Node nodeFrom , DataFlow:: Node nodeTo ) {
212- operandToInstructionTaintStep ( nodeFrom .asOperand ( ) , nodeTo .asInstruction ( ) )
217+ operandToInstructionTaintStep ( nodeFrom .asOperand ( ) , nodeTo .asInstruction ( ) ) and
218+ nodeTo .asInstruction ( ) instanceof PointerArithmeticInstruction
213219 }
214220}
215221
216222module TrackArray = DataFlow:: Global< TrackArrayConfig > ;
217223
218- predicate arrayIndexIsNegative (
219- DataFlow:: Node arrayDeclarationNode , DataFlow:: Node pointerFormationNode
220- ) {
221- /* 1. Ensure the array access is reachable from the array declaration. */
222- TrackArray:: flow ( arrayDeclarationNode , pointerFormationNode ) and
223- /* 2. An offset cannot be negative. */
224- exists ( ArrayAllocation arrayAllocation , PointerFormation pointerFormation |
225- arrayDeclarationNode = arrayAllocation .getNode ( ) and
226- pointerFormationNode = pointerFormation .getNode ( )
227- |
228- pointerFormation .getOffset ( ) < 0
229- )
230- }
231-
232224predicate arrayIndexExceedsBounds (
233225 DataFlow:: Node arrayDeclarationNode , DataFlow:: Node pointerFormationNode , int pointerOffset ,
234226 int arrayLength
@@ -252,15 +244,10 @@ from TrackArray::PathNode source, TrackArray::PathNode sink, string message
252244where
253245 not isExcluded ( sink .getNode ( ) .asExpr ( ) ,
254246 Memory1Package:: pointerArithmeticFormsAnInvalidPointerQuery ( ) ) and
255- (
256- exists ( int pointerOffset , int arrayLength |
257- arrayIndexExceedsBounds ( source .getNode ( ) , sink .getNode ( ) , pointerOffset , arrayLength ) and
258- message =
259- "This pointer has offset " + pointerOffset +
260- " when the minimum possible length of the object is " + arrayLength + "."
261- )
262- or
263- arrayIndexIsNegative ( source .getNode ( ) , sink .getNode ( ) ) and
264- message = "This pointer has a negative offset."
247+ exists ( int pointerOffset , int arrayLength |
248+ arrayIndexExceedsBounds ( source .getNode ( ) , sink .getNode ( ) , pointerOffset , arrayLength ) and
249+ message =
250+ "This pointer has offset " + pointerOffset +
251+ " when the minimum possible length of the object might be " + arrayLength + "."
265252 )
266253select sink , source , sink , message
0 commit comments