Skip to content

config: Limit double getters to C's 63-byte conversion window - #738

Merged
zackees merged 2 commits into
obs-rust:masterfrom
chriscoveries:fix/config-double-window
Oct 14, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
chriscoveries:fix/config-double-window

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_get_double and config_get_default_double read past the 63-byte window that C's os_strtod converts. A stored value whose number starts or continues past byte 63 converts differently: 63 spaces then 1 (64 bytes) gives 1.0 in Rust and 0.0 in C, and 0. + 61 zeros + 1 gives 1e-62 in Rust and 0.0 in C. It only affects values longer than 63 bytes, which normal OBS config files don't write, so severity is low. With the Rust port OFF, nothing changes.

Fixes: TBD

Root cause

C's getters (libobs/util/config-file.c:692-699 and :765-772) pass the stored string to os_strtod (libobs/util/platform.c:564-571). That copies at most sizeof(buf) - 1 = 63 bytes into char buf[64], swaps the decimal point for the locale's, and calls strtod on the copy. Leading whitespace and the sign count against those 63 bytes, and anything after byte 63 is never seen.

On master (29d8bcc8b), the Rust getters Config::get_double and Config::get_default_double (rust/obs-util/src/config_file.rs:974-977 and :1095-1098) pass the whole stored string to parse_double, so nothing is cut.

Fix

At GREEN fd24d2d6a, rust/obs-util/src/config_file.rs:

  • const OS_STRTOD_BUF: usize = 64 (:567) names the size of C's buffer, with a comment pointing at os_strtod in libobs/util/platform.c.
  • A private os_strtod (:574-576) cuts the stored bytes to OS_STRTOD_BUF - 1 and then calls the existing parse_double. Both getters call it (:990, :1111).

The cut is made on the whole stored string, before parse_double skips whitespace, reads the sign, or chooses between hex and decimal. That is where C makes it: strncpy copies the raw bytes before strtod looks at any of them. A cut taken later measures the window from the wrong byte. For example, 61 spaces then 0x1 is 64 bytes, so C sees only 61 spaces and 0x and returns 0.0. A cut after the whitespace still sees 0x1 and returns 1.0.

Values of 63 bytes or fewer are unchanged. A missing value still returns 0.0. The cut doesn't allocate, and slicing at min(len, 63) is always in bounds.

Alternatives considered:

  • Cut inside parse_double: rejected. parse_double is the strtod model and has other callers in waiting (see Compatibility). The window belongs to os_strtod, so it sits in a wrapper with that name.
  • Port all of os_strtod (window, locale swap, libc strtod) and drop the Rust decimal parser: rejected for this PR. It changes decimal behaviour in comma locales and needs its own issue (see Compatibility).

Evidence

Branch fix/config-double-window on the fork, two commits on 29d8bcc8b:

  • RED, tests only: 47b26e625937626fac53f9ea88fb8c887c102c26 "config: Test 63-byte double conversion window (RED)"
  • GREEN, the fix: fd24d2d6ada21142405dcfdc646072b20d3beb04 "config: Limit double getters to C's 63-byte conversion window (GREEN)"

The test is test/cmocka/test_config_double_window.c, registered in test/cmocka/tests.d/test_config_double_window.cmake. Each row is stored as a user value and as a default value, read back through config_get_double and config_get_default_double, and compared bit for bit with the value C gives:

Row Value Bytes Expected
spaces_63_bytes 62 spaces + 1 63 1.0
spaces_64_bytes 63 spaces + 1 64 0.0
spaces_65_bytes 64 spaces + 1 65 0.0
fraction_63_bytes 0. + 60 zeros + 1 63 1e-61
fraction_64_bytes 0. + 61 zeros + 1 64 0.0
integer_63_bytes 1 + 62 zeros 63 1e62
integer_64_bytes 1 + 63 zeros 64 1e62
exponent_63_bytes 1. + 59 zeros + e5 63 1e5
exponent_64_bytes 1. + 60 zeros + e5 64 1.0
hex_63_bytes 0x + 60 zeros + 1 63 1.0
hex_64_bytes 0x + 61 zeros + 1 64 0.0
hex_spaces_64_bytes 61 spaces + 0x1 64 0.0

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

RED with ENABLE_RUST_LIBOBS=OFF passes all 12 rows. RED with ON (the master Rust) fails on the first 64-byte row:

[ RUN      ] test_double_conversion_window_boundary
case spaces_63_bytes (63 bytes)
case spaces_64_bytes (64 bytes)
[  ERROR   ] --- 4607182418800017408 != 0
[  FAILED  ] test_double_conversion_window_boundary

(4607182418800017408 is 0x3ff0000000000000, which is 1.0.)

GREEN passes all 12 rows with the Rust port OFF and ON:

case hex_63_bytes (63 bytes)
case hex_64_bytes (64 bytes)
case hex_spaces_64_bytes (64 bytes)
[       OK ] test_double_conversion_window_boundary
[  PASSED  ] 1 test(s).

At GREEN, the whole cmocka suite passed 88/88 with OFF and with ON (test_svt_av1 skipped, as on master). cargo test -p obs-util --locked passed 472 tests with 0 failures. cargo clippy -p obs-util --all-targets -- -D warnings and cargo fmt --check are clean, clang-format 22 is clean on the test, and gersemi is clean on the fragment.

Mutation check: each mutant was applied to GREEN, built with ON and run against the committed test.

Mutant Result First failing row
window of 62 bytes killed spaces_63_bytes
window of 64 bytes killed spaces_64_bytes
no window killed spaces_64_bytes
window on the user getter only killed spaces_64_bytes
window on the default getter only killed spaces_64_bytes
window measured after whitespace and sign killed spaces_64_bytes; with the decimal space rows removed, still killed by hex_64_bytes
window applied after the hex dispatch (decimal path only) killed hex_64_bytes; with that row removed, still killed by hex_spaces_64_bytes. It passes all nine decimal rows.

The hex rows were added for the "after the hex dispatch" mutant. Without them, a later change could move the window behind the hex check and every decimal row would still pass.

Real runs: the full app was not run for this change. On the previous head, whose conversion logic is the same as this one with a literal 63 in place of the constant, an independent review built a probe that calls only config_get_double and config_get_default_double, against the C sources (OFF) and the Rust port (ON). Its 15 window rows (decimal, hex, signed, space-padded, at 63, 64 and 65 bytes) matched C bit for bit in the C and de_DE.UTF-8 locales, through both getters.

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, no new exports, no test hooks. The new function and constant are private. With the Rust port ON, values longer than 63 bytes now convert the same as with it OFF. Shorter values are unchanged.
  • Interaction with fix/config-hex-float-rounding: that branch deletes parse_hex_float and sends hex values to a libc-backed platform::os_strtod. The two branches conflict in one hunk of rust/obs-util/src/config_file.rs, because this branch adds the wrapper just above parse_hex_float. The resolution is to keep this branch's OS_STRTOD_BUF and window wrapper and drop parse_hex_float. An independent review tested that resolved tree: cargo test -p obs-util, clippy and fmt were clean, both cmocka tests passed, and every row of the window and hex probe matched C in both locales. The two changes compose correctly only because the window is cut in the getter, before the hex dispatch. The hex rows in this test check that ordering.
  • Merge order: this PR lands first. It is self-contained and smaller. fix/config-hex-float-rounding then merges master with a merge commit, resolves the one hunk by keeping this window wrapper, and is re-reviewed on that head. At that point it should also rename one of the two os_strtod functions (for example config_file::os_strtod_window) and change its platform::os_strtod doc comment to say that callers apply the 63-byte window. If the hex-float branch lands first instead, this branch does the same resolution when it merges master, and the resulting tree is the same.
  • Remaining differences (not changed here, for a separate issue):
    • Under a comma-decimal locale such as de_DE, C converts 1,5 to 1.5, because os_strtod calls the locale-aware strtod. The Rust decimal path gives 1.0.
    • NaN payloads: C keeps them (nan(0x1234) gives 0x7ff8000000001234, and nan(1234) gives 0x7ff80000000004d2). Rust returns the default NaN 0x7ff8000000000000.
    • Both are outside the 63-byte window and change decimal behaviour, so they need their own issue with options.
  • Platforms: the test is registered on every platform, because C's os_strtod and the Rust code are the same on all of them. It was run on macOS arm64 only. Linux was not run locally. Windows was not built or run.

How this was tested

  • RED → GREEN: test_config_double_window 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 it ON, it fails on RED with the output above and passes on GREEN. All seven mutants are killed.
  • 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 is a pure value conversion, so the full app wasn't run. Parity was checked instead with the getter-only probe described under Evidence.

Status

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


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.

os_strtod() copies at most 63 bytes before strtod(), so leading
whitespace and the sign count against the window and later bytes are
ignored. Test both double getters at the 63/64 byte boundary for
decimal and hex values, including a hex value pushed past the window by
leading spaces: Rust ON parses the whole string.
os_strtod() copies at most sizeof(buf) - 1 = 63 bytes into a local
buffer before strtod(). Cut the whole stored string to that window in
config_get_double and config_get_default_double, before whitespace
skipping, the sign and the hex/decimal dispatch, so those bytes count
against the window as they do in C.
@zackees
zackees marked this pull request as ready for review October 14, 2026 23:10
@zackees
zackees self-requested a review as a code owner October 14, 2026 23:10
@zackees
zackees merged commit 54b2e10 into obs-rust:master Oct 14, 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