Skip to content

config: Append .0 to non-finite config_set_double values - #739

Merged
zackees merged 3 commits into
obs-rust:masterfrom
chriscoveries:fix/config-double-nonfinite
Oct 15, 2026
Merged

zackees merged 3 commits into
obs-rust:masterfrom
chriscoveries:fix/config-double-nonfinite

Conversation

@chriscoveries

Copy link
Copy Markdown
Contributor

Draft. Opened so upstream PR CI can run it. Our own CI is down, so this hasn't been through our usual CI runs: see our note on #699.

Problem

With ENABLE_RUST_LIBOBS=ON, config_set_double stores inf, -inf and nan where C stores inf.0, -inf.0 and nan.0. The difference shows through config_get_string and in saved ini files. On macOS, ON also stored -nan for a NaN with the sign bit set (-NAN), where Apple's libc prints nan, and config_set_default_double(-NAN) stored -nan where C stores nan. Reading the value back with config_get_double gives the same number either way, so the impact is the stored text only. Severity: Low. With the Rust port OFF, nothing changes.

Fixes: TBD

Root cause

On master (3410e7b2b), rust/obs-util/src/config_file.rs:

  • dtostr (:722-725) returned the nan_inf spelling early, before the .0 rule. C's os_dtostr (libobs/util/platform.c:579) formats with snprintf("%.17g") and then appends .0 whenever the text has neither . nor e (platform.c:589-600). inf and nan contain neither, so C stores inf.0 and nan.0.
  • nan_inf (:668-684) is a fixed table: -nan for any sign-bit NaN, else nan, plus inf/-inf. That is glibc's spelling. It is wrong on Apple libc, which prints nan for every NaN, and on the Windows UCRT, which prints -nan(ind) and nan(snan). The same table also feeds config_set_default_double (:1067), which C formats with dstr_printf("%g") (libobs/util/config-file.c:622).

Fix

At GREEN e76db9d223a79e6f6443244ff14b56c3d0b6f7fb:

  • dtostr (rust/obs-util/src/config_file.rs:711-712) no longer returns early, so non-finite values go through the same .0 rule and exponent code as finite ones. The exponent code itself is unchanged.
  • g_format (config_file.rs:676-678) formats non-finite values with the C library's own snprintf, using the same format string as C: "%.17g" for config_set_double and "%g" for config_set_default_double (:1053). The C runtime chooses the spelling, so ON matches C on every libc by construction instead of through a per-platform table. Finite values keep the existing Rust formatting.
  • The snprintf call is in the platform layer: rust/obs-util/src/platform/stdio.rs:38-54, re-exported from platform/mod.rs:53-54. The libc crate declares snprintf on every supported target; in the CMake build the symbol resolves to the snprintf that libobs's own C objects already emit (on Windows, the UCRT inline snprintf), so the file has no per-OS code. legacy_stdio_definitions, which the libc crate attaches on MSVC, applies only to binaries that cargo links (the cargo test executables), not to the CMake link. A DoubleFormat enum (stdio.rs:12) limits the call to these two format literals. ffi/ is unchanged and there are no new exported symbols.
  • The doc comment on dtostr is corrected. It said - exponents lose leading zeros cleanly (1e-05 -> 1e-5). In fact C's memmove in os_dtostr misses the NUL for - exponents too (1.5e-05 is stored as 1.5e-55), and Rust mirrors that on purpose. That C bug is tracked separately and isn't changed here.

Alternatives considered:

  • Keep a Rust spelling table and key the NaN sign on the platform (target_vendor = "apple"). Rejected: it would also need FreeBSD's libc, which is from the same family, plus UCRT's -nan(ind)/nan(snan), and the table would need to be kept in step with each libc by hand.
  • Guard the new test off Windows and keep the table. Rejected: Windows ON would keep storing a different string from C for -NAN and for signalling NaNs.

Evidence

Branch fix/config-double-nonfinite on the fork, two commits on 3410e7b2b:

  • RED, tests only: d6cf2e8b32822b9ee9f2d6807550b824688b7ffc "config: Test config_set_double non-finite spelling (RED)"
  • GREEN, the fix: e76db9d223a79e6f6443244ff14b56c3d0b6f7fb "config: Append .0 to non-finite config_set_double values (GREEN)"

The test is test/cmocka/test_config_set_double_format.c, registered in test/cmocka/tests.d/test_config_set_double_format.cmake on every platform. Byte-exact comparisons are against C's os_dtostr and snprintf("%g"), which stay C in both builds:

  • test_set_double_nonfinite_spelling (:45): inf.0, -inf.0, nan.0 as literals and against os_dtostr.
  • test_set_double_nan_variants (:54): -NAN, NaNs with payloads, and a signalling NaN, compared to C only, because the spelling belongs to the libc.
  • test_set_default_double_nonfinite (:85): config_set_default_double against snprintf("%g") for the same non-finite values, through both config_get_default_string and config_get_string.
  • test_set_double_nonfinite_round_trip (:98) and test_set_double_finite_unchanged (:113): guards for the read-back and for finite values (0.1, -0.0, 1e15-1e18, 1.5e-5, DBL_MAX, DBL_MIN, the smallest subnormal and others).

Local runs: macOS arm64, CMake Xcode generator, Debug, the repo's own flags.

RED with ENABLE_RUST_LIBOBS=OFF passes 5/5. RED with ON (the master Rust) fails 3 of 5:

[ RUN      ] test_set_double_nonfinite_spelling
[  ERROR   ] --- "inf" != "inf.0"
[  FAILED  ] test_set_double_nonfinite_spelling
[ RUN      ] test_set_double_nan_variants
[  ERROR   ] --- "-nan" != "nan.0"
[  FAILED  ] test_set_double_nan_variants
[ RUN      ] test_set_default_double_nonfinite
[  ERROR   ] --- "-nan" != "nan"
[  FAILED  ] test_set_default_double_nonfinite
[  PASSED  ] 2 test(s).

GREEN passes 5/5 with OFF and 5/5 with ON. The full cmocka suite passes 88/88 with OFF and 88/88 with ON (test_svt_av1 skipped). soldr cargo test -p obs-util --locked passes 472 tests on macOS, and clippy -D warnings, fmt --check, the repo Dylints and clang-format 22.1.3 are clean.

Linux (x86_64, Debian 13, glibc 2.41, cmocka 1.1.7): the committed test, compiled against the obs-util staticlib and the C os_dtostr from platform.c, fails 2 of 5 on RED ("inf" != "inf.0", "-nan" != "-nan.0") and passes 5/5 on GREEN. This is a harness build, not the full CMake build; the CMake Linux run was not done and relies on upstream PR CI. It compiles with cmocka 1.1.7 without extra flags. soldr cargo test -p obs-util passes 475 tests there, and the obs-util tests build and link for x86_64-pc-windows-gnu (--no-run) with clippy -D warnings clean.

Mutants, built and run with ON on macOS:

Mutant Result Killed by
glibc table: -nan for any sign-bit NaN, both formats killed -NAN in test_set_double_nan_variants and test_set_default_double_nonfinite
-nan kept on the %g path only killed -NAN in test_set_default_double_nonfinite ("-nan" != "nan")
snprintf applied to finite values too survived (equivalent in the C locale) —

The surviving mutant removes the !v.is_finite() guard so that every value goes through libc snprintf. It passes the test and the 14-pattern public-ABI probe because it is equivalent in the C locale. Only a comma-decimal LC_NUMERIC tells it apart: snprintf would write 1,5, which C's os_dtostr repairs with from_locale. The guard is correct, but nothing pins it, and a locale-dependent cmocka test was not added because it would be fragile in CI. Swapping the G17 and G formats on the non-finite path is equivalent by construction (%g and %.17g print inf and nan identically) and was not built.

The opposite table mutant (NaN sign always dropped) is killed on glibc by -NAN ("nan.0" != "-nan.0"), which an independent review showed on the previous head.

Real runs: the full app was not run for this change. A public-ABI probe (config_set_double, config_set_default_double, os_dtostr, snprintf("%g")) shows what each libc stores at GREEN. ON and OFF match on both:

Value (bits) macOS (Apple libc) set_double / default Linux (glibc) set_double / default
inf (7ff0000000000000) inf.0 / inf inf.0 / inf
-inf (fff0000000000000) -inf.0 / -inf -inf.0 / -inf
NAN (7ff8000000000000) nan.0 / nan nan.0 / nan
-NAN (fff8000000000000) nan.0 / nan -nan.0 / -nan
payload (fff8000000001234) nan.0 / nan -nan.0 / -nan
signalling (7ff0000000000001) nan.0 / nan nan.0 / nan

Windows (UCRT) was not run; the Windows link has never been built or tested. From Microsoft's documented printf behaviour, C stores -nan(ind).0 for -NAN and nan(snan).0 for a signalling NaN; with this change Rust calls the same CRT snprintf and should store the same.

Fork CI: not run. Our self-hosted CI is down (see our note on #699), so this relies on upstream PR CI.

Compatibility & risk

  • C API/ABI: no changes and no new exports. With the Rust port ON, config_set_double and config_set_default_double now store the same text as with it OFF for non-finite values. Finite values are unchanged.
  • Per platform: macOS and glibc were run (table above). The Windows link has never been built or tested: no Windows ON build, link or ctest run exists for this change. The expectation for MSVC is that Rust's reference to snprintf binds, in the CMake build, to the UCRT inline snprintf that libobs's own C objects already emit (for example os_dtostr in platform.c), so -nan(ind) and nan(snan) should match C. That is a prediction from how the pieces link, not a result. The legacy_stdio_definitions library that the libc crate attaches on MSVC applies only to binaries that cargo links, such as the cargo test executables, not to the CMake link. Upstream PR CI's Windows job will be the first place this is built. Older msvcrt-based MinGW is unverified: there C's snprintf can be mingw-w64's own C99 implementation while Rust binds msvcrt's. OBS ships MSVC builds on Windows, and the x86_64-pc-windows-gnu lane only links. FreeBSD and musl were not run; both now get their own libc's spelling by construction.
  • Test registration: the test runs on every platform, Windows included, because the comparisons are against C's own output there too.
  • Not changed here: C's os_dtostr exponent trim keeps a stray digit (1.5e-5 is stored as 1.5e-55, 1e17 as 1e177), and Rust mirrors it for parity. That is a C bug with its own issue.

How this was tested

  • RED → GREEN: test_config_set_double_format was built locally on macOS arm64 with the CMake Xcode generator (Debug, repo flags, no sanitizers). With ENABLE_RUST_LIBOBS=OFF it passes on both RED and GREEN. With ON it fails on RED with the output above and passes on GREEN. On Linux glibc, a harness build of the same test against the obs-util staticlib fails on RED and passes on GREEN. Both table mutants above are killed; the finite-values snprintf mutant survives (see Evidence).
  • Our own CI: not run. Our self-hosted CI is down (see our note on #699), so this relies on upstream PR CI.
  • Real runs: not applicable. The change only affects the stored text of non-finite config values, so the full app wasn't run. The public-ABI probe above was run instead.

Status

Status: draft. Opened so upstream PR CI can run it. Branch fix/config-double-nonfinite at e76db9d223a79e6f6443244ff14b56c3d0b6f7fb.


Thank you to the obs-rust maintainers and contributors for building this project in the open. In the American spirit of free expression and individual independence, and with the principle of judging contributions on their merit alone, this PR is a small piece of what independent people can build when they come together.

@zackees

zackees commented Oct 14, 2026

Copy link
Copy Markdown
Member

Tier 2 Windows (ENABLE_RUST_LIBOBS=ON) fails at link; it is not flaky.

LINK : warning LNK4217: symbol 'snprintf' defined in 'platform.obj' is imported by 'libobs_rust.lib(obs_util-...obs_util.eb4e56ac2a58ca77-cgu.00.rcgu.o)' in function '...obs_util11config_file8g_format...' [libobs.vcxproj]
LINK : error LNK1218: warning treated as error; no output file generated

Cause: rust/obs-util/src/platform/stdio.rs declares snprintf via the libc crate, so Rust references it as an imported (__imp_snprintf) symbol, but libobs.dll also defines snprintf itself in platform.obj, and the repo links with warnings as errors. Likely fix: do not import snprintf by that name on MSVC. Either call the C runtime through a uniquely named symbol (e.g. _snprintf/__stdio_common_vsprintf, or a small extern "C" shim in libobs with its own name), or format the double in Rust without the CRT. The Linux/OFF builds are not affected. Reproduce on Windows with rust/tools/tier2-validate/validate.ps1.

@zackees
zackees marked this pull request as ready for review October 14, 2026 23:11
@zackees
zackees self-requested a review as a code owner October 14, 2026 23:11
@zackees

zackees commented Oct 14, 2026

Copy link
Copy Markdown
Member

Updated from master and marked ready; everything passes except Rust Tier 2 on Windows, which fails to link libobs with ENABLE_RUST_LIBOBS=ON:

LINK : warning LNK4217: symbol 'snprintf' defined in 'platform.obj' is imported by 'libobs_rust.lib(obs_util-…)' in function '…obs_util11config_file8g_format…'
LINK : error LNK1218: warning treated as error

The new config_file::g_format calls snprintf through an extern "C" import. On MSVC libobs defines its own snprintf (in util/platform.c), so the Rust staticlib's import of it conflicts and the linker treats it as an error. Formatting the value in Rust (or calling a non-variadic helper that libobs exports) instead of importing snprintf should fix it. Linux and macOS are green. Once Windows Tier 2 passes I'll merge it.

chriscoveries and others added 3 commits October 14, 2026 18:48
C config_set_double stores os_dtostr(): %.17g with ".0" appended when
there is no '.' or 'e', which applies to inf, -inf and nan too (inf.0,
-inf.0, nan.0). Rust ON returns the bare spelling. Compare against
os_dtostr and literal spellings, with finite values as a regression guard.

The NaN sign and payload spellings belong to the C library (glibc -nan,
Apple nan, UCRT -nan(ind) and nan(snan)), so the NaN cases compare to C
only. config_set_default_double ("%g", no ".0") is compared to snprintf
for the same values.
os_dtostr() applies its ".0" rule to the %.17g text of inf and nan, so
C stores inf.0, -inf.0 and nan.0. Drop the early non-finite return in the
Rust dtostr so those spellings take the same path as finite values.

The spelling of a non-finite value is the C library's choice: glibc
prints a sign-bit NaN as "-nan", Apple libc as "nan", and UCRT prints
"-nan(ind)" and "nan(snan)". Format inf and nan with the C library's own
snprintf and the same format string as C ("%.17g" for config_set_double,
"%g" for config_set_default_double) instead of a fixed table, so both
match C on every libc.
libc::snprintf is a DLL import; on MSVC libobs's platform.obj already
defines the inline snprintf, so the Tier 2 Windows link failed with
LNK4217 (warning treated as error).
@zackees

zackees commented Oct 15, 2026

Copy link
Copy Markdown
Member

Maintainer rebase, as allowed by coordination.md §4. I rebased onto master (dropping the master merge commit), replacing head 8023db788. I also added a fix for the Windows Tier 2 failure: LNK4217: symbol 'snprintf' defined in 'platform.obj' is imported by libobs_rust.lib. On MSVC, snprintf is an inline function that libobs's own objects already define, but libc::snprintf is declared as a DLL import. platform/stdio.rs now declares snprintf directly instead. Locally, ./lint and the cross lane pass, and Tier 2 Linux is 227/227 OFF and ON with symbols IDENTICAL. I can't link MSVC locally, so this CI run is the check.

@zackees
zackees force-pushed the fix/config-double-nonfinite branch from 8023db7 to a11a766 Compare October 15, 2026 01:52
@zackees
zackees added this pull request to the merge queue Oct 15, 2026
Merged via the queue into obs-rust:master with commit 03acc12 Oct 15, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants