@@ -20,8 +20,13 @@ import semmle.code.cpp.dataflow.internal.AddressFlow
20
20
import codingstandards.cpp.misra
21
21
import codingstandards.cpp.Call
22
22
import codingstandards.cpp.Loops
23
+ import codingstandards.cpp.ast.Increment
23
24
import codingstandards.cpp.misra.BuiltInTypeRules:: MisraCpp23BuiltInTypes
24
25
26
+ Variable getDeclaredVariableInForLoop ( ForStmt forLoop ) {
27
+ result = forLoop .getADeclaration ( ) .getADeclarationEntry ( ) .( VariableDeclarationEntry ) .getVariable ( )
28
+ }
29
+
25
30
/**
26
31
* Holds if the given expression may mutate the variable.
27
32
*/
@@ -75,7 +80,7 @@ Expr getLoopStepOfForStmt(ForStmt forLoop) {
75
80
iterationVariableAccess =
76
81
forLoop .getUpdate ( ) .getAChild * ( ) .( AssignExpr ) .getRValue ( ) .( SubExpr ) .getAnOperand ( )
77
82
) and
78
- iterationVariableAccess .getTarget ( ) = forLoop . getAnIterationVariable ( ) and
83
+ iterationVariableAccess .getTarget ( ) = getDeclaredVariableInForLoop ( forLoop ) and
79
84
result != iterationVariableAccess
80
85
)
81
86
}
@@ -159,9 +164,9 @@ predicate loopVariablePassedAsArgumentToNonConstReferenceParameter(
159
164
160
165
private newtype TAlertType =
161
166
/* 1. There is a counter variable that is not of an integer type. */
162
- TNonIntegerTypeCounterVariable ( ForStmt forLoop , Variable iterationVariable ) {
163
- iterationVariable = forLoop . getAnIterationVariable ( ) and
164
- exists ( Type type | type = iterationVariable .getType ( ) |
167
+ TNonIntegerTypeCounterVariable ( ForStmt forLoop , Variable loopCounter ) {
168
+ loopCounter = getDeclaredVariableInForLoop ( forLoop ) and
169
+ exists ( Type type | type = loopCounter .getType ( ) |
165
170
not (
166
171
type instanceof IntegralType or
167
172
type instanceof FixedWidthIntegralType
@@ -178,14 +183,18 @@ private newtype TAlertType =
178
183
not condition instanceof LegacyForLoopCondition
179
184
} or
180
185
/* 3-1. The loop counter is mutated somewhere other than its update expression. */
181
- TLoopCounterMutatedInLoopBody ( ForStmt forLoop , Variable loopCounter ) {
182
- loopCounter = forLoop . getAnIterationVariable ( ) and
186
+ TLoopCounterMutatedInLoopBody ( ForStmt forLoop , Variable loopCounterVariable ) {
187
+ loopCounterVariable = getDeclaredVariableInForLoop ( forLoop ) and
183
188
variableModifiedInExpression ( forLoop .getStmt ( ) .getChildStmt ( ) .getAChild * ( ) ,
184
- loopCounter .getAnAccess ( ) )
189
+ loopCounterVariable .getAnAccess ( ) )
185
190
} or
186
191
/* 3-2. The loop counter is not updated using either of `++`, `--`, `+=`, or `-=`. */
187
- TLoopCounterUpdatedNotByCrementOrAddSubAssignmentExpr ( ForStmt forLoop ) {
188
- none ( ) // TODO
192
+ TLoopCounterUpdatedNotByCrementOrAddSubAssignmentExpr (
193
+ ForStmt forLoop , Variable loopCounterVariable , Expr updateExpr
194
+ ) {
195
+ loopCounterVariable = getDeclaredVariableInForLoop ( forLoop ) and
196
+ variableModifiedInExpression ( updateExpr , loopCounterVariable .getAnAccess ( ) ) and
197
+ not updateExpr instanceof LegacyForLoopUpdateExpression
189
198
} or
190
199
/* 4. The type size of the loop counter is smaller than that of the loop bound. */
191
200
TLoopCounterSmallerThanLoopBound ( ForStmt forLoop , LegacyForLoopCondition forLoopCondition ) {
@@ -281,6 +290,7 @@ class AlertType extends TAlertType {
281
290
this = TNonIntegerTypeCounterVariable ( result , _) or
282
291
this = TNoRelationalOperatorInLoopCondition ( result , _) or
283
292
this = TLoopCounterMutatedInLoopBody ( result , _) or
293
+ this = TLoopCounterUpdatedNotByCrementOrAddSubAssignmentExpr ( result , _, _) or
284
294
this = TLoopCounterSmallerThanLoopBound ( result , _) or
285
295
this = TLoopBoundIsMutatedVariableAccess ( result , _, _) or
286
296
this = TLoopBoundIsNonConstExpr ( result , _) or
@@ -301,6 +311,8 @@ class AlertType extends TAlertType {
301
311
or
302
312
this = TLoopCounterMutatedInLoopBody ( _, result )
303
313
or
314
+ this = TLoopCounterUpdatedNotByCrementOrAddSubAssignmentExpr ( _, result , _)
315
+ or
304
316
exists ( LegacyForLoopCondition forLoopCondition |
305
317
this = TLoopCounterSmallerThanLoopBound ( _, forLoopCondition ) and
306
318
result = forLoopCondition .getLoopCounter ( )
@@ -334,6 +346,9 @@ class AlertType extends TAlertType {
334
346
this = TLoopCounterMutatedInLoopBody ( _, _) and
335
347
result = "counter variable"
336
348
or
349
+ this = TLoopCounterUpdatedNotByCrementOrAddSubAssignmentExpr ( _, _, _) and
350
+ result = "counter variable"
351
+ or
337
352
this = TLoopCounterSmallerThanLoopBound ( _, _) and
338
353
result = "counter variable"
339
354
or
@@ -364,15 +379,18 @@ class AlertType extends TAlertType {
364
379
*/
365
380
string getMessage ( ) {
366
381
this = TNonIntegerTypeCounterVariable ( _, _) and
367
- result = "The $@ is not of an integer type." // Throwaway placeholder
382
+ result = "The $@ is not of an integer type."
368
383
or
369
384
this = TNoRelationalOperatorInLoopCondition ( _, _) and
370
385
result =
371
- "The $@ does not compare the counter variable to an expression using a relational operator." // Throwaway placeholder
386
+ "The $@ does not compare the counter variable to an expression using a relational operator."
372
387
or
373
388
this = TLoopCounterMutatedInLoopBody ( _, _) and
374
389
result = "The $@ may be mutated in a location other than its update expression."
375
390
or
391
+ this = TLoopCounterUpdatedNotByCrementOrAddSubAssignmentExpr ( _, _, _) and
392
+ result = "The $@ is not updated with an $@ other than addition or subtraction."
393
+ or
376
394
this = TLoopCounterSmallerThanLoopBound ( _, _) and
377
395
result = "The $@ has a smaller type than that of the $@."
378
396
or
@@ -403,7 +421,9 @@ class AlertType extends TAlertType {
403
421
or
404
422
this = TNoRelationalOperatorInLoopCondition ( _, result ) // Throwaway
405
423
or
406
- this = TLoopCounterMutatedInLoopBody ( _, _) // Throwaway
424
+ this = TLoopCounterMutatedInLoopBody ( _, result ) // Throwaway
425
+ or
426
+ this = TLoopCounterUpdatedNotByCrementOrAddSubAssignmentExpr ( _, _, result )
407
427
or
408
428
exists ( LegacyForLoopCondition forLoopCondition |
409
429
this = TLoopCounterSmallerThanLoopBound ( _, forLoopCondition ) and
@@ -435,6 +455,9 @@ class AlertType extends TAlertType {
435
455
this = TLoopCounterMutatedInLoopBody ( _, _) and
436
456
result = "N/A" // Throwaway
437
457
or
458
+ this = TLoopCounterUpdatedNotByCrementOrAddSubAssignmentExpr ( _, _, _) and
459
+ result = "expression"
460
+ or
438
461
this = TLoopCounterSmallerThanLoopBound ( _, _) and
439
462
result = "loop bound"
440
463
or
0 commit comments