Repository navigation
config: Limit double getters to C's 63-byte conversion window - #738
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
With
ENABLE_RUST_LIBOBS=ON,config_get_doubleandconfig_get_default_doubleread past the 63-byte window that C'sos_strtodconverts. A stored value whose number starts or continues past byte 63 converts differently: 63 spaces then1(64 bytes) gives 1.0 in Rust and 0.0 in C, and0.+ 61 zeros +1gives 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 portOFF, nothing changes.Fixes: TBD
Root cause
C's getters (
libobs/util/config-file.c:692-699and:765-772) pass the stored string toos_strtod(libobs/util/platform.c:564-571). That copies at mostsizeof(buf) - 1= 63 bytes intochar buf[64], swaps the decimal point for the locale's, and callsstrtodon 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 gettersConfig::get_doubleandConfig::get_default_double(rust/obs-util/src/config_file.rs:974-977and:1095-1098) pass the whole stored string toparse_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 atos_strtodinlibobs/util/platform.c.os_strtod(:574-576) cuts the stored bytes toOS_STRTOD_BUF - 1and then calls the existingparse_double. Both getters call it (:990,:1111).The cut is made on the whole stored string, before
parse_doubleskips whitespace, reads the sign, or chooses between hex and decimal. That is where C makes it:strncpycopies the raw bytes beforestrtodlooks at any of them. A cut taken later measures the window from the wrong byte. For example, 61 spaces then0x1is 64 bytes, so C sees only 61 spaces and0xand returns 0.0. A cut after the whitespace still sees0x1and 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:
parse_double: rejected.parse_doubleis thestrtodmodel and has other callers in waiting (see Compatibility). The window belongs toos_strtod, so it sits in a wrapper with that name.os_strtod(window, locale swap, libcstrtod) 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-windowon the fork, two commits on29d8bcc8b:47b26e625937626fac53f9ea88fb8c887c102c26"config: Test 63-byte double conversion window (RED)"fd24d2d6ada21142405dcfdc646072b20d3beb04"config: Limit double getters to C's 63-byte conversion window (GREEN)"The test is
test/cmocka/test_config_double_window.c, registered intest/cmocka/tests.d/test_config_double_window.cmake. Each row is stored as a user value and as a default value, read back throughconfig_get_doubleandconfig_get_default_double, and compared bit for bit with the value C gives:spaces_63_bytes1spaces_64_bytes1spaces_65_bytes1fraction_63_bytes0.+ 60 zeros +1fraction_64_bytes0.+ 61 zeros +1integer_63_bytes1+ 62 zerosinteger_64_bytes1+ 63 zerosexponent_63_bytes1.+ 59 zeros +e5exponent_64_bytes1.+ 60 zeros +e5hex_63_bytes0x+ 60 zeros +1hex_64_bytes0x+ 61 zeros +1hex_spaces_64_bytes0x1Local runs: macOS arm64, CMake Xcode generator, Debug, the repo's own flags.
RED with
ENABLE_RUST_LIBOBS=OFFpasses all 12 rows. RED withON(themasterRust) fails on the first 64-byte row:(
4607182418800017408is0x3ff0000000000000, which is 1.0.)GREEN passes all 12 rows with the Rust port
OFFandON:At GREEN, the whole cmocka suite passed 88/88 with
OFFand withON(test_svt_av1skipped, as onmaster).cargo test -p obs-util --lockedpassed 472 tests with 0 failures.cargo clippy -p obs-util --all-targets -- -D warningsandcargo fmt --checkare 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
ONand run against the committed test.spaces_63_bytesspaces_64_bytesspaces_64_bytesspaces_64_bytesspaces_64_bytesspaces_64_bytes; with the decimal space rows removed, still killed byhex_64_byteshex_64_bytes; with that row removed, still killed byhex_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_doubleandconfig_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 theCandde_DE.UTF-8locales, 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
ON, values longer than 63 bytes now convert the same as with itOFF. Shorter values are unchanged.fix/config-hex-float-rounding: that branch deletesparse_hex_floatand sends hex values to a libc-backedplatform::os_strtod. The two branches conflict in one hunk ofrust/obs-util/src/config_file.rs, because this branch adds the wrapper just aboveparse_hex_float. The resolution is to keep this branch'sOS_STRTOD_BUFand window wrapper and dropparse_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.fix/config-hex-float-roundingthen mergesmasterwith 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 twoos_strtodfunctions (for exampleconfig_file::os_strtod_window) and change itsplatform::os_strtoddoc 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 mergesmaster, and the resulting tree is the same.de_DE, C converts1,5to 1.5, becauseos_strtodcalls the locale-awarestrtod. The Rust decimal path gives 1.0.nan(0x1234)gives0x7ff8000000001234, andnan(1234)gives0x7ff80000000004d2). Rust returns the default NaN0x7ff8000000000000.os_strtodand 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
test_config_double_windowwas built locally on macOS arm64 with the CMake Xcode generator (Debug, repo flags, no sanitizers). WithENABLE_RUST_LIBOBS=OFF, it passes on both RED and GREEN. With itON, it fails on RED with the output above and passes on GREEN. All seven mutants are killed.Status
Status: draft. Opened so upstream PR CI can run it. Branch
fix/config-double-windowatfd24d2d6ada21142405dcfdc646072b20d3beb04.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.