Skip to content

bug: libobs: scene item crops load unclamped, and out-of-range crop and group size casts give wrong sizes #1839

Closed

Description

@rdoupe

Upstream bug: obsproject/obs-studio master (7fc60ea38) has the same code in libobs/obs-scene.c; no upstream issue found. OBS never writes these values itself, so this only shows with scene collections edited by hand or written by other tools. The result is a wrong size, not memory corruption.

Location

libobs/obs-scene.c.h on master 209d977:

  • scene_load_item(), lines 1046-1049: the crop from the file goes straight into the item's int fields.
    item->crop.left = (uint32_t)obs_data_get_int(item_data, "crop_left");
    obs_sceneitem_set_crop() (2997-3004) clamps a negative crop to 0; the load does not.
  • calc_cx() and calc_cy(), lines 355 and 361: four int crops are added up in an int, which can overflow.
    uint32_t crop_cx = item->crop.left + item->crop.right + item->bounds_crop.left + item->bounds_crop.right;
    return (crop_cx > width) ? 2 : (width - crop_cx);
  • update_nested_scene_crop(), lines 388-391: (int)((float)item->crop.left * scale_x), undefined when the product is outside the int range.
  • render_item(), lines 762-763: (float)(item->crop.left + item->bounds_crop.left), also an int sum.
  • resize_scene_base(), lines 3285-3286: a group's size is (uint32_t)ceilf(scale->x), undefined when the extent is outside the uint32_t range.

Class

Logic error / integer overflow (undefined behaviour in C). The result is a wrong size, not memory corruption.

How it is reached

Loading a scene collection whose scene item has, for example, "crop_left": -100. Before the crop sum is compared, it is converted to unsigned, so it comes out larger than any source and the item is drawn 2 px wide (or high). A crop of 2^31 or more comes back with the other sign (3000000000 is loaded as -1294967296). Then:

  • a nested scene with a large crop that grows a lot (a crop of 1000000000 on a 1x1 scene that becomes 4000x4000) gets an INT_MIN crop on x86-64, so it is drawn 2 px wide;
  • a group whose sources are 1e20 px apart gets a size of 0 on x86-64.

Memory safety: checked; these give only wrong sizes. Each item's drawn size is at most the source size, or 2. The crop only goes into the render matrix as a float translation. A huge group size makes texture creation fail. A scene collection can already set a custom scene size of up to UINT32_MAX anyway. The frontend and obs-websocket use obs_sceneitem_get_crop() only for arithmetic and display, never as an index or allocation size.

Proposed fix and failing test

  • Load: read each crop as the nearest int, then set it through obs_sceneitem_set_crop(), so the setter's clamp is the only rule.
  • Casts and sums: add the crops in 64 bits, and give each float cast the nearest value in range (0 for NaN or a negative size). Every value in range gives the result it gave before.
  • The arithmetic moves to static inline helpers in obs-scene.h, so a test can build it with UBSan.

Tests:

  • test_scene_item_crop_load loads sources and scenes from a JSON string with obs_load_sources() through the real libobs. It covers a negative crop, a crop past the int range, a crop sum past the int range, a nested scene that grows, a group 1e20 px wide and an in-range control. On master 4 of 6 fail: the negative crop is kept and the item is drawn 2 x 2; 3000000000 comes back as -1294967296; the grown nested crop is INT_MIN; the group is 0 x 0.
  • test_scene_crop_steps covers the helpers with -fsanitize=undefined,float-cast-overflow -fno-sanitize-recover. With master's arithmetic it fails with runtime error: signed integer overflow: 2147483647 + 2147483647 cannot be represented in type 'int'.

Activity

  1. rdoupe commented on Oct 22, 2026

    @rdoupe
    CollaboratorAuthor

    Claiming: #1839. Tests and fix are ready; PR follows.

  2. added
    claimed
    on Oct 22, 2026
  3. added a commit that references this issue on Oct 22, 2026
    b4bb789
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    claimed

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions