Skip to content

lib: range-check doubles before auto-converting to int and int64 - #309

Merged
hyperrealm merged 2 commits into
hyperrealm:masterfrom
affan-arch:autoconvert-float-range-check
Nov 7, 2026
Merged

hyperrealm merged 2 commits into
hyperrealm:masterfrom
affan-arch:autoconvert-float-range-check

Conversation

@affan-arch

Copy link
Copy Markdown
Contributor

with autoconvert on, __config_setting_get_int(), __config_setting_get_int64() and config_setting_set_float() cast a double straight to int or long long, so a config value like x = 1e300; read through config_lookup_int() is undefined behavior (ubsan: 1e+300 is outside the range of representable values of type 'int') and the lookup reports success with a saturated or indefinite value; they now check the double fits the target type first and return CONFIG_FALSE otherwise, the same way the int64-to-int path already does, and the new AutoConvertFloatToInteger test fails without the fix.

Comment thread lib/libconfig.c Outdated

static int __config_double_fits_int(double value)
{
return((value >= -2147483648.0) && (value < 2147483648.0));

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of hardcoded constant 2147483648.0, why not (double)INT_MAX + 1.0

Similarly for the other constants.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, the int bounds are (double)INT_MIN and (double)INT_MAX + 1.0 now. for the int64 upper bound i used -(double)LLONG_MIN instead of (double)LLONG_MAX + 1.0: LLONG_MAX already rounds up to 2^63 as a double, so the + 1.0 is a no-op there, and on x87 with excess precision the sum can come out as 2^63 + 1, which would let 2^63 itself through the check. can switch it to the symmetric form if you prefer.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, thanks!

@hyperrealm

Copy link
Copy Markdown
Owner

please rebase and then I'll merge.

@affan-arch
affan-arch force-pushed the autoconvert-float-range-check branch from 03774f4 to bf0d2c0 Compare November 6, 2026 08:09
@affan-arch

Copy link
Copy Markdown
Contributor Author

rebased on master. the only conflict was tests/tests.c, where WriteLargeFloat from #307 landed in the same spot, so both tests are kept now and the suite passes.

@hyperrealm
hyperrealm merged commit a82aa4d into hyperrealm:master Nov 7, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants