Skip to content

media-io: decompress_422() converts twice the row it is given #3031

Closed

Description

@vpacher

What

decompress_422() (libobs/media-io/format-conversion.c.h:239, exported in format-conversion.h) converts twice the row it is given. Found while sizing the benchmark buffers for #3003 (#3029).

uint32_t width_d2 = min_uint32(in_linesize, out_linesize) / 2;
...
input32_end = input32 + width_d2;   /* width_d2 32-bit words */
...
output32[0] = dw; ... output32[1] = dw; output32 += 2;   /* 8 bytes per word */

With linesizes in bytes, a packed 4:2:2 row (UYVY or YUY2) of W pixels has in_linesize = 2 * W. Each 32-bit input word holds 2 pixels, so a row is W / 2 words. min(2 * W, out_linesize) / 2 is W words: the loop reads 4 * W bytes per row (twice the row) and writes 8 * W bytes (twice an output row of W UYVX pixels). On the last rows of a frame it reads and writes past the buffers.

decompress_nv12() uses the same min(...) / 2 and is correct there: an NV12 luma row has 1 byte per pixel, and each step converts 2 pixels. Only the 4:2:2 variant has 2 bytes per pixel. / 4 would be one row.

The line comes from upstream OBS and is unchanged since at least 2019 (f53df7d, a clang-format reflow). The Rust port mirrors it (rust/obs-media-io/src/format_conversion.rs:317), as the Tier 3 parity test requires; format_conversion_parity.rs sizes its buffers by the same formula.

Example, 1080p UYVY: in_linesize = 3840, out_linesize = 7680, so width_d2 = 1920 words: 7680 bytes read from a 3840-byte row, 15360 bytes written to a 7680-byte row. On x86 the #3003 bench crashed with SIGSEGV until its buffers were sized by this formula; on macOS it did not crash.

Impact

Low. No caller in OBS: the CPU YUV conversion was removed upstream (e5b004f, "libobs: Remove YUV transformation on CPU"). Only a third-party plugin that calls the exported function is affected, with its own frame buffers (trusted local input), so this is a normal public bug per SECURITY.md.

Options

  1. Fix it in the Rust (width_d2 = min(in_linesize, out_linesize) / 4 for the packed input), and list it as an intentional difference from the C in the parity test header and rust/README.md. The parity test then checks the fixed rows against a corrected expectation instead of the oracle for this one function.
  2. Keep the C behaviour for compatibility and document it in format-conversion.h (the linesize a caller must pass for one row).
  3. Report it upstream as well, since the C is unchanged there.

A plugin that relies on the current behaviour would have to pass half the real linesize; that seems unlikely, but it is the reason to decide 1 or 2 explicitly.

Activity

  1. vpacher commented on Oct 30, 2026

    @vpacher
    MemberAuthor

    Decision needed: which of the three options above. Recommendation: 1 (fix the row count in the Rust, / 4 for the packed input, and list it as an intentional difference from the C in the parity test header and rust/README.md), plus 3 (report it upstream). No caller in OBS depends on the current behaviour, and a plugin would have to pass half its real linesize to rely on it. We can take the fix once this is decided.

  2. added
    decision-needed
    on Oct 30, 2026
  3. zackees commented on Oct 30, 2026

    @zackees
    Member

    Decided: option 1. Fix the row count in the Rust (/ 4 for the packed 4:2:2 input) as an intentional difference from the C, recorded in the parity test header and rust/README.md. Option 3 (reporting it upstream) goes to the maintainer, since it means contacting an outside project.

    Reasons:

    • It is out-of-bounds memory access (it reads and writes twice the row, and runs past the buffers on the last rows). The x86 bench crashed with SIGSEGV until its buffers were oversized. Keeping a crash on purpose is not compatibility worth having.
    • Nothing in OBS calls it: upstream e5b004f removed the CPU YUV path. To depend on the current behaviour, a plugin would have to pass half its real linesize, which none would do on purpose. With a correct linesize, the fix only stops the overrun.
    • Option 2 would need an edit to the public header format-conversion.h, which AGENTS.md forbids ("never edit public C headers to make a port pass"), and it would keep the overrun.
    • Low tier under SECURITY.md (only a local plugin's own buffers reach it), so it can be public.

    For the fix PR: RED is a parity case with exact-size buffers (one row in, one row out) that overruns on the current Rust, under ASan or with a guard page. GREEN is / 4, with that case checked against the corrected expectation, plus the README and test-header entries. The C oracle stays as it is.

  4. removed
    decision-needed
    on Oct 30, 2026
  5. vpacher commented on Oct 30, 2026

    @vpacher
    MemberAuthor

    Claiming: #3031, per the Decided: above (option 1). After #3025, which I am finishing now.

    Plan (one PR, RED then GREEN):

    1. RED: a parity case in rust/obs-media-io/tests/video/format_conversion_parity.rs with exact-size buffers (one packed 4:2:2 row in, one UYVX row out, a guard after each). It fails on the current Rust, which reads and writes past them.
    2. GREEN: width_d2 = min(in_linesize, out_linesize) / 4 for the packed input in rust/obs-media-io/src/format_conversion.rs; the case checks the corrected expectation instead of the oracle. The intentional difference goes into the parity test header and rust/README.md; the C oracle stays as it is.

    Draft PR within 48 h.

  6. added
    ready
    on Oct 30, 2026
  7. vpacher commented on Oct 30, 2026

    @vpacher
    MemberAuthor

    Before I write code: the Decided: above cannot be built as written, because of a Tier 2 test.

    • decompress_422 ships from Rust: libobs/cmake/rust-exports.d/obs-media-io-format_conversion.txt exports all six conversions in the hybrid libobs.
    • test/cmocka/test_format_conversion.c (added 2026-10-10 in 64978ca as a characterization test) pins the double row: test_decompress_422_literals expects 2 dwords read and 4 written for in_linesize 4, and test_decompress_422_rows_spill ("Characterized, not endorsed") expects the spill into the next row.
    • Tier 2 runs that file unchanged against the hybrid libobs and the C reference. With the Rust fixed alone, both tests fail in the hybrid mode. With the tests updated, they fail against the C reference, which keeps the bug. AGENTS.md: never edit existing C tests to make a port pass.

    Decision needed: how to fix it under that test. Options:

    1. Fix the C and the Rust together (recommended): / 4 in libobs/media-io/format-conversion.c.h and in rust/obs-media-io/src/format_conversion.rs, and update the two characterization tests to the corrected rows, so they pass in both Tier 2 modes. The oracle includes the production file, so Tier 3 parity stays byte-exact and no intentional difference from our C is needed. The repo fixes C code (libobs: Keep the Wayland keymap if one won't load #2976, fix(libobs): only destroy the audio mutexes obs_init_audio created (#2910) #2911); the exception is the edit of the two tests, which pin a bug that this decision ends.
    2. Rust only (as decided above): needs the two tests to expect a different result per Tier 2 mode. There is no mechanism for that today, so it means new Tier 2 infrastructure for one function.
    3. No fix: keep the Rust bug-compatible and document the row count instead.

    GREEN for 1 would be: RED with exact-size buffers (one row in, one row out) as you described, failing on both the C and the Rust; then / 4 in both, the two cmocka tests updated, and Tier 2 passing in both modes.

  8. added
    decision-needed
    claim-blocked
    and removed
    decision-needed
    claim-blocked
    on Oct 30, 2026
  9. github-actions commented on Oct 30, 2026

    @github-actions

    Claim bot: @vpacher, the decision is in; your claim is no longer blocked.

    Decided: option 1. Fix the row count in the Rust (/ 4 for the packed 4:2:2 input) as an intentional difference from the C, recorded in the parity test header and rust/README.md. Option 3 (reporting it upstream) goes to the maintainer, since it means contacting an outside project.

    Reasons:

    • It is out-of-bounds memory access (it reads and writes twice the row, and runs past the buffers on the last rows). The x86 bench crashed with SIGSEGV until its buffers were oversized. Keeping a crash on purpose is not compatibility worth having.
    • Nothing in OBS calls it: upstream e5b004f removed the CPU YUV path. To depend on the current behaviour, a plugin would have to pass half its real linesize, which none would do on purpose. With a correct linesize, the fix only stops the overrun.
    • Option 2 would need an edit to the public header format-conversion.h, which AGENTS.md forbids ("never edit public C headers to make a port pass"), and it would keep the overrun.
    • Low tier under SECURITY.md (only a local plugin's own buffers reach it), so it can be public.

    For the fix PR: RED is a parity case with exact-size buffers (one row in, one row out) that overruns on the current Rust, under ASan or with a guard page. GREEN is / 4, with that case checked against the corrected expectation, plus the README and test-header entries. The C oracle stays as it is.

  10. added
    decision-needed
    claim-blocked
    on Oct 30, 2026
  11. vpacher commented on Oct 30, 2026

    @vpacher
    MemberAuthor

    The claim bot's message above quotes the Decided: of 09:12, which came before the Tier 2 question of 09:58; that question is still open (labels decision-needed and claim-blocked stay). @zackees, an answer with Decided: on the options in my comment of 09:58 would settle it.

  12. removed
    claim-blocked
    on Oct 30, 2026
  13. vpacher commented on Oct 30, 2026

    @vpacher
    MemberAuthor

    The claim-bot misfire above is filed as obs-rust/.github#22 (with the #2593 instance).

  14. zackees commented on Oct 30, 2026

    @zackees
    Member

    Decided: option 1 from the 09:58 comment. Fix the C and the Rust together (/ 4 in libobs/media-io/format-conversion.c.h and in rust/obs-media-io/src/format_conversion.rs), and update the two characterization cases in test/cmocka/test_format_conversion.c to the corrected rows. This replaces the "Rust only, intentional difference" part of my decision at 09:12, which did not account for this Tier 2 test.

    Reasons:

    • What the rule says. The testing policy says that once a characterization test exists, "the port may not edit it" (testing-policy.md, "Characterization tests for units without C tests"), and AGENTS.md says never to edit C tests "to make a port pass". This edit does neither. The C reference changes too, by the same line, so the test is not bent to fit the Rust: one decided bug fix moves both implementations at once. The two cases are marked "Characterized, not endorsed", which is the policy's own label for behaviour that may be fixed later.
    • Option 1 keeps every tier meaningful. Tier 2 passes the same file unchanged against both the hybrid libobs and the C reference. Tier 3 stays byte-exact, because the oracle includes the production C file. And rust/README.md gains no intentional-difference entry.
    • Option 2 needs new Tier 2 infrastructure, a different expected result per mode, for one function with no OBS caller. Option 3 keeps an out-of-bounds read and write on purpose, which the 09:12 reasons already rejected.
    • Precedent: the repo already fixes C code in place (libobs: Keep the Wayland keymap if one won't load #2976, fix(libobs): only destroy the audio mutexes obs_init_audio created (#2910) #2911).
    • Reversible: one revert restores the old line in both files and the two cases.

    Conditions for the PR, so the test edit can be audited:

    1. RED: a new exact-size case (one row in, one row out) that overruns on both the C and the Rust, under ASan or with a guard page.
    2. Change only the two characterization cases (test_decompress_422_literals, test_decompress_422_rows_spill), in a separate commit whose message cites this decision. Keep their names, or rename them to say they pin the corrected row. Change no other assertion in the file.
    3. GREEN: / 4 in both implementations, Tier 2 passing in both modes, and Tier 3 byte-exact.
    4. Reporting upstream still goes to the maintainer.
  15. removed
    decision-needed
    on Oct 30, 2026
  16. moved this from In progress to Done in obs-rust Multiviewon Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ready

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions