-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
from_str_radix panics for some values of radix #42034
Comments
IIRC this is intentional, because the radix is 99% of the time a literal. |
How can failing to parse something potentially untrusted ever be construed as a contract violation? In my eyes it is logical to return an
The contents of a string does not observe any invariant other than being a string. |
@alexcrichton: be it as it may that the expected use case on string literals makes the assertion more acceptable, since this method is already fallible, is there a great cost to not using the assertion there? |
If returning |
The contract violation is in the set of allowable values for the |
@BurntSushi: Sorry, I was completely mistaken! Seems I misread there. 😕 I still wonder whether replacing the assertion with an early return has a significant performance cost at all. From my perspective, avoiding a panic is worth a lot, but perhaps some people with high performance requirements disagree? |
Why do you think performance has anything to do with this? Doesn't seem related to me. |
Do you see another reason why the assertion was put in (1) instead of an |
The radix parameter is a typical case for "contract violation" clause error handling, which is used when passing an invalid parameter is a programmer error / program bug and not an error encountered in normal operation. (That assumes the text can be unsanitized program input and the radix is not.) Mixing panic and result error handling in the same call is tricky — do we have any other good examples of doing that? The worst of both those worlds is to not have it documented at all 😉 |
Fun. When referencing the function in the other issue, I also noticed it didn't document the error so I went ahead and made a pull request (#43563). |
Document the `from_str_radix` panic CC #42034
+1 for declaring it a contract violation |
With the merge of #43563, this was resolved, so any objection needs to be swift. The issue seems to be resolved otherwise. |
Triage: I think this should be closed? No real decision was made; however, the PR to document it landed, and that seems to make everyone happy. |
Thanks @Phlosioneer, this issue is resolved. The possible panic was documented in #43563. |
The
from_str_radix
methods on the various number types panics ifradix < 2
or ifradix > 36
: https://doc.rust-lang.org/src/core/num/mod.rs.html#2693I think we should fix this through one of two means:
ParseIntError
internals aren't exposed, and return an error whenradix
is invalid. Finally, document the error condition.cc @rust-lang/libs
The text was updated successfully, but these errors were encountered: