Repository navigation
libobs/util: Fix dstr_insert_ch overrun and dstr_replace hang - #53
Merged
Merged
Conversation
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]>
This was referenced Oct 9, 2026
This was referenced Oct 9, 2026
3 of 7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_chwrote past the bufferAfter
++dst->len, the memmove movedlen - idx + 1bytes, one byte past the old NUL, so it wrotearray[new_len + 1]. When capacity is exactlynew_len + 1, that byte is outside the buffer. The fix moveslen - idxbytes: the old tail and its NUL. The string result is unchanged.Why a canary test instead of ASan: on Linux,
bmem.c'sALIGNMENT_HACKallocatessize + 32and shifts the pointer by 16 or 32 bytes. A one-byte overrun of abmallocbuffer therefore often lands in that padding, where ASan doesn't see it. The new test uses astruct dstrover 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_replacenever terminated on an emptyfindstrstr(temp, "")always returnstemp, so the loops never advanced. Chosen semantics: no-op for an emptyfind, and also for a NULLfind, which used to crash instrlen. This matches the Rust dstr port (#51). The test has a 60 s ctestTIMEOUT, 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 testtest_dstr.cis not edited. Itsinsert_chcase reserves extra capacity, so it passes before and after.Verification
dstr.cwith MSVC into a scratch harness running the same scenarios:dstr_replace(&d, "", "x")hangs (3 s watchdog);findcalls leave"abc"unchanged../lintpasses.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