OMNeT++/OMNEST Bug Tracker - OMNeT++
View Issue Details
0000633OMNeT++simulation kernelpublic2013-03-06 09:372017-02-01 15:36
0000633: left shift of negative numbers invokes undefined behaviour in C++
It is undefined behavior in C++ and C to left-shift negative integers. The actual C standard has this in section 6.5.7 paragraph 4 in C++ this is in 5.8 paragraph 3. It is also in the older standards but I don't have the reference at hand.

The problem is with the definitions of FSM_Transient and FSM_Exit and can be seen in the samples/routing/ FSM_Transient and FSM_Exit are

#define FSM_Transient(state) (-(state))
#define FSM_Exit(state) (((state)<<1)|1)

So the enum member declaration

SEND = FSM_Transient(1)

and the case:

  case FSM_Exit(SEND)

expand to

  case ((-1 << 1) | 1):

which invokes undefined behaviour.

The fix is to modify FSM_Exit to

#define FSM_Exit(state) (((state) * 2) | 1)

Given that this is mostly calculated at compile time there is no runtime difference and even the runtime calculation would probably converted to a shift by the compiler.
No tags attached.
Issue History
2013-03-06 09:37HartiNew Issue
2013-03-06 09:39HartiNote Added: 0000795
2017-02-01 15:36andrasNote Added: 0001295
2017-02-01 15:36andrasStatusnew => resolved
2017-02-01 15:36andrasFixed in Version => 5.1pre3
2017-02-01 15:36andrasResolutionopen => fixed
2017-02-01 15:36andrasAssigned To => andras

2013-03-06 09:39   
FSM_Enter should also be modified

#define FSM_Enter(state) ((state) * 2)
2017-02-01 15:36   
Agreed, code will use *2 instead of <<1, to avoid undefined behavior.