Re:How to Get Fired Using Switch Statements and Statement Ex...
by yorwba on Friday October 28, 2016 @09:25AM
(#12815504)
i is initialized twice in the example code
int i = 0;
switch(i){
for(i = 0; i < ({case 0:; 10;}); i++){
The compiler probably tried to be intelligent and optimized it away.EDIT: Okay I tried reading the assembly and I think I understand whats going on:
movl $0, -8(%rbp) # translation of i = 0
... # the following is jumped over
movl -8(%rbp), %eax
movl %eax, -16(%rbp) # 4-byte Spill # i is moved to somewhere else in memory
... # the switch jumps between these
movl -16(%rbp), %eax # 4-byte Reload # tries to reload i from its "new" position
Whoever wrote the code to place variables into memory obviously didn't consider this kind of insane control flow.
Re:How to Get Fired Using Switch Statements and Statement Ex...
by jjnoakes on Friday October 28, 2016 @12:00PM
(#12817405)
Yep, you are right, I missed that "i" was initialized twice; when I see "i=0" in a for loop I assume it isn't initialized right above (otherwise, why put the "i=0" in the for loop at all?).
The code you wrote did one additional thing: it moved the "switch" starting point from before the "i=0" initialization to after the "i=0" initialization, which is more than just a different interpretation, it's completely different semantics.