Skip to content

Error when parsing implausibly large float strings - #165163

Open
wpegden wants to merge 1 commit into
rust-lang:mainfrom
wpegden:dec2flt-large-input-error
Open

wpegden wants to merge 1 commit into
rust-lang:mainfrom
wpegden:dec2flt-large-input-error

Conversation

@wpegden

@wpegden wpegden commented Oct 29, 2026 •

Copy link
Copy Markdown

This proposes that dec2flt error on very large decimal character string inputs (2^50 bytes) rather than claim to process them correctly.

Context: In an attempted lean formalization of the the correctness of the behavior of the dec2flt libary these bugs were discovered:
#163674
#163676

They concern parsing and overflow problems that can occur with large inputs.

The pull request #164331 proposes fixes for these problems but still doesn't handle some physically enormous (exabyte scale) inputs. Although the code could be changed to handle these, the added complexity seems like it may not be worth it in favor of just refusing on these huge inputs, as suggested/considered in #164331 (comment)

With this change, together with PR #164331, the code would work on all inputs.

(Without #164331, there are inputs of size around 65KiB that are also incorrect)

2^58 would actually suffice here; 2^50 is still enormous and may make it easier to reason when reading.

Note: these PRs are generated in response to a project Lean-formalizing the correctness of the dec2flt library using an LLM based autoformalization system; LLMs were used to write code but not commit messages, comments, etc.

Together with other fixes separately proposed, the code would be correct on all inputs after this change.

(Without the other fixes, there are inputs of size around 65KiB that are also incorrect)

2^58 would actually suffice here; 2^50 is still enormous and may make it easier to reason when reading.

The existing code even with the other proposed fixes is invalid for some inputs of size around 2^59 bytes.  This could be corrected but the added complexity may not be worth it given the practical nonrealizability of these inputs.
@rustbot

rustbot commented Oct 29, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in float parsing

cc @tgross35

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 29, 2026
@rustbot

rustbot commented Oct 29, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project has assigned @JohnTitor (or someone else) to review your changes, you should hear from them (or someone else) within the next two weeks.

Please see the contribution instructions and our LLM policy for more information.

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 37 candidates
  • Random selection from ChrisDenton, JohnTitor, Mark-Simulacrum, clarfonthey, hanna-kruppe

@Kivooeo

Kivooeo commented Oct 29, 2026

Copy link
Copy Markdown
Member

r? tgross35

@rustbot rustbot assigned tgross35 and unassigned JohnTitor Oct 29, 2026
@rustbot

rustbot commented Oct 29, 2026

Copy link
Copy Markdown
Collaborator

tgross35 is currently at their maximum review capacity.
They may take a while to respond.

@Kivooeo Kivooeo added the llm-assisted An LLM-assisted PR as defined by the LLM policy. Requires ahead-of-time consent by assignee. label Oct 29, 2026
@tgross35

Copy link
Copy Markdown
Member

Please write your PR summaries/descriptions and commit messages as if nobody knows what the context is. I'm lucky enough to know the context, but based on the description you might think it's about rustc erroring on large source files, or decreasing the precision of some float operation with large inputs :)

Comment on lines +32 to +33
#[cfg(any(target_pointer_width = "16", target_pointer_width = "32"))]
assert_eq!(check_input_length(usize::MAX), Ok(()));

@workingjubilee workingjubilee Oct 30, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This part of the code doesn't really do anything to verify the correctness of the implementation, it is only verifying that 32-bit platforms and 16-bit platforms are 32-bit and 16-bit, respectively, so either just don't put any code here (and consider changing the entire test to be under #[cfg(target_pointer_width = "64")]) or change the original function to instead be over u64 (since the function, itself, does not actually consume any such string inputs, though ofc it will never reach an error so... like... why bother? it's one of your, how to say, conundrums of philosophy)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Additionally I don't really think this is useful anyway as a (effective) unit test, it's basically checking that >= 1u64 << 50 works right. An integration test would be better but that's not really possible, so it's fine to trust the simple op here.

You can do a "bad" integration test locally as a sanity check, using slice::from_raw_parts to fudge a massive string length -- but don't commit that.

@workingjubilee workingjubilee Oct 30, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Specifically that would be UB, yes, which we probably should not have in the test suite, as it should make miri complain.

FloatErrorKind::Empty => "cannot parse float from empty string",
FloatErrorKind::Invalid => "invalid float literal",
FloatErrorKind::TooLong => {
"ERROR: Your input is very large. Only input lengths < 2^50 are supported"

@workingjubilee workingjubilee Oct 30, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why does this have an ERROR prefix? The other strings here do not. If we need to emit this error in such a way that it should get that prefix, we add it elsewhere.

View changes since the review

@workingjubilee workingjubilee Oct 30, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

also the other strings are not addressed with "your", nor do they talk about "support". The "you" can easily be a user of a Rust program who did nothing, as this parse function can be invoked at runtime, by a program, on a string that is not user input, so if the program surfaced this message during a panic, addressing "you" is weird. If they filed a support ticket, it would not go to someone who could change what is "supported", here. We do not need to be helpful, chatty, and informative, we need to simply say "no".

If the error is surfaced via rustc using this function, of course, it will be its usual chirpy self.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 30, 2026
@rustbot

rustbot commented Oct 30, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@workingjubilee workingjubilee changed the title This introduces an error on very large inputs. Error when parsing implausibly large float inputs Oct 30, 2026
@workingjubilee workingjubilee changed the title Error when parsing implausibly large float inputs Error when parsing implausibly large float strings Oct 30, 2026
@Kivooeo

Kivooeo commented Oct 30, 2026

Copy link
Copy Markdown
Member

but based on the description you might think it's about rustc erroring on large source files

Yes, it was my first thought, but then I read more context around to understand what is going on

@workingjubilee

workingjubilee commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Please feel free to change the PR's title further to your taste, @wpegden, I just wanted to make sure it was clear that this was about floats written using very large strings rather than in a hypothetical IEEE754 binary floating point format that was extended to use... Too Many bytes. It would be nice if you made a similar change to the commit message's first "subject" line itself, also in whatever way matches your preference on what to communicate, but I will leave the final decision on that to Trevor.

@tgross35

Copy link
Copy Markdown
Member

@wpegden for future reference, try not to use an LLM for actually writing the code: you can definitely use it for reference and to review what you write, but it hits more clauses in our LLM policy if you didn't actually write the code yourself. And these changes are pretty small, I expect that handwritten code will wind up more natural anyway (probably part of why this tiny PR gathered a ton of feedback).

No worries for work that's already done, of course.

Comment on lines +200 to +207
#[inline]
pub fn check_input_length(len: usize) -> Result<(), ParseFloatError> {
if len as u64 >= 1u64 << 50 {
Err(ParseFloatError { kind: FloatErrorKind::TooLong })
} else {
Ok(())
}
}

@tgross35 tgross35 Oct 30, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Once the test is removed, this can be inlined. But put the max value in a constant with a comment about how it was selected.

View changes since the review

Comment on lines 29 to +36
/// Note that alphabetical characters are not case-sensitive.
///
/// Leading and trailing whitespace represent an error.
///
/// # Grammar
///
/// All strings that adhere to the following [EBNF] grammar when
/// lowercased will result in an [`Ok`] being returned:
/// All strings of length < 2^50 bytes that adhere to the following [EBNF] grammar
/// when lowercased will result in an [`Ok`] being returned:

@tgross35 tgross35 Oct 30, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Rather than squishing it into grammar, add a sentence after "Leading and trailing whitespace represents an error".

Also these docs are wrapped to 80 chars, that is kind of unusual for std but try to be consistent anyway.

View changes since the review

@tgross35

Copy link
Copy Markdown
Member

Zulip discussion: #t-libs > Capping float strings to a sane value

@wpegden

wpegden commented Nov 3, 2026

Copy link
Copy Markdown
Author

Discussion there seems to have petered out... but seems to have been generally favorable? should I make the changes here e.g. to the error message (sorry, was my first time writing a rust library error message!)

@tgross35

Copy link
Copy Markdown
Member

@wpegden would limiting to i32::MAX >> 1 mean we can do needed arithmetic in an i32? If so then I think that could be reasonable, so we get slightly better performance on 32-bit (and 16-bit) platforms.

@rust-bors

rust-bors Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #166567) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llm-assisted An LLM-assisted PR as defined by the LLM policy. Requires ahead-of-time consent by assignee. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants