Skip to content

bug: libobs/util: os_dtostr corrupts exponents (1e17 -> "1e177", 1.5e-5 -> "1.5e-55"), so config_set_double does not round-trip; the Rust port copies it #754

Closed

Description

@chriscoveries

Summary

os_dtostr (used by config_set_double) mangles any value that %.17g prints in exponent form with a positive exponent or a single-digit negative exponent. The exponent cleanup shifts the digits left with memmove but does not move the terminating NUL, so the last digit is left behind twice: 1e17 is stored as 1e177, 1.5e-5 as 1.5e-55, and 1e300 as 1.0000000000000001e3000, which reads back as inf. config_get_double then returns a value many orders of magnitude off. This is in the C reference itself. The Rust port (ENABLE_RUST_LIBOBS=ON) reproduces it byte for byte on purpose, and the parity test locks it in, so both sides have to change together.

Severity: low in-tree, since no current frontend setting stores such a value. The function is exported and config_set_double is reachable from plugins and from Lua/Python scripts, so any caller storing very small or very large doubles gets silently wrong values back.

Steps / trigger

  1. config_set_double(config, "S", "k", 1e17);
  2. config_get_string(config, "S", "k") returns "1e177", and config_get_double returns 1e177.

The same happens through os_dtostr directly. The table below is the output of os_dtostr(value, buf, 64) followed by os_strtod(buf), with the function and its from_locale/to_locale helpers copied verbatim from libobs/util/platform.c into a standalone program (Apple clang, macOS libc). The Rust Config::set_double/get_string/get_double output was identical on every row, and so was the C oracle in rust/obs-c-oracle.

value %.17g os_dtostr (C and Rust) read back round-trips
1.5e-5 1.5e-05 1.5e-55 1.5e-55 no
1e-5 1.0000000000000001e-05 1.0000000000000001e-55 1.0000000000000002e-55 no
1e17 1e+17 1e177 1e177 no
1.5e17 1.5e+17 1.5e177 1.4999999999999999e177 no
1e-300 1e-300 1e-300 1e-300 yes
1e300 1.0000000000000001e+300 1.0000000000000001e3000 inf no
0.1 0.10000000000000001 0.10000000000000001 0.1 yes
100 100 100.0 100 yes
1e16 10000000000000000 10000000000000000.0 1e16 yes
123456789012345678 1.2345678901234568e+17 1.2345678901234568e177 1.2345678901234569e177 no

Affected ranges with a two-digit printf exponent: every |x| >= 1e17 (all positive exponents), and 1e-9 <= |x| < 1e-4 (exponents e-05 to e-09, which have a leading zero). Negative exponents of e-10 and below have no leading zero, so the cleanup does nothing and they are stored correctly. Fixed-notation values (1e-4 <= |x| < 1e17) are unaffected. The return value is also wrong for affected inputs: it is the intended length, one less than the string actually in the buffer (6 for "1.5e-55").

Cause

Line numbers are against current master (a48a9a1).

  • C reference, libobs/util/platform.c:602-619, in os_dtostr (:573): the block that removes the + and leading zeros from the exponent ends with
    memmove(start, end, length - (size_t)(end - dst)); at :616.
    length - (end - dst) is the number of characters from end up to, but not including, the NUL. The tail is shifted left by end - start bytes, but the old NUL stays where it was, so the last end - start bytes of the old string stay visible. For + exponents end - start is always at least 1 (the +); for - exponents it is the number of leading zeros.
  • libobs/util/config-file.c:582-587: config_set_double stores that buffer as-is. config_get_double (:692-699) parses it with os_strtod.
  • Rust port, rust/obs-util/src/config_file.rs:719-751 (dtostr): simulates the C deliberately. s.copy_within(end..end + n, start) at :748 copies without shortening the Vec, which keeps the leftover tail bytes. It is called from Config::set_double at :1012-1014, which backs the config_set_double C ABI export (rust/obs-util/src/ffi/config_file.rs:351). The doc comment at :719-724 describes the + case but says - exponents "lose leading zeros cleanly (1e-05 -> 1e-5)". They don't: the port produces 1e-55, matching C.
  • rust/obs-util/tests/config_file_parity.rs:731-763 (dtostr_values) compares Rust to the C oracle for 1e18, 1e100, 3.9e151, 1.5e-5 and 9.999e-5. Both sides are wrong in the same way, so the test passes and pins the bug.
  • test/cmocka/test_platform.c:688-708 (test_dtostr) only covers 1.5, 0.0, -2.25 and small buffers, so no exponent value is tested.

config_set_default_double (config-file.c:618-624) does not use os_dtostr. It formats with "%g" and is not affected by this bug.

Impact

os_dtostr's only in-tree caller is config_set_double. Its in-tree callers store values that never reach exponent form:

  • BasicWindow/SnapDistance (frontend/OBSApp.cpp:783, frontend/settings/OBSBasicSettings.cpp:3211): a spin box with 1 decimal and a 0.5 step, default 5.0.
  • Audio/MeterDecayRate (frontend/settings/OBSBasicSettings.cpp:3791): one of the constants 23.53, 11.76 or 8.57.

So the frontend is not affected today. The exposure is the public API: config_set_double and os_dtostr are EXPORTed and wrapped for scripting (shared/obs-scripting/obslua/obslua.i:109, shared/obs-scripting/obspython/obspython.i:132). A plugin or script that stores a tiny or huge double in a profile or user config (obs_frontend_get_profile_config() and similar) gets a wrong value back after the next load, and huge values turn into inf. Submodules (plugins/obs-browser, plugins/obs-websocket) were not checked.

Proposed fix

C: include the terminator in the move, so the string is shortened along with length:

memmove(start, end, length - (size_t)(end - dst) + 1);

With that one change the same standalone program prints 1.5e-5, 1e17, 1.5e17, 1.0000000000000001e300, 1.2345678901234568e17, and every row in the table round-trips exactly through os_strtod. The returned length then also matches strlen(dst).

Rust, same change for parity: in dtostr, drop the removed bytes instead of copying over them (for example s.drain(start..end); in place of the copy_within), and fix the doc comment at config_file.rs:719-724. The oracle copy (rust/obs-c-oracle/oracle/config_file_host.c:121) takes the same one-line C change, so dtostr_values keeps comparing like with like.

Tests: extend test_dtostr in test/cmocka/test_platform.c with the exponent rows above (exact strings, returned length equal to strlen, and os_strtod(os_dtostr(x)) == x), and add a matching round-trip assertion to the Rust side (config_file.rs unit tests or dtostr_values) so a future copy of the old behaviour fails on both sides.

Values already written in the corrupted form stay wrong in existing config files; the fix only stops new ones being written.

Why no PR yet

The fix is one line in C plus the matching Rust change, and we can send it. We haven't yet because our CI is in the repair shop (see our note on #699), so it would go up untested by us. Severity is low: nothing in the frontend stores a value this small or large today.

Activity

  1. zackees commented on Oct 15, 2026

    @zackees
    Member

    Claiming: #754 (os_dtostr exponent cleanup leaves the old tail behind).

    1. RED: Rust round-trip test for the exponent rows in the issue (C oracle and Rust both), plus a new cmocka case for os_dtostr exponents.
    2. GREEN: move the NUL in libobs/util/platform.c and the oracle copy; drain instead of copy_within in rust/obs-util/src/config_file.rs, fix its doc comment.

    First PR (draft, RED) within a few hours. Thanks @chriscoveries for the precise write-up.

  2. added
    claimed
    on Oct 15, 2026
  3. added 3 commits that reference this issue on Oct 15, 2026
    131e443
    94eff6e
    29a2411
  4. zackees commented on Oct 15, 2026

    @zackees
    Member

    Fixed by #796: os_dtostr now moves the NUL with the exponent cleanup (C, oracle and Rust), covered by test/cmocka/test_dtostr_exponent.c and dtostr_exponent_round_trips. Left: config values already written in the corrupted form stay wrong; no migration planned.

  5. removed
    claimed
    on Oct 15, 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

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions