Longan nano : a new gcc weird behaviour

 

This is a known quirk of recent compiler. 
It began with Arm, then appeared in apple llvm and now everywhere.
Can you see the error in the following code ? (answer at the end [1])

bool        myMcp23017::dumpRegisters()
{
uint8_t reg=0;
for(int i=0;i<27;i++)
   Logger("Reg 0x%x : 0x%x\n  ",i,reg);
}

The usual bug is there is a crash at returns because the return address is mixed-up.
On riscV, the generated code is as follows :

Dump of assembler code for function _ZN10myMcp2301713dumpRegistersEv:
0x08007f3e <+0>:     addi    sp,sp,-16
0x08007f40 <+2>:     sw      ra,12(sp)
0x08007f42 <+4>:     sw      s0,8(sp)
0x08007f44 <+6>:     sw      s1,4(sp)
0x08007f46 <+8>:     sw      s2,0(sp)
0x08007f48 <+10>:    li      s2,0
0x08007f4a <+12>:    li      s0,0
0x08007f4c <+14>:    lui     s1,0x8001
0x08007f50 <+18>:    mv      a2,s2
0x08007f52 <+20>:    mv      a1,s0
0x08007f54 <+22>:    addi    a0,s1,1608 # 0x8001648
0x08007f58 <+26>:    jal     ra,0x8001f48 <_Z6LoggerPKcz>
0x08007f5c <+30>:    addi    s0,s0,1
0x08007f5e <+32>:    j       0x8007f50 <_ZN10myMcp2301713dumpRegistersEv+18>

What is the problem ?
The comparison is completely missing !
So it will loop forever. 
Took me a while to understand it, as the effect of the mistake is not the usual one.


[1] return true/false is missing


Comments

Popular posts from this blog

Component tester with STM32 : Part 1 ADC, Resistor

Fixing the INA3221

INA3221, weird wiring