Skip to content

libobs/util: Fix dstr_insert_ch overrun and dstr_replace hang - #53

Merged
zackees merged 4 commits into
obs-rust:masterfrom
b7216309-jpg:fix/dstr-insert-ch-replace
Oct 9, 2026
Merged

zackees merged 4 commits into
obs-rust:masterfrom
b7216309-jpg:fix/dstr-insert-ch-replace

Conversation

@b7216309-jpg

Copy link
Copy Markdown
Contributor

Fixes #46
Fixes #47
Refs #43

Two small C fixes in libobs/util/dstr.c, each with a RED commit first. No Rust.

#46: dstr_insert_ch wrote past the buffer

After ++dst->len, the memmove moved len - idx + 1 bytes, one byte past the old NUL, so it wrote array[new_len + 1]. When capacity is exactly new_len + 1, that byte is outside the buffer. The fix moves len - idx bytes: the old tail and its NUL. The string result is unchanged.

Why a canary test instead of ASan: on Linux, bmem.c's ALIGNMENT_HACK allocates size + 32 and shifts the pointer by 16 or 32 bytes. A one-byte overrun of a bmalloc buffer therefore often lands in that padding, where ASan doesn't see it. The new test uses a struct dstr over a stack buffer with exactly the needed capacity and a canary byte right past it. It fails deterministically on every platform, MSVC included, with no sanitizer.

#47: dstr_replace never terminated on an empty find

strstr(temp, "") always returns temp, so the loops never advanced. Chosen semantics: no-op for an empty find, and also for a NULL find, which used to crash in strlen. This matches the Rust dstr port (#51). The test has a 60 s ctest TIMEOUT, so a regression fails fast instead of hanging CI.

Tests

These are new tests in a new test/cmocka/test_dstr_regressions.c. The characterization test test_dstr.c is not edited. Its insert_ch case reserves extra capacity, so it passes before and after.

Verification

  • The cmocka suite was not run locally (no Docker/cmocka here).
  • I compiled the real dstr.c with MSVC into a scratch harness running the same scenarios:
    • before the fixes: the canary is overwritten, and dstr_replace(&d, "", "x") hangs (3 s watchdog);
    • after: the canary is intact, and all four empty/NULL find calls leave "abc" unchanged.
  • ./lint passes.

Effect on the Rust ports

With these fixes, #51 (dstr) no longer differs from C on either point, so those two entries drop out of its "intentional differences". I'll update #51 once this merges.

🤖 Generated with Claude Code

b7216309-jpg and others added 4 commits October 9, 2026 14:18
Adds test_dstr_regressions with a canary test for obs-rust#46: a dstr on a
stack buffer whose capacity is exactly new_len + 1, with a canary byte
right past it. The current dstr_insert_ch moves one byte too many and
overwrites the canary, so the test fails on every platform without a
sanitizer (libobs's ALIGNMENT_HACK allocator pads blocks, so ASan would
not reliably see a one-byte overrun in a bmalloc buffer).

Refs obs-rust#46

Co-Authored-By: Claude Opus 5.5 <[email protected]>
dstr_insert_ch increments len before the memmove, so moving
len - idx + 1 bytes moved one byte past the old NUL and wrote
array[new_len + 1], which is past the buffer when capacity is exactly
new_len + 1. Move len - idx bytes: the old tail and its NUL.

The string result is unchanged; only the stray write is gone. GREEN for
test_dstr_regressions.

Fixes obs-rust#46

Co-Authored-By: Claude Opus 5.5 <[email protected]>
dstr_replace with an empty find string loops forever today:
strstr(temp, "") always returns temp. With replace_len 0 temp never
advances; with a longer replacement the counting loop never ends. The
test runs into test_dstr_regressions' 60 s ctest timeout until the fix.

The chosen semantics are a no-op for an empty (and a NULL) find, the
same as the Rust dstr port.

Refs obs-rust#47

Co-Authored-By: Claude Opus 5.5 <[email protected]>
strstr(temp, "") always returns temp, so an empty find string never
advanced and dstr_replace looped forever (and a NULL find crashed in
strlen). Return early for an empty or NULL find, which leaves the dstr
unchanged; the Rust dstr port does the same.

GREEN for test_dstr_regressions.

Fixes obs-rust#47

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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: dstr_replace never terminates when find is the empty string bug: dstr_insert_ch writes one byte past the buffer when inserting mid-string

2 participants