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 Sep 28, 2024
1 parent 198a66e commit 594d20a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
17 changes: 12 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_str = tuple(tok.str for tok in counterTokens)
whileBodyStart = None
if simpleMatch(rpar, ') {'):
whileBodyStart = rpar.next
Expand All @@ -840,13 +841,19 @@ 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 token.str not in tok_str:
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.astOperand1.str == token.str and parent.astOperand1 != token:
return True
parent = parent.astParent
return False


Expand Down
18 changes: 18 additions & 0 deletions addons/test/misra/misra-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,24 @@ 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_compliant_foo(float x){
int i = 0;
while(i < x){ // 10.4
i += x; // 10.3 10.4 no-warning for 14_1
}
}

static void misra_14_2_init_value(int32_t *var) {
*var = 0;
Expand Down

0 comments on commit 594d20a

Please sign in to comment.