willowsystems / jSignature

jQuery plugin - draw signature in browser.
720 stars 266 forks source link

Fix TypeError in PubSubClass.unsubscribe #44

Closed mgedmin closed 6 years ago

mgedmin commented 10 years ago

Modifying an array while iterating over it is tricky. If we delete an element in the middle and continue to iterate using the old length, we'll perform an access beyond the end of the array, get back an undefined, and then get a TypeError when we try to [0] it.

I've seen this in the wild, when attempting to show a popup that embeds jSignature for the second time on the same page. The error is uncaught and disables all subsequent JavaScript on the page.

I don't know if this method was meant to remove just the first matching callback, or all of them. My modification assumes the first; if you need the second, instead of break do i--; l--.

mgedmin commented 10 years ago

My fix is incomplete: I still get a TypeError when I try to show a popup with a signature the second time, only the new error is in a different place.

It looks like jSignature is trying to remove a PubSub subscriber from inside a PubSub callback handler, which makes the dispatch code iterate over more array indexes than remain. This one is harder to fix. Idea: copy the array before looping through it in the publish method.