Skip to content

Commit 4780155

Browse files
committed
Remove alerts for negative offsets and fix @name property
Too many of the negative offsets alerts were false positives. We leave it for future work.
1 parent ccd7993 commit 4780155

File tree

5 files changed

+265
-454
lines changed

5 files changed

+265
-454
lines changed

cpp/misra/src/rules/RULE-8-7-1/PointerArgumentToCstringFunctionIsInvalid.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @id cpp/misra/pointer-argument-to-cstring-function-is-invalid
3-
* @name RULE-8-7-1: Pointer and index arguments passed to functions in <cstring> shall not be invalid.
3+
* @name RULE-8-7-1: Pointer and index arguments passed to functions in <cstring> shall not be invalid
44
* @description Pointer and index arguments passed to functions in <cstring> should result in valid
55
* reads and/or writes.
66
* @kind problem

cpp/misra/src/rules/RULE-8-7-1/PointerArithmeticFormsAnInvalidPointer.ql

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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

216222
module 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-
232224
predicate 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
252244
where
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
)
266253
select sink, source, sink, message

0 commit comments

Comments
 (0)