Skip to content
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

Closed
BurntSushi opened this issue May 16, 2017 · 14 comments
Closed

from_str_radix panics for some values of radix #42034

BurntSushi opened this issue May 16, 2017 · 14 comments
Labels
C-bug Category: This is a bug. I-needs-decision Issue: In need of a decision. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@BurntSushi
Copy link
Member

The from_str_radix methods on the various number types panics if radix < 2 or if radix > 36: https://doc.rust-lang.org/src/core/num/mod.rs.html#2693

I think we should fix this through one of two means:

  1. Decide that it is a contract violation and document the cases where the function panics.
  2. Decide that it is a normal error, be thankful that the ParseIntError internals aren't exposed, and return an error when radix is invalid. Finally, document the error condition.

cc @rust-lang/libs

@BurntSushi BurntSushi added I-needs-decision Issue: In need of a decision. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels May 16, 2017
@alexcrichton
Copy link
Member

IIRC this is intentional, because the radix is 99% of the time a literal.

@sanmai-NL
Copy link

How can failing to parse something potentially untrusted ever be construed as a contract violation? In my eyes it is logical to return an Err instead here.

Other use-cases of assert! include testing and enforcing run-time invariants in safe code (whose violation cannot result in unsafety).

The contents of a string does not observe any invariant other than being a string.

@sanmai-NL
Copy link

@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?

@sanmai-NL
Copy link

sanmai-NL commented May 17, 2017

If returning Results is found too costly for a parsing function, then perhaps it can be implemented to panic given a &'static str argument. That is, assuming that is deemed trusted input. This is a general suggestion, the API can't be changed so easily of course. For sure, the current implementation isn't consistent in its error handling approach.

@BurntSushi
Copy link
Member Author

How can failing to parse something potentially untrusted ever be construed as a contract violation?

The contract violation is in the set of allowable values for the radix parameter, not the string.

@sanmai-NL
Copy link

@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?

@BurntSushi
Copy link
Member Author

Why do you think performance has anything to do with this? Doesn't seem related to me.

@sanmai-NL
Copy link

sanmai-NL commented May 17, 2017

Do you see another reason why the assertion was put in (1) instead of an Err return (2)? Do you agree 1 has the downside making handling errors harder? I do not see any downsides to 2 in this case. So I take it there must be a very good motivation to implement 1. When abort is the panic strategy, I believe 1 is less costly in the non-error case than 2. But I do not have benchmarks to back this up.

@bluss
Copy link
Member

bluss commented Jul 30, 2017

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 😉

tbu- added a commit to tbu-/rust that referenced this issue Jul 30, 2017
@tbu-
Copy link
Contributor

tbu- commented Jul 30, 2017

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).

bors added a commit that referenced this issue Jul 30, 2017
@aturon aturon added the S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). label Aug 8, 2017
@aturon
Copy link
Member

aturon commented Aug 8, 2017

+1 for declaring it a contract violation

@bluss
Copy link
Member

bluss commented Aug 9, 2017

With the merge of #43563, this was resolved, so any objection needs to be swift. The issue seems to be resolved otherwise.

@Phlosioneer
Copy link
Contributor

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.

@bluss
Copy link
Member

bluss commented Mar 24, 2018

Thanks @Phlosioneer, this issue is resolved. The possible panic was documented in #43563.

@bluss bluss closed this as completed Mar 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. I-needs-decision Issue: In need of a decision. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

8 participants