MISRA Discussion Forums
Possible inaccurate example? - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA C:2012 and MISRA C:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=21)
+---- Forum: 8.2 Unused code (https://forum.misra.org.uk/forumdisplay.php?fid=157)
+---- Thread: Possible inaccurate example? (/showthread.php?tid=1293)



Possible inaccurate example? - andream - 17-11-2016

The following is the example reported for Rule 2.2 (dead code):

Quote:In this example, it is assumed that the object pointed to by p is used in other functions.

Code:
extern volatile uint16_t v;
extern char *p;

void f (void )  {
   uint16_t x;
   (void) v;  /* Compliant - v is accessed for its side effect * and the cast  to void is permitted by exception */
   (int32_t) v; /* Non-compliant - the cast operator is dead */
   v >> 3;     /* Non-compliant - the >> operator is dead */
   x = 3;      /* Non-compliant - the = operator is dead * - x is not subsequently read */
   *p++;       /* Non-compliant - result of * operator is not used */
   (*p)++;     /* Compliant - *p is incremented */
}

I'm not sure that last but one statement represents a non-compliance. In fact, the expression *p++ is treated as *(p++), as the precedence of postfix ++ is higher than *. Indeed the result of * operator is not used, but p value results permanently increased, and p is a global variable. How can be stated that it is not compliant, alias dead code, i.e. "...whose removal would not affect program behaviour"?


Re: Possible inaccurate example? - andream - 18-11-2016

Sorry for viewers. I apologize, I simpy didn't notice that the declared non compliance, in the cited example, is not dead code, rather unused "*" operator.


Re: Possible inaccurate example? - misra-c - 09-12-2016

Rule 2.2 applies to each operator in an expression, not the expression as a whole.

As was mentioned in the 2nd post, the "*" operation is unused and constitues dead code. The expression "*p++;" could be replaced by "p++;" with no change in behaviour of the program.