Bug 5971

Summary: Associativity of C operators
Product: frysk Reporter: Teresa Thomas <tthomas>
Component: generalAssignee: Unassigned <frysk-bugzilla>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: unspecified   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:
Project(s) to access: ssh public key:
Bug Depends on:    
Bug Blocks: 2246    

Description Teresa Thomas 2008-03-19 16:11:04 UTC
Incorrect evaluation for C operators that associate right-to-left.

Frysk:
(fhpd) print i
1
(fhpd) print i++ + ++i + i 
7

But on a GCC compiler:
int i=1;
printf ("%d\n", i++ + ++i + i);

Output: 6


Gdb also seems to have this bug:
(gdb) print i
$2 = 1
(gdb)  print i++ + ++i + i
$3 = 7
Comment 1 Petr Machata 2008-03-19 17:03:26 UTC
I believe this hits undefined behavior, because you change "i" twice between two
sequence points.  I would notabug this.
Comment 2 Teresa Thomas 2008-03-19 17:30:20 UTC
Even if I do something like: 

(fhpd) print i = 1
1
(fhpd) print i++ + i
3

frysk and gdb gives result 3, whereas C evaluates it to a 2, which is why I
think its a bug with associativity.
Comment 3 Petr Machata 2008-03-19 18:29:35 UTC
But that expression has the same problem.  You can't both change and read the
number in one expression (or, more preciselly, between two sequence points). 
You may want to read the exact wording of the C or C++ standard, but FWIW,
expressions like these always make me feel icky.
Comment 4 Teresa Thomas 2008-03-19 19:17:17 UTC
Closing bug. 

Would be a neat feature to get frysk to warn users of possibly undefined operations.