Skip to content

bug: util: os_nstime_to_timespec can return tv_nsec == 1000000000 #1505

Closed

Description

@rdoupe

Found by the #262 slice 6 characterization test (test_platform_time, in the #262 slice 6 move PR). Nobody has reported it upstream; the code is the same in upstream OBS master (libobs/util/platform.c), added in 2024 with Broadcast Performance Metrics. The port keeps the C behavior, so this is a separate fix.

Location

libobs/util/platform-time.c.h (was libobs/util/platform.c.h), os_nstime_to_timespec(). For a timestamp at or above the base taken on the first call, the nanoseconds are the base's tv_nsec plus offset % 1000000000. That sum can be anything from 0 to 1999999998. The carry is

if (nsecs > nsecs_per_sec) {

so a sum of exactly 1000000000 is not carried: the result is tv_nsec == 1000000000 with tv_sec one second short. That is not a valid struct timespec (tv_nsec must be below 1000000000). Below the base the result is always normalized.

Class

Wrong result (an invalid timespec). No crash or memory error. The input is a local clock timestamp, so nothing outside OBS can trigger it.

How it is reached

Every timestamp whose wall time is a whole second, unless the base's own wall time was a whole second. The only caller in the tree is shared/bpm (Broadcast Performance Metrics SEI on multitrack video). render_metrics_time() prints tv_sec with %T and tv_nsec / 1000000 with .%03ld, so a frame at …:33.000 is sent as …:32.1000Z, which reads as 32.1 s: 900 ms early. A plugin that passes the result to pthread_cond_timedwait or nanosleep gets EINVAL. Per timestamp it is a 1 in 10^9 chance, so it is rare in practice, but test_platform_time hits it every time.

Reproduced in the current release

Headless (private Xvfb, private D-Bus, fresh HOME). test_platform_time and a small program that calls os_nstime_to_timespec for the timestamp that lands on a whole second, then 1 ns later, and formats both the way BPM does.

Build Case Result
v0.1.0-beta.20261018 AppImage, libobs.so.30 whole-second timestamp tv_nsec=1000000000, BPM string …T05:36:32.1000Z
v0.1.0-beta.20261018 AppImage, libobs.so.30 1 ns later tv_nsec=1, BPM string …T05:36:33.000Z
v0.1.0-beta.20261018 AppImage, libobs.so.30 and obs-studio-rust/libobs.so.30 test_platform_time (test_whole_second_above_the_base pins the bug) 9/9 pass, so the bug is present in both
master 13e73b1 plus the move (the #262 slice 6 move PR) whole-second timestamp tv_nsec=1000000000, BPM string …T05:36:37.1000Z

Fix

Carry with >= (if (nsecs >= nsecs_per_sec)) in the C file and in the Rust port (obs-util::platform_time), and change test_whole_second_above_the_base (and its Tier 1 port) to expect the normalized result, {sec, 0}. That is a deliberate change to a characterization test, so the PR says so. The Tier 3 parity test then needs no exclusion, since the C and the Rust change together. This goes after the slice 6 port PR lands.

Activity

  1. rdoupe commented on Oct 20, 2026

    @rdoupe
    CollaboratorAuthor

    Claiming: #1505

    1. One PR: carry with >= in util/platform-time.c.h and in obs-util::platform_time together, and change test_whole_second_above_the_base (and its Tier 1 mirror) to expect the normalized {sec, 0}. That is a deliberate change to a characterization test, called out in the PR. Tier 3 parity needs no exclusion, since C and Rust change together.

    PR expected today.

  2. added
    claimed
    on Oct 20, 2026
  3. zackees commented on Oct 20, 2026

    @zackees
    Member

    Coordinator note on the plan: step 1 changes the expectation of an existing C test (test_whole_second_above_the_base). AGENTS.md says never to edit existing C tests to make a change pass, so this needs the maintainer's explicit sign-off as a deliberate behaviour change before the PR can merge. Suggest stating that in the PR description and pinging @zackees there, so it doesn't stall in review.

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

    claimed

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions