Skip to content

bug: libobs/util: dstr_left passes NULL to memcpy when pos == 0 #174

Closed

Description

@zackees

Found by the first CI run of the sanitizer workflow (run, PR #171; meta issue linked as parent). Upstream OBS code, unchanged in this fork.

Report (UBSan, test_formatted_filename)

libobs/util/dstr.c:523:3: runtime error: null pointer passed as argument 1, which is declared to never be null
    #0 dstr_left                         libobs/util/dstr.c:523
    #1 replace_text                      libobs/util/platform.c:661
    #2 os_generate_formatted_filename    libobs/util/platform.c:717
    #3 zero_width_space_report_test      test/cmocka/test_formatted_filename.c:75

Class

memcpy(NULL, src, 0): UB (C11 7.24.1p2), which the optimizer may use to drop later NULL checks.

Cause

dstr_left calls dstr_resize(dst, pos) and then memcpy(dst->array, str->array, pos). With pos == 0, dstr_resize leaves dst->array NULL. replace_text calls it with pos == 0 whenever a format token (e.g. %CCYY) is at the start of the recording/replay filename format, which is the default format.

Fix

Return early when pos == 0 (after dstr_free(dst) or dstr_resize), or guard the memcpy with if (pos). Check dstr_mid/dstr_right for the same pattern.

Failing test sketch

test_formatted_filename already hits it but keeps passing, because UBSan recovers by default for this check. A Tier 1/2 test calling dstr_left(&d, &s, 0) on an empty dstr under -fno-sanitize-recover=nonnull-attribute goes RED.

Activity

  1. added
    bug
    on Oct 11, 2026
  2. zackees commented on Oct 11, 2026

    @zackees
    MemberAuthor

    Claiming. Plan: RED cmocka test that compiles libobs/util/dstr.c with -fsanitize=undefined -fno-sanitize-recover=undefined and calls dstr_left/dstr_mid/dstr_right with zero lengths, then GREEN guards.

  3. added
    claimed
    on Oct 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugclaimed

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions