[code] [textadept] Patch for events

From: D K <mccoy.att.doctor.com>
Date: Thu, 14 Jul 2016 10:47:05 +0200
First of all, thanks for an excellent text editor.
 
While scripting against textadept, I discovered two issues in core/events.lua.
 
The first issue is in events.connect and events.disconnect. When inserting a new event handler that is not at the length of the sequence+1 (#handlers[event]+1), or when removing an old event handler that is not at the length of the sequence (#handlers[event]), other event handlers that uses the callback function to lookup their respective indeces in handlers[event] will be affected since table.remove/table.insert will shift all event handlers following the affected (removed/added) index in handlers[event].
 
Before the defect fix, the following could therefore be true after using events.connect (with an index in the middle of the list) and events.disconnect (when removing an event that wasn't the last one in the list). It is assumed the callback function f exists in handlers[event]:
 
local index = handlers[event][f]
handlers[event][index] ~= f
 
The consequence of that could in turn mean that when removing further event callbacks from the same event type, you can potentially remove an unrelated callback.
 
The second defect concerns removing an event handler while events.emit is being processed. Since this will affect the length of the sequence handlers[event], the main for loop in events.emit will try to process more events than is actually available (it has already evaluated the length of the sequence in the for statement). By checking if we get a nil value and that the length of the array has been decreased, we exit the loop.
 
The patch is against textadept-0cb79f5e9960, but it might work with later versions.
 
Best regards
/David Karell

-- 
You are subscribed to code.att.foicica.com.
To change subscription settings, send an e-mail to code+help.att.foicica.com.
To unsubscribe, send an e-mail to code+unsubscribe.att.foicica.com.
Received on Thu 14 Jul 2016 - 04:47:05 EDT

This archive was generated by hypermail 2.2.0 : Thu 14 Jul 2016 - 06:42:02 EDT