Skip to content

lib: don't truncate large floats in config_write - #307

Merged
hyperrealm merged 2 commits into
hyperrealm:masterfrom
emincan2000:write-large-floats
Nov 5, 2026
Merged

hyperrealm merged 2 commits into
hyperrealm:masterfrom
emincan2000:write-large-floats

Conversation

@emincan2000

Copy link
Copy Markdown
Contributor

config_write formats floats into value_buf, which only has room for a 64-bit binary integer. %f never switches to an exponent, so large values get cut off at 61 characters, leaving a different number in the file. x = 1e300 reads back as about 1e60, with no error.

The float case now uses its own buffer with room for every integer digit of DBL_MAX.

I added a WriteLargeFloat test that writes and rereads values from 1e60 up to DBL_MAX. It fails on master, and the suite passes with the fix. I also compared old and new output on 300k random doubles under ASan and UBSan: nothing below 1e55 changed.

Very small values such as 1e-300 still come out as 0.0 unless scientific notation is enabled. I left that separate issue unchanged.

value_buf only has room for a 64-bit binary integer, and %f never uses
an exponent, so large floats were cut off at 61 characters and written
as a different value. Give the float case a buffer that fits every
integer digit of DBL_MAX.
Comment thread lib/libconfig.c Outdated
{
/* %f never switches to an exponent, so this must hold every integer
digit of DBL_MAX; only fractional digits may be truncated. */
char float_buf[DBL_MAX_10_EXP + 64];

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.

It would be better to just make value_buf big enough for any type of value instead of having a separate buffer here just for floats.

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.

Agreed, that's simpler. I removed float_buf and made value_buf large enough for both 64-bit binary output and floats printed with %f, so floats use value_buf again. All tests still pass, including the new WriteLargeFloat test.

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