Skip to content

libobs: Check the CoreAudio device list allocation - #2863

Merged
zackees merged 3 commits into
masterfrom
fix/bug-enum-audio-devices-doesn-t-c-243
Oct 29, 2026
Merged

zackees merged 3 commits into
masterfrom
fix/bug-enum-audio-devices-doesn-t-c-243

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #2862

What and why

enum_audio_devices() in libobs/audio-monitoring/osx/coreaudio-enum-devices.c.h allocates the CoreAudio device list with malloc() and passes the result straight to AudioObjectGetPropertyData() as the output buffer. When the allocation fails, CoreAudio is asked to write the device ids into NULL, and if it reports success the loop reads ids[i] from NULL. Every walk of the device list goes through it: the monitoring device list, devices_match() and get_desktop_default_id().

The fix returns when ids is NULL, before the property read, so the caller sees an empty device list, as it already does when the size query fails. Every call and read stays where it was and under its condition, and no log message changes. The same code is in upstream obs-studio (no upstream issue).

The new test, test_coreaudio_enum_alloc, compiles the file over stand-ins for CoreAudio, CoreFoundation and the libobs calls it makes (test/cmocka/coreaudio-enum-stubs/, found first as build/libobs-macos.pch.h), so it needs no Apple header or macOS SDK. It has been built and run only on Linux (gcc and clang, locally; PR CI runs it in the Linux Tier 2 lane); no native macOS or Windows Tier 2 run has built it. The fake HAL counts a read of the device list into NULL and refuses it with an error, so the bug fails the test instead of crashing it. It runs under ASan and UBSan.

RED → GREEN

  • RED commit: f1fa9b1 (test: Fail the CoreAudio device list allocation). test_coreaudio_enum_alloc fails 3 of 7: test_failed_allocation_reads_nothing, test_devices_match_failed_allocation and test_desktop_default_failed_allocation (the device list is read into NULL once, twice and once); the four cases that pin the normal walk pass. Built with gcc (ASan/UBSan) and clang.
  • GREEN: bd5fdc2 (libobs: Check the CoreAudio device list allocation). test_coreaudio_enum_alloc passes 7/7 (gcc with ASan/UBSan, clang) and runs under ctest. The local gate's --test-diff origin/master (whole workspace: 4870 Rust tests, 0 failed, 6 skipped as on master), lint and cross lanes pass, and soldr cargo clippy --workspace --target x86_64-pc-windows-msvc --all-targets is clean; runs were on bd5fdc2. The changed file builds only on macOS, so the exported symbol sets of the Linux C, hybrid and C-reference libobs don't change. The full Linux Tier 2 suite was not run locally (no Docker access on the build machine), so the head is not stamped and PR CI runs the full suite.
  • Real app (UI, protocol output or logging changes): not applicable, no UI, protocol or log change; the failure path needs a failed allocation.

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits (N/A for docs-only; a focused CI fix may push RED and GREEN together) (RED, then GREEN: this is a bug fix, not a port, so no separate characterization commit; the RED commit's four passing cases pin the normal walk, and the file's characterization comes with its port)
  • User-visible change: its line in docs/features.md is added or updated (Fixed upstream bugs, Stability)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): built with scripting ON (PR CI builds no OBS app: run gh workflow run ci.yml --ref <branch> for the full builds) and cross-checked with clang and MSVC (not applicable: no warning or flag change)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports) (not applicable: not a port)

@zackees

zackees commented Oct 29, 2026

Copy link
Copy Markdown
Member

Status from the coordinator: the branch is red on its latest run (40815028760) in Tier 2 Linux (tests OFF): test_coreaudio_enum_alloc fails at test/cmocka/test_coreaudio_enum_alloc.c:294 — test_failed_allocation_reads_nothing, test_devices_match_failed_allocation and test_desktop_default_failed_allocation, with enum_audio_devices: get data failed: -1 and 1 != 0. 99% of 593 tests passed. Log: https://github.com/obs-rust/obs-studio/actions/runs/40815028760/job/123255649878. Still a draft; no queue action from me.

@rdoupe
rdoupe force-pushed the fix/bug-enum-audio-devices-doesn-t-c-243 branch from d115f48 to 342cbfe Compare October 29, 2026 06:17
@zackees

zackees commented Oct 29, 2026

Copy link
Copy Markdown
Member

Coordinator: the branch is green now, so the only thing left before the queue is marking it ready.

Still a draft, so no queue action from me. Mark it ready when you are satisfied and I will put it in the merge queue on the next tick.

@rdoupe
rdoupe marked this pull request as ready for review October 29, 2026 08:06
@rdoupe
rdoupe requested a review from zackees as a code owner October 29, 2026 08:06
@zackees
zackees added this pull request to the merge queue Oct 29, 2026
@obs-rust-project-manager obs-rust-project-manager Bot moved this to In merge queue in obs-rust Multiview Oct 29, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 29, 2026
@zackees
zackees added this pull request to the merge queue Oct 29, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 29, 2026
@zackees
zackees added this pull request to the merge queue Oct 29, 2026

@zackees zackees left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coordinator review (no changes requested; the queue entry stands).

Verified in the diff

  • The fix is the minimal correct one. ids = malloc(size); if (!ids) return; sits before the property read, so the failure path is exactly the one the size query already takes (empty list), nothing is logged that was not logged before, and the free(ids) at the end is skipped only when ids is NULL, so there is no leak. count is still derived from size and is now only used on the success path.
  • RED is honest. The fake HAL counts a read of the device list into NULL and refuses it, so the old code fails test_failed_allocation_reads_nothing on device_list_reads instead of crashing the test, and the four cases that pin the normal walk (device order, the input-only device skipped through kAudioDevicePropertyStreams, stop at the first false) show the assertions are not vacuous.
  • Every entry point the bug reached is covered: obs_enum_audio_monitoring_devices(), devices_match() (both the equal-UID and the "default" path) and get_desktop_default_id() (system default is not a loopback, so the loopback walk runs; and the bzalloc(1) empty-string fallback). I traced those three paths against coreaudio-enum-devices.c.h and the expected values match, including "" for the failed-allocation case.
  • Housekeeping is right: the ledger line is in docs/features.md, the test is registered in test/cmocka/build/test-graph.json with address,undefined, and the graph's partitions still cover every test and target (rust/obs-build/tests/native.rs asserts that, and CI Gate is green).
  • No C test or public header is touched, and the changed file is macOS-only, so the Linux C, hybrid and C-reference symbol sets do not move.

Notes, none blocking

  1. The stub reimplements astrstri() and cfstr_copy_cstr(), so device_is_input() is exercised against the copy rather than libobs's. That is the right trade for a test that links no libobs, and the stub says so ("As libobs's: ASCII case only"); it is worth keeping that sentence if the stub is ever extended, because a change in libobs's folding semantics would otherwise not show up here.
  2. enum_audio_devices() returns before count is used, so count could move below the check as a one-line tidy. Not worth a commit on its own; fold it in if this file is touched again.
  3. The description is honest that no native macOS or Windows Tier 2 run built the test. The stubs are portable and the source under test is macOS-only, so nothing else needs a lane for it; the macOS verdict comes from the file compiling there, which the macOS 🍏 lane covers.

Reviewed by the coordinator on 2026-10-29.

@rdoupe

rdoupe commented Oct 29, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. On the notes:

  1. Kept as is. The stand-in astrstri() keeps its "As libobs's: ASCII case only" comment, so a later change to libobs's folding has a pointer to the copy.
  2. Leaving count where it is for now: moving it is behaviour-neutral and would rerun every lane on its own. It can go below the check whenever this file is next touched (the port of these rules is planned next in this file).
  3. Agreed; nothing more to add there.

On the two queue ejections (runs 40840984942 and 40847888429): both failed only Rust Tier 2 (macos-26), on four conversion errors in libobs/obs-cocoa.m.h (lines 161, 727, 836, 861) that the libobs ratchet in #2877 turned on. This PR's macOS path is what selects that lane in the merge group; master pushes don't run it, so master stays green. #2892 casts exactly those four, so this PR should go through unchanged once that lands.

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 29, 2026
@rdoupe
rdoupe added this pull request to the merge queue Oct 29, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Oct 29, 2026
@zackees

zackees commented Oct 29, 2026

Copy link
Copy Markdown
Member

Why this left the merge queue (coordinator, 2026-10-29)

Not your diff. The merge group for pr-2863 (f8ced38a1) failed in Build 🧱 / Rust Tier 2 (macos-26) on four sites in libobs/obs-cocoa.m.h — a file this PR does not touch — because master is latently red on the enum/int seam conversions that #2877's -Wsign-conversion opt-in exposes (#2896, fixed in flight by #2897). The lane only compiles those TUs when a PR touches libobs, which is why your PR is the one that surfaced it.

Keeping your lease and the requeue: once #2897 lands I will rebase this onto the new master and put it back in the queue in the same tick. Nothing needed from you.

@zackees
zackees force-pushed the fix/bug-enum-audio-devices-doesn-t-c-243 branch from 342cbfe to 5dbb0dd Compare October 29, 2026 11:42
@zackees
zackees enabled auto-merge October 29, 2026 11:43
enum_audio_devices() allocates the CoreAudio device list with malloc()
and reads the list into it without checking the result. A failed
allocation hands NULL to AudioObjectGetPropertyData() as the output
buffer, and if that call succeeds the loop reads the ids from NULL.

test_coreaudio_enum_alloc compiles coreaudio-enum-devices.c over
stand-ins for CoreAudio, CoreFoundation and the libobs calls it makes,
found first as build/libobs-macos.pch.h, so it needs no macOS SDK. The
fake HAL counts a read of the device list into NULL and refuses it, so
the bug fails the test instead of crashing it. With malloc failing,
obs_enum_audio_monitoring_devices(), devices_match() and
get_desktop_default_id() must not read the list at all; the other cases
pin the normal walk.

Refs #2862.
enum_audio_devices() passed the result of malloc() straight to
AudioObjectGetPropertyData() as the output buffer, so a failed
allocation asked CoreAudio to write the device ids into NULL, and on
success the loop read them from NULL.

Return when the allocation fails, before the property read: the caller
sees an empty device list, as it does when the size query fails. The
calls keep their order and no log message changes.

Refs #2862.
@zackees
zackees force-pushed the fix/bug-enum-audio-devices-doesn-t-c-243 branch from 5dbb0dd to a906b02 Compare October 29, 2026 12:23
@zackees
zackees added this pull request to the merge queue Oct 29, 2026
Merged via the queue into master with commit 32c4bcc Oct 29, 2026
38 checks passed
@zackees
zackees deleted the fix/bug-enum-audio-devices-doesn-t-c-243 branch October 29, 2026 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug: libobs: enum_audio_devices doesn't check its allocation

2 participants