Repository navigation
Description
Summary
obs_scene_enum_items remembers the next item before it calls the callback, and only holds a reference on the current one. If the callback removes a later item with obs_sceneitem_remove, the walk either visits an item that was just removed or steps onto one that has been freed. Severity: high for code that removes items during an enumeration, because the result is a use-after-free; code that only removes the current item is not affected.
Steps / trigger
Public API only, on a scene with four items:
- Call
obs_scene_enum_items(scene, cb, param). - In
cb, on the first item, callobs_sceneitem_removeon the second (or the second and third). - If someone else still holds the second item, the callback is called with the removed item. If the scene owned the only reference, the walk reaches a freed item (a crash in testing).
Removing the current item in the callback is fine. obs_sceneitem_group_enum_items calls this function, so it has the same problem.
Cause
Line numbers are against current master.
libobs/obs-scene.c:2111:next = item->nextis read before the callback.libobs/obs-scene.c:2113: only the currentitemgetsobs_sceneitem_addref.libobs/obs-scene.c:2115-2122: after the callback the loop doesitem = next, whatever the callback did.libobs/obs-scene.c:2405,:2410,:2433:obs_sceneitem_removemarks the item removed, detaches it from the list and drops the scene's reference, which frees an item nobody else holds.
The scene code is C only, so ON and OFF behave the same.
Proposed fix
While holding the scene lock, take a reference on every item into a snapshot array, walk the snapshot, and release the references at the end. Skip any snapshot item that is marked removed or is no longer in this scene's list. Membership is checked against scene->first_item and its links, which the held scene lock protects, rather than by reading fields of an item that may have moved into a group under a different lock. One behaviour change: items added by the callback during the walk are no longer visited by that walk. The regression test uses the public scene API and covers removal of the next item, of two adjacent items, items moved into or out of a group, early exit on a false return, signal re-entry and reference release. It also runs clean under ThreadSanitizer.
I'd like to work on this; a fix with a regression test is ready.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
zackees commentedon Oct 13, 2026
@chriscoveries go ahead: please open the PR with the RED test commit first, then the fix (one PR per issue, targeting master). Marking it claimed for you so nobody duplicates it.
chriscoveries commentedon Oct 14, 2026
Releasing this one. We're backed up on CI and won't get it landed soon, so anyone who wants it is welcome to take it.
If it helps, our RED test and fix are on the fork branch
fix/scene-enum-items-removal. They passed review on our side but haven't been through CI, so reuse them or start fresh, whichever you prefer.zackees commentedon Oct 14, 2026
Claiming.
libobs: obs_scene_enum_items must not walk items removed by its callb…