Repository navigation
Conversation
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.
|
Some changes occurred in float parsing cc @tgross35 |
|
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:
|
|
r? tgross35 |
|
|
|
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 :) |
| #[cfg(any(target_pointer_width = "16", target_pointer_width = "32"))] | ||
| assert_eq!(check_input_length(usize::MAX), Ok(())); |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
Reminder, once the PR becomes ready for a review, use |
Yes, it was my first thought, but then I read more context around to understand what is going on |
|
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. |
|
@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. |
| #[inline] | ||
| pub fn check_input_length(len: usize) -> Result<(), ParseFloatError> { | ||
| if len as u64 >= 1u64 << 50 { | ||
| Err(ParseFloatError { kind: FloatErrorKind::TooLong }) | ||
| } else { | ||
| Ok(()) | ||
| } | ||
| } |
There was a problem hiding this comment.
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.
| /// 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: |
There was a problem hiding this comment.
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.
|
Zulip discussion: #t-libs > Capping float strings to a sane value |
|
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!) |
|
@wpegden would limiting to |
|
☔ The latest upstream changes (presumably #166567) made this pull request unmergeable. Please resolve the merge conflicts by rebasing. |
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.