Fix crash with events in the past
Fix crash with events in the past

ticker_insert_event() can crash on KLXX (and probably other platforms) if an event is inserted with a timestamp before the current real time.  

The problem is easy to trigger:  you just need to set up a Ticker object, and then disable interrupts for slightly longer than the Ticker object's interval.  It's generally bad practice to disable interrupts for too long, but there are some cases where it's unavoidable, and anyway it would be better for the core library function not to crash.  The case where I had an unavoidably long interrupts-off interval was writing flash with the FTFA.  The FTFA hardware prohibits flash reads while an FTFA command is in progress, so interrupts must be disabled for the whole duration of each command to ensure that there are no instruction fetches from flash-resident ISRs in the course of the execution.  An FTFA "erase sector" command takes a fairly long time (milliseconds), and I have a fairly high frequency Ticker (1ms).

The problem and the fix are pretty straightforward.  ticker_insert_event() searches the linked list to figure out where to insert the new event, looking for a spot earlier than any event currently queued.  If the event is in the past, it'll usually end up at the head of the list.  When the routine sees that the new event belongs at the head of the list, it calls data->interface->set_interrupt() to schedule the interrupt for the event, since it's the new soonest event.  The KLXX version of us_ticker_set_interrupt() then looks to see if the event is in the past, which we've stipulated that it is, so rather than actually setting the interrupt, it simply calls the handler directly.  The first thing the Ticker interrupt handler does is re-schedule itself, so we re-enter ticker_insert_event() at this point.  This is where the problem comes in:  we didn't finish updating the linked list before we called set_interrupt() and thus before we recursed back into ticker_insert_event().  We set the head of the list to the new event but we didn't set the new event's 'next' pointer.

The fix is simply to finish updating the list before we call set_interrupt(), which we can do by moving the obj->next initialization ahead of the head pointer update.
bcostm/mbed-os,fahhem/mbed-os,pradeep-gr/mbed-os5-onsemi,YarivCol/mbed-os,catiedev/mbed-os,fanghuaqi/mbed,CalSol/mbed,Archcady/mbed-os,svogl/mbed-os,kl-cruz/mbed-os,netzimme/mbed-os,svogl/mbed-os,ryankurte/mbed-os,fanghuaqi/mbed,svogl/mbed-os,adamgreen/mbed,mikaleppanen/mbed-os,andcor02/mbed-os,bulislaw/mbed-os,netzimme/mbed-os,catiedev/mbed-os,YarivCol/mbed-os,monkiineko/mbed-os,nRFMesh/mbed-os,karsev/mbed-os,nRFMesh/mbed-os,karsev/mbed-os,NXPmicro/mbed,pradeep-gr/mbed-os5-onsemi,Archcady/mbed-os,kl-cruz/mbed-os,mikaleppanen/mbed-os,andcor02/mbed-os,fahhem/mbed-os,infinnovation/mbed-os,screamerbg/mbed,mazimkhan/mbed-os,NXPmicro/mbed,catiedev/mbed-os,betzw/mbed-os,HeadsUpDisplayInc/mbed,netzimme/mbed-os,HeadsUpDisplayInc/mbed,c1728p9/mbed-os,catiedev/mbed-os,YarivCol/mbed-os,kjbracey-arm/mbed,bulislaw/mbed-os,karsev/mbed-os,YarivCol/mbed-os,netzimme/mbed-os,infinnovation/mbed-os,andcor02/mbed-os,c1728p9/mbed-os,mazimkhan/mbed-os,NXPmicro/mbed,adamgreen/mbed,nRFMesh/mbed-os,YarivCol/mbed-os,pradeep-gr/mbed-os5-onsemi,adamgreen/mbed,mazimkhan/mbed-os,screamerbg/mbed,catiedev/mbed-os,screamerbg/mbed,mazimkhan/mbed-os,svogl/mbed-os,kl-cruz/mbed-os,YarivCol/mbed-os,andcor02/mbed-os,mbedmicro/mbed,mbedmicro/mbed,karsev/mbed-os,CalSol/mbed,Archcady/mbed-os,nRFMesh/mbed-os,arostm/mbed-os,arostm/mbed-os,monkiineko/mbed-os,monkiineko/mbed-os,nRFMesh/mbed-os,HeadsUpDisplayInc/mbed,kjbracey-arm/mbed,c1728p9/mbed-os,bcostm/mbed-os,bcostm/mbed-os,fahhem/mbed-os,mikaleppanen/mbed-os,fanghuaqi/mbed,svogl/mbed-os,NXPmicro/mbed,NXPmicro/mbed,Archcady/mbed-os,infinnovation/mbed-os,mazimkhan/mbed-os,bcostm/mbed-os,netzimme/mbed-os,betzw/mbed-os,screamerbg/mbed,ryankurte/mbed-os,mikaleppanen/mbed-os,bcostm/mbed-os,pradeep-gr/mbed-os5-onsemi,catiedev/mbed-os,kjbracey-arm/mbed,CalSol/mbed,screamerbg/mbed,infinnovation/mbed-os,HeadsUpDisplayInc/mbed,betzw/mbed-os,mbedmicro/mbed,ryankurte/mbed-os,c1728p9/mbed-os,pradeep-gr/mbed-os5-onsemi,mmorenobarm/mbed-os,mmorenobarm/mbed-os,NXPmicro/mbed,Archcady/mbed-os,kjbracey-arm/mbed,nRFMesh/mbed-os,mbedmicro/mbed,mmorenobarm/mbed-os,monkiineko/mbed-os,betzw/mbed-os,mbedmicro/mbed,fanghuaqi/mbed,netzimme/mbed-os,adamgreen/mbed,arostm/mbed-os,mmorenobarm/mbed-os,monkiineko/mbed-os,screamerbg/mbed,betzw/mbed-os,adamgreen/mbed,infinnovation/mbed-os,bulislaw/mbed-os,karsev/mbed-os,c1728p9/mbed-os,fanghuaqi/mbed,CalSol/mbed,bulislaw/mbed-os,fahhem/mbed-os,kl-cruz/mbed-os,svogl/mbed-os,andcor02/mbed-os,mazimkhan/mbed-os,arostm/mbed-os,pradeep-gr/mbed-os5-onsemi,mmorenobarm/mbed-os,ryankurte/mbed-os,fahhem/mbed-os,mikaleppanen/mbed-os,Archcady/mbed-os,karsev/mbed-os,bulislaw/mbed-os,fahhem/mbed-os,bcostm/mbed-os,HeadsUpDisplayInc/mbed,CalSol/mbed,arostm/mbed-os,c1728p9/mbed-os,bulislaw/mbed-os,monkiineko/mbed-os,andcor02/mbed-os,mmorenobarm/mbed-os,HeadsUpDisplayInc/mbed,infinnovation/mbed-os,mikaleppanen/mbed-os,CalSol/mbed,ryankurte/mbed-os,kl-cruz/mbed-os,ryankurte/mbed-os,arostm/mbed-os,kl-cruz/mbed-os,betzw/mbed-os,adamgreen/mbed