Skip to content

Commit

Permalink
Fix 13143: False positive: misra 14.1 (i < n) && (x[i] > last)
Browse files Browse the repository at this point in the history
  • Loading branch information
swasti16 committed Oct 5, 2024
1 parent 198a66e commit 2f0d870
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
19 changes: 14 additions & 5 deletions addons/misra.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ def isFloatCounterInWhileLoop(whileToken):
lpar = whileToken.next
rpar = lpar.link
counterTokens = findCounterTokens(lpar.astOperand2)
tok_varid = tuple(tok.varId for tok in counterTokens if tok.varId)
whileBodyStart = None
if simpleMatch(rpar, ') {'):
whileBodyStart = rpar.next
Expand All @@ -840,13 +841,21 @@ def isFloatCounterInWhileLoop(whileToken):
token = whileBodyStart
while token != whileBodyStart.link:
token = token.next
for counterToken in counterTokens:
if not counterToken.valueType or not counterToken.valueType.isFloat():
continue
if token.isAssignmentOp and token.astOperand1.str == counterToken.str:
if not token.varId:
continue
if token.varId not in tok_varid:
continue
if not token.astParent or not token.valueType or not token.valueType.isFloat():
continue
parent = token.astParent
if parent.str in ('++', '--'):
return True
while parent:
if parent.isAssignmentOp and parent.str != "=" and parent.astOperand1 == token:
return True
if token.str == counterToken.str and token.astParent and token.astParent.str in ('++', '--'):
if parent.str == "=" and parent.astOperand1.str == token.str and parent.astOperand1 != token:
return True
parent = parent.astParent
return False


Expand Down
27 changes: 27 additions & 0 deletions addons/test/misra/misra-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,34 @@ static void misra_14_1(void) {
} while ( a < 10.0f ); // no-warning

}
//13143
static bool misra_14_1_is_increasing (const double* const x, const size_t n)
{
double last = -INFINITY;
size_t i = ((size_t) 0);
while ((i < n) && isfinite(x[i]) && (x[i] > last)) { //no-warning for 14_1
last = x[i];
i++;
}
return i == n;
}

static void misra_14_1_compliant1_foo(float x){
int i = 0;
while(i < x){ // 10.4
i += x + x; // 10.3 10.4 no-warning for 14_1
}
}

static void misra_14_1_compliant2_foo(void){
int i = 0;
float f = 0f;
while (f < 10 && i < 10) { //10.4 12.1
float f = 0f;
f = f + i; // 10.3 10.4 no warning for 14_1
i++;
}
}
static void misra_14_2_init_value(int32_t *var) {
*var = 0;
}
Expand Down

0 comments on commit 2f0d870

Please sign in to comment.