Repository navigation
libobs/util: Skip the copy in dstr_left when pos is 0 - #222
Merged
Merged
Conversation
dstr_left() resizes the destination to pos and then memcpys pos bytes into it. With pos == 0 the destination array is NULL, and memcpy(NULL, src, 0) is undefined behaviour. os_generate_formatted_filename() hits it whenever the format starts with a token (#174). test_dstr_zero_len compiles dstr.c with UBSan aborting on the first report: dstr.c:523:3: runtime error: null pointer passed as argument 1 Co-Authored-By: Claude Opus 5.5 <[email protected]>
dstr_left() now only memcpys when there is something to copy, so it never passes the NULL array of an empty dstr to memcpy. test_dstr_zero_len passes under UBSan. Fixes #174 Co-Authored-By: Claude Opus 5.5 <[email protected]> Local-Gate: v1 tree=d9265b58d85924f6af1ed9ca903e488a47dc8a0a secs=155 lanes=lint:run,rust-test:run,tier2-linux:run Ci-Attestation: {"at":1791710344,"gate":"rust/all/fmt","host":"linux-x86_64","key":"0df2bf9fd1bcc0ef660bcf0a6dc3556735595ddf72c20223cf6d9dee8f539118","lane":"lint","parents":["bfc8c1dc5d93c69cb8316d73c272b998bda7bfcc"],"secs":62,"stamp":"ab00892bc3b3cbdf0a6b670cb9577623","tree":"d9265b58d85924f6af1ed9ca903e488a47dc8a0a","v":1,"via":"run"} Ci-Attestation: {"at":1791710344,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"0df2bf9fd1bcc0ef660bcf0a6dc3556735595ddf72c20223cf6d9dee8f539118","lane":"lint","parents":["bfc8c1dc5d93c69cb8316d73c272b998bda7bfcc"],"secs":62,"stamp":"5ed7bc8aa223d36d827b3f029e7ff4f2","tree":"d9265b58d85924f6af1ed9ca903e488a47dc8a0a","v":1,"via":"run"} Ci-Attestation: {"at":1791710344,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"df0baa52b476dd471641ecdd0bf0c33cb12e3844a8e028590f2a47525091ed1c","lane":"rust-test","parents":["bfc8c1dc5d93c69cb8316d73c272b998bda7bfcc"],"secs":33,"stamp":"09861dc608962f9765648120817c3dc5","tree":"d9265b58d85924f6af1ed9ca903e488a47dc8a0a","v":1,"via":"run"} Ci-Attestation: {"at":1791710344,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"76d009956b90b8068571e386a3104292cdebbdd005cf62a37a0399967457b98b","lane":"tier2-linux","parents":["bfc8c1dc5d93c69cb8316d73c272b998bda7bfcc"],"secs":60,"stamp":"fb7214a9d5b44eed6d4085b558c2c25d","tree":"d9265b58d85924f6af1ed9ca903e488a47dc8a0a","v":1,"via":"run"}
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 #174.
dstr_left()resized the destination toposand then memcpy'dposbytes into it. Withpos == 0the array is NULL, andmemcpy(NULL, src, 0)is undefined behaviour (C11 7.24.1p2).os_generate_formatted_filename()hits it on every recording whose filename format starts with a token, which includes the default format.test: Fail dstr_left with pos 0 under UBSan:test_dstr_zero_lencompileslibobs/util/dstr.cwith-fsanitize=undefined -fno-sanitize-recover=undefined(non-MSVC, same as obs-outputs: Decode 32-bit RTMP/AMF fields without signed shifts #188). Tier 2 passed 71 of 72, withdstr.c:523:3: runtime error: null pointer passed as argument 1, which is declared to never be null, the exact report in bug: libobs/util: dstr_left passes NULL to memcpy when pos == 0 #174.posis non-zero. Tier 2 passed 72 of 72 withENABLE_RUST_LIBOBSOFF and 72 of 72 with it ON. The test also coversdstr_righton an empty dstr, which was already fine.With
ENABLE_RUST_LIBOBS=ON, libobs uses the Rustobs-utildstr, which has no raw memcpy. The test still checks the C file directly. Local gate: lint, rust-test and tier2-linux all passed.