<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
</head>
<body dir="ltr">
<div id="divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif;" dir="ltr">
<p style="margin-top:0;margin-bottom:0"></p>
<div>Here is my personal interpretation of Rule 13.4, subject to review.<br>
</div>
<div><br>
</div>
<div>The applicable C18 paragraphs (equivalent to the C99 references in MISRA C:2012) are:<br>
<br>
[C18-J.1-16] Unspecified behavior - "The order in which subexpressions are evaluated and the order in which side effects take place,<br>
except as specified for the function-call () , && , || , ?: , and comma operators".<br>
<br>
[C18-J.1-18] Unspecified behavior - "The order in which the operands of an assignment operator are evaluated".<br>
<br>
[C18-J.2-35] Undefined behaviour - "A side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object."<br>
<br>
Some examples:<br>
<br>
~~~~><br>
int f1 (void)<br>
{<br>
  int x;<br>
  return (x = 2) + (x = 3); // unsequenced side effects<br>
}<br>
<br>
void f2 (void)<br>
{<br>
  int v [2] = { 2, 3 };<br>
  int x = 0;<br>
  v [x] = v [x = 1]; // unsequenced side effect and value computation<br>
  return v [0];<br>
}<br>
<br>
int x3 = 2;<br>
<br>
int g3 (void)<br>
{<br>
  return x3;<br>
}<br>
<br>
int f3 (void)<br>
{<br>
  return (x3 += 3) + g3 (); // unspecified behaviour<br>
}<br>
<~~~~<br>
<br>
Rule 13.4 relates to Rules 13.2 and 13.3:<br>
<br>
* Rule 13.2 - "The value of an expression and its persistent side effects shall be the same under all permitted evaluation orders".<br>
<br>
* Rule 13.3 - "A full expression containing an increment (++) or decrement (--) operator should have no other potential side effects than that caused by the increment or decrement operator".<br>
<br>
Rule 13.2 would address the general problem of unspecified/undefined behaviours caused by side effects, while Rules 13.3 and 13.4 place further restrictions on the use of the increment/decrement and assignment operators, and they reference behaviours that are
 already referenced by Rule 13.2.<br>
<br>
Note that Rule 13.2 is Required/Undecidable, while Rules 13.3 and 13.4 are Advisory/Decidable. This indicates that their inclusion may help prevent problems that could otherwise skip through the enforcement of Rule 13.2, due to undecidability.<br>
<br>
Aside from undefined/unspecified behaviours, the rationales of Rules 13.3 and 13.4 also mentions the issue of code readability.<br>
<br>
Other potential problems that Rule 13.4 can prevent are those related with developers incorrectly using '=' instead of '=='.<br>
<br>
CERT C includes Rule EXP45-C "Do not perform assignments in selection statements"<br>
<br>
Rule EXP45-C also covers a number of other situations besides selection statements, but only where it is deemed that a programming error may typically occur, therefore its scope would be a subset of MISRA Rule 13.4's.<br>
<br>
TS 17961 Annex B Undefined Behaviour contains a reference to [C18-J.2-35] but this UB does not appear to be referenced by any rules.<br>
</div>
<div><br>
</div>
<div>Fulvio<br>
</div>
<p></p>
</div>
</body>
</html>