How to fix 13.3 MISRA-C 2012 Polyspace Warning?

18 views (last 30 days)
A full expression containing an increment (++) or decrement (--) operator should have no other potential side effects other than that caused by the increment or decrement operator.
1.int input(void);
2.int choice(void);
3.int operation(int, int);
4.int func() {
5. int x = input(), y = input(), res;
6. int ch = choice();
7. if (choice == -1)
8. return(x++); /* Non-compliant */
9. if (choice == 0) {
10. res = x++ + y++; /* Non-compliant */
11. return(res);
12. }
13.}
--> from the above code,i am getting 13.3 warning for line no.8 and no.10, please answer how to fix 13.3 MISRA-C 2012 Warning.

Accepted Answer

Walter Roberson
Walter Roberson on 17 Mar 2022
Your x and y are automatic variables, local to func(). What is the purpose of incrementing them just before returning ?
Anyhow
Move the increments to be in expressions of their own. SO
if (choice == -1) {
int old_x;
old_x = x;
x++;
return(old_x);
}
if (choice == 0) {
int old_x, old_y;
old_x = x; old_y = y;
x++; y++;
res = old_x + old_y;
return(res);
}

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!