Commit graph

1 commit

Author SHA1 Message Date
Vadim Peretokin
a8b0b06c32
fix: Mudlet no longer crashes when adding an event handler after switching scripts (#9839)
#### Brief overview of PR changes/additions
- `slot_scriptsSelected()` now drops the noted "Add User Event" item
after tearing the Registered Events list down, instead of relying on
`saveScript()` doing it beforehand. `QListWidget::clear()` drops the
selection before it deletes the items, and that selection change runs
`slot_scriptMainAreaEditHandler()`, which re-notes the item that is
about to be freed.
- Also releases the row the "-" button takes out of that list -
`takeItem()` hands ownership over, and the returned item was being
dropped.
- New `ScriptEventHandlerLifetimeTest` covers the four ways the list
gets torn down while an entry is noted (switch script, re-click the same
script, add a script, jump from the search results), plus renaming and
deleting so the fix cannot overreach.

#### Motivation for adding to Mudlet
Pressing "+" after switching scripts dereferenced freed memory and
killed the client, losing whatever was unsaved.

#### Other info (issues closed, discussion etc)
Fixes #9835. Reported on Windows 10 / Mudlet 4.22.0, and reproduced on
Windows 11 against both 4.22.0 and the current PTB.

Confirmed under ASan as a `heap-use-after-free` in
`QListWidgetItem::text()` from `slot_scriptMainAreaAddHandler()`, freed
by `QListModel::clear()` from `slot_scriptsSelected()`. Where the
replacement item happens to land on the freed block there is no crash
and "+" silently renames the wrong entry instead.

Note this also stops a name typed into "Add User Event" but never added
from following you to the next script.

**Test case:** Script editor > new script, type an event name into "Add
User Event" and press "+", save. Make a second script, save. Select the
first script, click its entry under "Registered Events", then click the
second script and press "+" - 4.22.0 crashes, this branch adds the
handler to the second script.

Assisted-by: Claude:claude-opus-5
Assisted-by: Claude:claude-fable-5
Signed-off-by: Vadim Peretokin <vadim.peretokin@mudlet.org>

---------

Signed-off-by: Vadim Peretokin <vadim.peretokin@mudlet.org>
2026-08-13 13:37:57 +02:00